位置内的 Nginx root 指令似乎不起作用
Nginx root directive inside location doesn't seem to be working
nginx 版本:nginx/1.10.3 (Ubuntu)
使用 OpenSSL 1.0.2g 构建 2016 年 3 月 1 日
启用 TLS SNI 支持
通过 apt-get 安装
使用 html5 样板配置项目作为基础
我一直在尝试做一个简单的任务,但它并没有像我想象的那样工作。我的目录结构如下:
/var/www$ tree
.
├── docroot
│ └── files
│ ├── booger.txt
│ └── favicon.ico
└── static
├── h5bp
│ ├── humans.txt
│ ├── js
│ │ ├── main.js
│ │ ├── plugins.js
│ │ └── vendor
│ │ ├── jquery-1.12.0.min.js
│ │ └── modernizr-2.8.3.min.js
│ ├── tile.png
│ └── tile-wide.png
我认为最好的方法是拥有 docroot/files 的服务器根目录(真正的文档根目录),然后重新定义静态文件的根目录。这是我在 /etc/nginx/sites-available 中的默认配置:
# Choose between www and non-www, listen on the *wrong* one and redirect to
# the right one -- http://wiki.nginx.org/Pitfalls#Server_Name
#
server {
listen [::]:80 default_server;
listen 80 default_server;
# listen on all hosts
server_name _;
# and redirect to the https host (declared below)
# avoiding http://www -> https://www -> https:// chain.
return 301 https://www.ubercode.io$request_uri;
}
server {
# listen [::]:443 ssl http2 accept_filter=dataready; # for FreeBSD
# listen 443 ssl http2 accept_filter=dataready; # for FreeBSD
listen [::]:443 ssl http2 deferred default_server; # for Linux
listen 443 ssl http2 deferred default_server; # for Linux
# listen [::]:443 ssl http2;
# listen 443 ssl http2;
# The host name to respond to
server_name www.ubercode.io;
include h5bp/directive-only/ssl.conf;
# root /var/www/docroot/files;
root /var/www;
index index.html index.htm index.nginx-debian.html;
# return forbidden for any php, asp, jsp, our .dt templates, or myadmin requests
# location ~ (\.dt$|myadmin|\.php$|\.jsp$|\.asmx$|\.asp$) {
# deny all;
# }
# location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
# expires 1d;
# }
# location ~* \.(pdf)$ {
# expires 5d;
# }
# static
location /static/ {
# root /var/www;
autoindex on;
# expires 1d;
}
location / {
alias /var/www/docroot/files;
autoindex on;
}
#Specify a charset
charset utf-8;
# Custom 404 page
error_page 404 /404.html;
# Include the basic h5bp config set
include h5bp/basic.conf;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
# listen on the wrong host
server_name _;
include h5bp/directive-only/ssl.conf;
# and redirect to the non-www host (declared below)
return 301 https://www.ubercode.io$request_uri;
}
如果我像显示的那样使用它,静态文件可以工作,但根目录下的别名似乎不起作用,因为它们会抛出 404。如果我在 / 位置和 /var/www server root 然后取消注释服务器 returns docroot/files assets 但没有静态文件的其他行。位置块内的覆盖根似乎不起作用。任何人都可以解释发生了什么,并可能给我一些解决方法吗?
If I use it like it is shown, the static files work but the alias at
the root does not seem to work as they throw a 404.
您不应在 location /
语句中使用 alias
指令。请改用具有相同值的 root
语句。
If I comment the alias under the / location and the /var/www server
root then uncomment the other lines the server returns docroot/files
assets but no static files.
如果启用 location ~* \.(jpg|jpeg|png|gif|ico|css|js)$
,那么它将覆盖 location /static/
块,并且在后者中为 root
设置的任何值都将被忽略。此外,它将覆盖以 .ico
结尾的 URI 的 location /
块,因此 /favicon.ico
将不再有效,因为它来自不同的文档根目录。
请参阅 how nginx
processes a request 并注意正则表达式位置优先于前缀位置。
如果您需要微调 expires
值,则不能在不考虑它们影响哪个文档根的情况下添加 location
块。
请参阅 了解另一种方法,使用 map
指令根据 $request_uri
.
设置 expires
值
nginx 版本:nginx/1.10.3 (Ubuntu) 使用 OpenSSL 1.0.2g 构建 2016 年 3 月 1 日 启用 TLS SNI 支持 通过 apt-get 安装 使用 html5 样板配置项目作为基础
我一直在尝试做一个简单的任务,但它并没有像我想象的那样工作。我的目录结构如下:
/var/www$ tree
.
├── docroot
│ └── files
│ ├── booger.txt
│ └── favicon.ico
└── static
├── h5bp
│ ├── humans.txt
│ ├── js
│ │ ├── main.js
│ │ ├── plugins.js
│ │ └── vendor
│ │ ├── jquery-1.12.0.min.js
│ │ └── modernizr-2.8.3.min.js
│ ├── tile.png
│ └── tile-wide.png
我认为最好的方法是拥有 docroot/files 的服务器根目录(真正的文档根目录),然后重新定义静态文件的根目录。这是我在 /etc/nginx/sites-available 中的默认配置:
# Choose between www and non-www, listen on the *wrong* one and redirect to
# the right one -- http://wiki.nginx.org/Pitfalls#Server_Name
#
server {
listen [::]:80 default_server;
listen 80 default_server;
# listen on all hosts
server_name _;
# and redirect to the https host (declared below)
# avoiding http://www -> https://www -> https:// chain.
return 301 https://www.ubercode.io$request_uri;
}
server {
# listen [::]:443 ssl http2 accept_filter=dataready; # for FreeBSD
# listen 443 ssl http2 accept_filter=dataready; # for FreeBSD
listen [::]:443 ssl http2 deferred default_server; # for Linux
listen 443 ssl http2 deferred default_server; # for Linux
# listen [::]:443 ssl http2;
# listen 443 ssl http2;
# The host name to respond to
server_name www.ubercode.io;
include h5bp/directive-only/ssl.conf;
# root /var/www/docroot/files;
root /var/www;
index index.html index.htm index.nginx-debian.html;
# return forbidden for any php, asp, jsp, our .dt templates, or myadmin requests
# location ~ (\.dt$|myadmin|\.php$|\.jsp$|\.asmx$|\.asp$) {
# deny all;
# }
# location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
# expires 1d;
# }
# location ~* \.(pdf)$ {
# expires 5d;
# }
# static
location /static/ {
# root /var/www;
autoindex on;
# expires 1d;
}
location / {
alias /var/www/docroot/files;
autoindex on;
}
#Specify a charset
charset utf-8;
# Custom 404 page
error_page 404 /404.html;
# Include the basic h5bp config set
include h5bp/basic.conf;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
# listen on the wrong host
server_name _;
include h5bp/directive-only/ssl.conf;
# and redirect to the non-www host (declared below)
return 301 https://www.ubercode.io$request_uri;
}
如果我像显示的那样使用它,静态文件可以工作,但根目录下的别名似乎不起作用,因为它们会抛出 404。如果我在 / 位置和 /var/www server root 然后取消注释服务器 returns docroot/files assets 但没有静态文件的其他行。位置块内的覆盖根似乎不起作用。任何人都可以解释发生了什么,并可能给我一些解决方法吗?
If I use it like it is shown, the static files work but the alias at the root does not seem to work as they throw a 404.
您不应在 location /
语句中使用 alias
指令。请改用具有相同值的 root
语句。
If I comment the alias under the / location and the /var/www server root then uncomment the other lines the server returns docroot/files assets but no static files.
如果启用 location ~* \.(jpg|jpeg|png|gif|ico|css|js)$
,那么它将覆盖 location /static/
块,并且在后者中为 root
设置的任何值都将被忽略。此外,它将覆盖以 .ico
结尾的 URI 的 location /
块,因此 /favicon.ico
将不再有效,因为它来自不同的文档根目录。
请参阅 how nginx
processes a request 并注意正则表达式位置优先于前缀位置。
如果您需要微调 expires
值,则不能在不考虑它们影响哪个文档根的情况下添加 location
块。
请参阅 map
指令根据 $request_uri
.
expires
值