【发布时间】:2021-11-26 11:35:01
【问题描述】:
首先,我知道这个问题已经被问过很多次了,我已经浏览了所有解决方案,但我一直无法解决我的问题。我在 Dockerized Django 和 React Application 上收到上述错误。我可以毫无问题地通过 HTTPS 访问 Django API 和管理站点。但是,当尝试访问 React 站点时,我最终得到了上述错误。这是我的 Nginx 配置
server {
listen ${LISTEN_PORT_HTTP};
server_name example.com;
server_tokens off;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 default_server ssl http2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/live/example.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/example.com/privkey.pem;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /static {
alias /vol/static;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location /api {
uwsgi_pass ${APP_HOST}:${APP_PORT};
include /etc/nginx/uwsgi_params;
client_max_body_size 20M;
}
location /admin {
uwsgi_pass ${APP_HOST}:${APP_PORT};
include /etc/nginx/uwsgi_params;
client_max_body_size 20M;
}
}
我相信问题来自 NGINX。如果需要更多信息,我会提供。谁能帮我解决这个问题?谢谢
更新
我意识到try_files $uri $uri/ /index.html; 是导致循环重定向的行。删除它后,我可以看到页面正在加载,但反应静态路由通过 django 的static nginx 路由,因此抛出404 错误,如屏幕截图所示。谁能帮助我如何路由反应静态文件?
FROM nginxinc/nginx-unprivileged:1-alpine
COPY --from=portfolio-front_react-app /app/build /usr/share/nginx/html
COPY ./default.conf.tpl /etc/nginx/default.conf.tpl
COPY ./uwsgi_params /etc/nginx/uwsgi_params
COPY ./run.sh /run.sh
ENV LISTEN_PORT_HTTP=8000
ENV LISTEN_PORT_HTTPS=443
ENV APP_HOST=app
ENV APP_PORT=9000
USER root
RUN mkdir -p /vol/static && \
chmod 755 /vol/static && \
touch /etc/nginx/conf.d/default.conf && \
chown nginx:nginx /etc/nginx/conf.d/default.conf && \
chmod +x /run.sh
VOLUME /vol/static
# USER nginx
CMD ["/run.sh"]
【问题讨论】:
-
你的 nginx 配置看起来不错。似乎nginx只是在
/usr/share/nginx/html目录中找不到index.html文件,就像它发生在here一样(参见问题下的讨论)。重新检查您的 docker 构建配置。 -
感谢您的评论。在检查上述文件夹时,我可以看到
build的内容包括index.html