1. server { listen 80;
    2. server_name parent.weilaicheng.com;
    3. root /data/parent_server/current/public;
    4. ......
    5. location /frontend {
    6. index index.html;
    7. alias /data/uni-kpl-wechat/tdist/;
    8. }
    9. location /parent-wechat {
    10. proxy_pass http://127.0.0.1:9525;
    11. }
    12. }

    location中的root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。

    root的处理结果是:root路径+location路径

    1. root实例:
    2. location ^~ /frontend/ {
    3. root /data/uni-kpl-wechat/tdist/;
    4. }
    5. 如果一个请求的URI是/frontend/a.html时,web服务器将会返回服务器上的/data/uni-kpl-wechat/tdist/frontend/a.html的文件。

    alias的处理结果是:使用alias路径替换location路径

    1. alias实例:
    2. location ^~ /frontend/ {
    3. alias /data/uni-kpl-wechat/tdist/;
    4. }
    5. 如果一个请求的URI是/frontend/a.html时,web服务器将会返回服务器上的/data/uni-kpl-wechat/tdist/a.html的文件。注意alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。

    alias是一个目录别名的定义,root则是最上层目录的定义。
    还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无。