问题来源:
在做一个项目时要用到nginx,本来配置nginx是一件不太难的事情,结果怎么都访问不了 (404都是泪:( )。
server {
...
location / {
// 就是这里,alias 与 root配置,后面要 / 结尾,而且windows下也要使用 /,否则报错
alias D:/xc-nginx/nginx-1.16.1/html/xc-ui-pc-static-portal/;
index index.html index.htm;
}
...
但刚开始源文件添加的是 root,于是想探究一下root与alias区别
server {
...
location / {
root D:/xc-nginx/nginx-1.16.1/html/xc-ui-pc-static-portal;
index index.html index.htm;
}
...
探究root与alias
root和alias都是nginx指定文件路径的方式
[root]
语法:root path
默认值:root html
配置段:http、server、location、if
[alias]
语法:alias path
配置段:location
区别在于nginx如何解释location后面的uri
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
root实例:
1
2
3
|
location /ying/ {
root /www/root/html/;
}
|
如果一个请求的URI是/ying/a.html时,web服务器将会返回服务器上的/www/root/html/ying/a.html的文件。
alias实例:
1
2
3
|
location /ying/ {
alias /www/root/html/new_t/;
}
|
如果一个请求的URI是/ying/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。
总结
遇到自己不熟悉的技术点多次折腾无果后应该及时查阅配置,这样解决问题效率会更高一些。