【发布时间】:2020-04-24 12:39:37
【问题描述】:
我正在使用 Docker 的 nginx:1.16.0-alpine 图像作为服务反应应用程序(之前构建的),并且我想在任何情况下都重定向到 index.html 页面(在获得什么 URL 时)
nginx.conf文件有以下内容:
user nginx; worker_processes auto;
error_log /var/log/nginx/error.log warn; pid
/var/run/nginx.pid;
events {
worker_connections 1024; }
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
location / {
try_files $uri $uri/ /index.html;
}
location ~ ^/$ {
rewrite ^.*$ /index.html last;
}
}
}
其实server部分是加的,其他行都是默认的!
Dockerfile内容如下:
FROM nginx:1.16.0-alpine
COPY ./build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
为了确保在从图像构建容器并进入其中的外壳之后,文件/etc/nginx/nginx.conf 具有上述内容。
但问题是:当我浏览 url http://localhost:3000/login(例如)时,它不会重定向到 http://localhost:3000/index.html。它显示:
404 未找到 nginx/1.16.0
(Docker 容器在输出端口 3000 和本地端口 80 上运行)
有谁知道为什么它不起作用!?
(我也搜索了类似的方法,但没有成功!)
更新:
页面React-router and nginx 没有解决问题。
【问题讨论】:
-
@emix,请查看
location部分,该部分显示任何 URL 重定向到/index.html,但不起作用 -
好的,内容隐藏在移动设备中。您的问题存在严重的格式问题。这是一个代码,不知道你为什么把它放在引号里。
-
这能回答你的问题吗? React-router and nginx
-
@yurloc,很遗憾没有!
标签: reactjs docker nginx config