server { listen 80;
server_name parent.weilaicheng.com;
root /data/parent_server/current/public;
......
location /frontend {
index index.html;
alias /data/uni-kpl-wechat/tdist/;
}
location /parent-wechat {
proxy_pass http://127.0.0.1:9525;
}
}
location中的root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
root实例:
location ^~ /frontend/ {
root /data/uni-kpl-wechat/tdist/;
}
如果一个请求的URI是/frontend/a.html时,web服务器将会返回服务器上的/data/uni-kpl-wechat/tdist/frontend/a.html的文件。
alias的处理结果是:使用alias路径替换location路径
alias实例:
location ^~ /frontend/ {
alias /data/uni-kpl-wechat/tdist/;
}
如果一个请求的URI是/frontend/a.html时,web服务器将会返回服务器上的/data/uni-kpl-wechat/tdist/a.html的文件。注意alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无。