【发布时间】:2018-07-16 17:26:06
【问题描述】:
我有一个简单的 nginx 配置来服务我的 React 应用程序:
server {
listen 80;
root /usr/share/nginx/html;
location / {
try_files $uri $uri/ /index.html;
}
}
我有路线/ 和login。当我通过 React-Router 从 localhost/ 转到 localhost/login 时,一切正常。但是当我在浏览器中输入localhost/login 时,我得到了 404 错误。
确切地说,对于localhost/login,我在 nginx 控制台中得到"/usr/share/nginx/html/login" failed (2: No such file or directory)。
对于localhost/login/,我得到"/usr/share/nginx/html/login/index.html" is not found (2: No such file or directory)
我不明白为什么 nginx 一直在 /usr/share/nginx/html/login/ 中寻找 index.html 而不是 /usr/share/nginx/html/。任何想法我做错了什么?
【问题讨论】:
-
通过路径
/usr/share/nginx/html有一个index.html? -
是的,那个目录里有
index.html -
你想通过路由
/login打开你的应用index.html吗? -
是的,没错,
index,html在任何路线上 -
我已经更新了我的答案尝试改变你的规则 try_files 就像我的答案一样
标签: reactjs nginx react-router