Nginx -- 静态文件服务与 root & 混淆别名 [英] Nginx -- static file serving confusion with root & alias

查看:23
本文介绍了Nginx -- 静态文件服务与 root & 混淆别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过位于 8080 的应用服务器为我的应用提供服务,并在不接触应用服务器的情况下从目录中提供我的静态文件.我的 nginx 配置是这样的......

I need to serve my app through my app server at 8080, and my static files from a directory without touching the app server. The nginx config I have is something like this...

    # 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 指令已被注释掉.

Now, with this config, everything is working fine. Note that the root directive is commented out.

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

If I activate root and deactivate the alias -- it stops working. However, when I remove the trailing /static/ from the root it starts working again.

谁能解释一下这是怎么回事.还请清楚详细地解释rootalias 之间的区别,以及它们的用途.

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 指令之间有一个非常重要的区别.这种差异存在于 rootalias 中指定的路径的处理方式中.

There is a very important difference between the root and the alias directives. This difference exists in the way the path specified in the root or the alias is processed.

对于 root 指令,完整路径被附加到根目录包括位置部分,而对于 >alias 指令,只有路径的一部分不包括位置部分被附加到别名.

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.

举例说明:

假设我们有配置

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

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

In this case the final path that Nginx will derive will be

/var/www/app/static/static

这将返回 404 因为在 static/

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

This is because the location part is appended to the path specified in the root. Hence, with root, the correct way is

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

另一方面,使用alias,位置部分被删除.所以对于配置

On the other hand, with alias, the location part gets dropped. So for the config

location /static/ {
    alias /var/www/app/static/;
    autoindex off;           ↑
}                            |
                             pay attention to this trailing slash

最终路径将正确形成

/var/www/app/static

从某种意义上说,这是有道理的.alias 只是让您定义一个新路径来表示现有的真实"路径.小路.位置部分是新路径,因此它会被替换为真实路径.把它想象成一个符号链接.

In a way this makes sense. The alias just let's you define a new path to represent an existing "real" path. The location part is that new path, and so it gets replaced with the real path. Think of it as a symlink.

另一方面,Root 不是一个新路径,它包含一些信息,必须与其他一些信息进行整理才能形成最终路径.因此,位置部分被使用,而不是被删除.

Root, on the other hand is not a new path, it contains some information that has to be collated with some other info to make the final path. And so, the location part is used, not dropped.

对于每个Nginx文档,但这里和其他地方的人们的共同观察似乎表明确实如此.

There is no definitive guideline about whether a trailing slash is mandatory per Nginx documentation, but a common observation by people here and elsewhere seems to indicate that it is.

还有几个地方对此进行了讨论,但尚未得出结论.

A few more places have discussed this, not conclusively though.

https://serverfault.com/questions/376162/how-can-i-create-a-location-in-nginx-that-works-with-and-without-a-trailing-slas

https://serverfault.com/questions/375602/why-is-my-nginx-alias-not-working

这篇关于Nginx -- 静态文件服务与 root & 混淆别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆