reactjs - nginx 将所有 url 重定向到 index.html
问题描述
我正在尝试根据以下指南配置 nginx 服务器:https ://gkedge.gitbooks.io/react-router-in-the-real/content/nginx.html
但看起来它不起作用,我正在尝试使用我的 nginx docker 映像。所以,
Dockerfile:
FROM nginx:1.11.5
COPY /dist /usr/share/nginx/html
COPY /vhosts.conf /etc/nginx/conf.d/vhosts.conf
vhosts.conf:
server {
listen 80 default_server;
server_name /usr/share/nginx/html;
root /usr/share/nginx/html;
index index.html index.htm;
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
# access_log logs/static.log; # I don't usually include a static log
}
location ~* \.(?:css|js|json)$ {
try_files $uri =404;
expires 1y;
access_log off;
add_header Cache-Control "public";
}
# Any route containing a file extension (e.g. /devicesfile.js)
location ~ ^.+\..+$ {
try_files $uri =404;
}
# Any route that doesn't have a file extension (e.g. /devices)
location / {
try_files $uri $uri/ /index.html;
}
}
试图打开一个不存在的页面。
GET localhost/somestupidrandomurl
nginx错误日志:
[error] 7#7: *3 open() "/usr/share/nginx/html/somestupidrandomurl" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /somestupidrandomurl HTTP/1.1", host: "localhost"
提前致谢。
解决方案
经过深入搜索,我意识到问题是,/etc/nginx/conf.d/
路径中还有一个 default.conf 文件,该配置覆盖了我的配置。
我只是将 Dockerfile 第 3 行替换为
COPY /vhosts.conf /etc/nginx/conf.d/default.conf
并且像魔术一样工作。
推荐阅读
- r - 配对 t 检验的问题:并非所有参数都具有相同的长度
- asp.net - asp.net 代码第一个多对多同一张表
- bash - OSX 终端继续自行接线
- asp.net-mvc - 如何在新数据库中执行 SQL 查询?
- java - 从相机捕获图像时位图为空
- database - 如何在 DolphinDB 的内存表中添加一列?
- excel - Powerquery 将分隔的列拆分为唯一的行
- html - 使最小列的多列大小
- c++ - 功能无法匹配 GLDEBUGPROC,仅在 MSVC 上(使用相同的 glew 版本 2.1.0,在带有 gcc/clang 的 linux 上很好)
- java - 自定义输入强制 Pattern.compile 抛出 PatternSyntaxException