景象
通过首页进入拜访页面失常,刷新之后出404页面
起因
起因是因为web单页面开发模式,只有一个index.html入口,其余门路是前端路由去跳转的,nginx没有对应这个门路,当然就是404了。
解决方案
location / { root /usr/nginx/app/dist/; index index.html; try_files $uri $uri/ /index.html;}
总结
在配置中加上try_files,意思跟翻译差不多,“尝试读取文件”。u r i 这 个 是 n g i n x 的 一 个 变 量 , 存 放 着 用 户 访 问 的 地 址 , 例 如 h t t p : / / l o c a l h o s t : 8200 / c h o o s e S i z e 那 么 uri 这个是nginx的一个变量,寄存着用户拜访的地址,例如http://localhost:8200/chooseSize 那么uri这个是nginx的一个变量,寄存着用户拜访的地址,例如http://localhost:8200/chooseSize那么uri就是/chooseSize;u r i / 代 表 访 问 的 是 一 个 目 录 例 如 h t t p : / / l o c a l h o s t : 8200 / c h o o s e S i z e / 那 么 uri/ 代表拜访的是一个目录 例如http://localhost:8200/chooseSize/ 那么uri/代表拜访的是一个目录例如http://localhost:8200/chooseSize/那么uri/就是/chooseSize/;最初/index.html就是咱们首页的地址。
最终下面的意思是如果第一个存在,间接返回;不存在的话读取第二个,如果存在,读取返回;如果还是不存在,就会fall back到 try_files 的最初一个选项 /index.html,发动一个外部 “子申请”,也就是相当于 nginx 发动一个 HTTP 申请http://localhost:8200/index.html,再通过前端路由到/chooseSize