Nginx-静态文件服务与root&别名 [英] Nginx -- static file serving confusion with root & alias
问题描述
我需要通过位于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.
有人可以解释发生了什么事吗?另外,请清楚,详细地解释root
和alias
之间的区别以及它们的用途.
Can someone explain what's going on. Also please explain clearly and verbosely what are the differences between root
and alias
, and their purposes.
推荐答案
我找到了解决困惑的方法.
I have found answers to my confusions.
root
和alias
指令之间有一个非常重要的区别.这种差异存在于root
或alias
中指定的路径的处理方式上.
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/
中没有static/
This is going to return 404
since there is no static/
within 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
最终路径将正确地形成为
the final path will correctly be formed as
/var/www/app/static
alias
指令后跟斜杠的情况
对于 Nginx文档,没有明确的准则来说明是否必须使用斜杠.人们在这里和其他地方的普遍观察似乎表明确实如此.
The case of trailing slash for alias
directive
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/375602/why-is -my-nginx-alias-not-working
这篇关于Nginx-静态文件服务与root&别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!