reactjs - 使用 React 应用程序的 Nginx 500 内部服务器错误 - 1 次重写或内部重定向周期,同时内部重定向到“/index.html”
问题描述
这是我的 nginx default.conf 文件:
server {
listen 3000;
location / {
root usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
这是我的 Dockerfile:
FROM node:14-slim as builder
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx
EXPOSE 3000
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/build /usr/share/nginx/html
如果我查看我的 Docker 容器,我可以看到文件都在它们应该在的位置:
同样对于上下文,我还使用 nginx 进行路由,配置如下:
upstream client {
server client:3000;
}
upstream api {
server api:3001;
}
server {
listen 81;
location / {
proxy_pass http://client;
}
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
location /sockjs-node {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
但是,如果我尝试导航到 localhost:81,我会收到以下错误:
client_1 | 2021/02/18 15:46:35 [error] 28#28: *10 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 172.18.0.3, server: , request: "GET / HTTP/1.0", host: "client"
任何想法可能是什么问题?我看过的大多数其他线程都集中在这一点上,但我已经有了它,而且我确信文件位置都是正确的。:
server {
listen 3000;
location / {
root usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html; <-- this line here
}
}
另一个线程建议将"homepage": "./
" 添加到package.json
文件中,但这似乎也没有任何区别。
解决方案
能够从迈克尔汉普顿那里得到关于服务器故障的答案:
修复根指令中的错字。
缺少前导 /,因此 nginx 认为路径相对于编译的默认值(通常是 Linux 上的 /etc/nginx)。
实际发生的是目录 /etc/nginx/usr/share/nginx/html 不存在。因此,在处理 try_files 时,nginx 首先尝试 $uri 失败,然后尝试 $uri/ 处理 index 指令中的路径。这些都不存在。最后它到达 /index.html,它反馈给 try_files,但由于它与它刚刚在索引处理中尝试的路径相同,它检测到无限循环并退出。
所以我所要做的就是改变这个
root usr/share/nginx/html;
对此:
root /usr/share/nginx/html;
推荐阅读
- python-3.x - 在 readthedocs.org 上构建 Sphinx 文档时将所有警告视为错误
- c++ - 如何从 shell 脚本中获取 echo 语句以及执行状态到 c++ 程序中
- r - 在 R 中读取很长的字符串时遇到问题
- amazon-web-services - 单个 EC2 预留实例是否允许在部分小时内使用多个 EC2 实例
- c# - 如何在 WPF 的网页中实现与滚动查看器相同的行为?
- swiftui - SwiftUI List 基于动态内容的行填充变化
- laravel-5 - 获取所有 json post 请求的令牌过期
- activemq-artemis - Apache Artemis 和地址路由
- c++ - MPI_Bcast 导致分段错误
- php - Drupal 8.8.5 上奇怪的文件/文件夹权限错误