首页 文章

Nginx - 静态文件与root和别名混淆

提问于
浏览
340

我需要通过我的应用程序服务器 8080 提供我的应用程序,以及来自目录的静态文件,而无需触及应用程序服务器 . 我拥有的nginx配置是这样的......

# app server on port 8080
    # nginx listens on port 8123
    server {
            listen          8123;
            access_log      off;

            location /static/ {
                    # root /var/www/app/static/;
                    alias /var/www/app/static/;
                    autoindex off;
            }


            location / {
                    proxy_pass              http://127.0.0.1:8080;
                    proxy_set_header        Host             $host;
                    proxy_set_header        X-Real-IP        $remote_addr;
                    proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;
            }
    }

现在,使用此配置,一切正常 . 请注意, root 指令已注释掉 .

如果我激活 root 并停用 alias - 它将停止工作 . 但是,当我从 root 删除尾随 /static/ 时,它再次开始工作 .

有人可以解释发生了什么 . 另外请详细解释 rootalias 之间的区别及其目的 .

5 回答

  • 63

    我找到了我的困惑的答案 .

    rootalias 指令之间存在非常重要的区别 . 这种差异存在于处理 rootalias 中指定的路径的方式中 .

    如果是 root 指令,则为 full path is appended to the root including the location part ,而对于 alias 指令,则为 only the portion of the path NOT including the location part is appended to the alias .

    为了显示:

    假设我们有配置

    location /static/ {
        root /var/www/app/static/;
        autoindex off;
    }
    

    在这种情况下,Nginx将推导出的最终路径

    /var/www/app/static/static
    

    这将返回 404 ,因为 static/ 内没有 static/

    这是因为位置部分附加到 root 中指定的路径 . 因此,使用 root ,正确的方法是

    location /static/ {
        root /var/www/app/;
        autoindex off;
    }
    

    另一方面,使用 alias ,位置部分获得 dropped . 所以对于配置

    location /static/ {
        alias /var/www/app/static/;
        autoindex off;
    }
    

    最终路径将正确形成为

    /var/www/app/static
    

    请参阅此处的文档:http://wiki.nginx.org/HttpCoreModule#alias

  • 833

    就像@treecoder一样

    如果是root指令,则将完整路径附加到根,包括位置部分,而在alias指令的情况下,只将路径中不包括位置部分的部分附加到别名 .

    一张图片胜过千言万语

    对于 root

    enter image description here

    对于 alias

    enter image description here

  • 28

    在您的情况下,您可以使用 root 指令,因为 location 指令的 $uri 部分与最后 root 指令部分相同 .

    Nginx文档也提供了它:当location匹配指令值的最后一部分时:location / images / {
    别名/ data / w3 / images /;
    }
    最好使用root指令:location / images / {
    root / data / w3;
    }

    root 指令将 $uri 附加到路径 .

  • 15

    只是@ good_computer非常有用的答案的快速附录,我想用文件夹替换URL的根,但只有当它匹配包含静态文件的子文件夹(我想保留作为路径的一部分) .

    例如,如果请求的文件位于 /app/js/app/css 中,请查看 /app/location/public/[that folder] .

    我使用正则表达式来实现这一点 .

    location ~ ^/app/((images/|stylesheets/|javascripts/).*)$ {
         alias /home/user/sites/app/public/$1;
         access_log off;
         expires max;
     }
    
  • 1
    server {
        server_name xyz.com;
        root /home/ubuntu/project_folder/;
    
        client_max_body_size 10M;
        access_log  /var/log/nginx/project.access.log;
        error_log  /var/log/nginx/project.error.log;
    
        location /static {
            index index.html;
        }
    
        location /media {
            alias /home/ubuntu/project/media/;
        }
    }
    

    服务器块在nginx上存在静态页面 .

相关问题