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

本文翻译自:Nginx — static file serving confusion with root & alias

I need to serve my app through my app server at 8080 , and my static files from a directory without touching the app server. 我需要通过我的应用程序服务器8080提供我的应用程序,以及来自目录的静态文件,而无需触及应用程序服务器。 The nginx config I have is something like this... 我拥有的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;
            }
    }

Now, with this config, everything is working fine. 现在,使用此配置,一切正常。 Note that the root directive is commented out. 请注意, root指令已注释掉。

If I activate root and deactivate the alias -- it stops working. 如果我激活root并停用alias - 它将停止工作。 However, when I remove the trailing /static/ from the root it starts working again. 但是,当我从root删除尾随/static/它再次开始工作。

Can someone explain what's going on. 有人可以解释发生了什么。 Also please explain clearly and verbosely what are the differences between root and alias , and their purposes. 还请详细解释rootalias之间的区别及其用途。


#1楼

参考:https://stackoom.com/question/ibqn/Nginx-静态文件与root和别名混淆


#2楼

I have found answers to my confusions. 我找到了我的困惑的答案。

There is a very important difference between the root and the alias directives. rootalias指令之间有一个非常重要的区别。 This difference exists in the way the path specified in the root or the alias is processed. 这种差异存在于处理rootalias指定的路径的方式中。

In case of the root directive, full path is appended to the root including the location part , whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias . root指令的情况下,将完整路径附加到根, 包括位置部分 ,而在alias指令的情况下, 只有路径中不包括位置部分的部分被附加到别名

To illustrate: 为了显示:

Let's say we have the config 假设我们有配置

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

In this case the final path that Nginx will derive will be 在这种情况下,Nginx将推导出的最终路径

/var/www/app/static/static

This is going to return 404 since there is no static/ within static/ 这将返回404因为没有static/ static/

This is because the location part is appended to the path specified in the root . 这是因为位置部分附加到root指定的路径。 Hence, with root , the correct way is 因此,使用root ,正确的方法是

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

On the other hand, with alias , the location part gets dropped . 另一方面,使用alias ,位置部分会被删除 So for the config 所以对于配置

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

the final path will correctly be formed as 最终路径将正确形成为

/var/www/app/static

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


#3楼

Just a quick addendum to @good_computer's very helpful answer, I wanted to replace to root of the URL with a folder, but only if it matched a subfolder containing static files (which I wanted to retain as part of the path). 只是@ good_computer非常有用的答案的快速附录,我想用文件夹替换URL的根,但只有当它匹配包含静态文件的子文件夹(我想保留作为路径的一部分)。

For example if file requested is in /app/js or /app/css , look in /app/location/public/[that folder] . 例如,如果请求的文件位于/app/js/app/css ,请查看/app/location/public/[that folder]

I got this to work using a regex. 我使用正则表达式来使用它。

 location ~ ^/app/((images/|stylesheets/|javascripts/).*)$ {
     alias /home/user/sites/app/public/$1;
     access_log off;
     expires max;
 }

#4楼

In your case, you can use root directive, because $uri part of the location directive is the same with last root directive part. 在您的情况下,您可以使用root指令,因为location指令的$uri部分与最后一个root指令部分相同。

Nginx documentation advices it as well: Nginx文档也提供了建议:
When location matches the last part of the directive's value: 当location匹配指令值的最后一部分时:

 location /images/ { alias /data/w3/images/; } 

it is better to use the root directive instead: 最好使用root指令:

 location /images/ { root /data/w3; } 

and root directive will append $uri to the path. root指令会将$uri附加到路径中。


#5楼

as say as @treecoder 就像@treecoder一样

In case of the root directive, full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias. root指令的情况下,将完整路径附加到根,包括位置部分,而在alias指令的情况下,只有路径中不包括位置部分的部分被附加到别名。

A picture is worth a thousand words 一张图片胜过千言万语

for root : 对于root

在此输入图像描述

for alias : alias

在此输入图像描述


#6楼

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/;
    }
}

Server block to live the static page on nginx. 服务器块在nginx上生存静态页面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值