引言
在寻找try_files的解决方案时,发现很多讲解文章存在误区,特写明一下,帮助大家理解
代码
location / {
alias /home/appName/;
index index.html;
try_files $uri $uri/ /index.html;
}
误区
在这一段location配置中,解析流程是什么样的呢
举例:请求路径为 http://127.0.0.1:80/
graph TD
URL_http://127.0.0.1:80/ --> 文件夹_/home/appName/ --> 文件夹/home/appName// --> URL_http://127.0.0.1:80/index.html
误区点在try_files的最后一个参数,nginx会将其解析为URL而不是文件路径
引发问题点
可能有些人认为这个无所谓嘛,但是在请求路径和文件夹名称不一致时,会出现解析不到的场景:
location /appName/ {
alias /home/app-name/;
index index.html;
try_files $uri $uri/ /app-name/index.html;
}
在这种配置模式下,解析流程是什么样的呢
举例:请求路径为 http://127.0.0.1:80/appName/
graph TD
URL_http://127.0.0.1:80/appName/ --> 文件夹_/home/app-name/ --> 文件夹/home/app-name// --> URL_http://127.0.0.1:80/app-name/index.html
这时nginx就会找不到/app-name这个请求路径了