Nginx有两种配置静态路径文件的方法:
1,root配置
location / {
root /var/app_secrecy/resources/templates; //静态文件的在服务器路径
}
需要注意,使用root配置是,如果需要加前缀URI情况下,如:
location /app/ {
root /var/app_secrecy/resources/templates;
}
访问的地址:http://xxxx/app/a.html
其访问的路径: /var/app_secrecy/resources/templates/app/a.html
有可能引起404问题。
2,alias配置
针对有前缀URI的静态html需求,建议使用alias配置。
location ^~/app/ {
alias /var/app_secrecy/resources/templates/;
}
访问地址:http://xxxx/app/a.html
其访问的路径:/var/app_secrecy/resources/templates/a.html
需要注意:在alias配置时,alias配置目录名必须包含"/"。
3,总结
root配置:配置路径+完整的访问路径(完整的location前缀+静态文件);
alias配置:配置路径+静态文件(去掉location的URI前缀);
本文详细比较了Nginx中root和alias配置静态文件的区别,包括适用场景、路径结构和可能遇到的问题。推荐alias配置处理带前缀URI的静态资源访问。
1万+

被折叠的 条评论
为什么被折叠?



