为什么 nginx "location = /" 规则不起作用?
Why is nginx "location = /" rule not work?
我想使用 'location = /' 规则为“/”请求定义一个完全匹配,并指定 'index.html' 作为该请求的响应。但为什么我的设置不起作用?
我定义了两个位置如下(已更新:我也在底部post我的nginx.conf的全部内容。):
location = / {
root /opt/www/static/;
index index.html;
}
location / {
root /opt/www/resource/;
}
而我的“/opt/www”目录中的文件如下。 (# The content is:
后面的注释描述了其中的文件内容。)
/opt/www
├── resource
│ ├── hello.html # The content is: The hello.html from /resource
│ └── index.html # The content is: The index.html from /resource
└── static
└── index.html # The content is: The index.html from /static
但是当我访问以下网址时,输出是:
- http://localhost or http://localhost/ - 响应是:
The index.html from /resource
.
- http://localhost/index.html - 响应为:
The index.html from /resource
.
- http://localhost/hello.html - 响应是:
The hello.html from /resource
.
我认为#2 和#3 的结果是正确的,但是对于#1,为什么它 returns 而 resource/index.html
作为响应而不是 static/index.html
?因为我认为,根据 location 的定义,响应应该来自 static/index.html
文件。
using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison.
另一个问题是,如何更改我的 conf 文件以使用精确匹配将 static/index.html
指定为 http://localhost or http://localhost/ 的响应?
已更新
我在使用 error_log logs/error.log debug;
打开 nginx 调试日志后找到了窍门。根据日志,对于#1,请求与第一条规则 location = /
完全匹配,并且 /opt/www/static/index.html
已打开。但是后来,请求被内部重定向到/index.html
,然后匹配第二个规则,结果使用了/opt/www/resource/index.html
。
但我的问题是,为什么在确切规则(第一个)已经匹配并且找到 /opt/www/static/index.html
时将请求重定向到 /index.html
?我可以通过一些配置或其他指令停止内部重定向吗?
nginx的日志是(我的nginx版本是1.4.6):
2015/05/01 11:59:46 [debug] 112241#0: *1 http process request line
2015/05/01 11:59:46 [debug] 112241#0: *1 http request line: "GET / HTTP/1.1"
2015/05/01 11:59:46 [debug] 112241#0: *1 http uri: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http args: ""
2015/05/01 11:59:46 [debug] 112241#0: *1 http exten: ""
*** omit some logs to process request header and others ***
2015/05/01 11:59:46 [debug] 112241#0: *1 event timer del: 3: 1430452846698
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 0
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 1
2015/05/01 11:59:46 [debug] 112241#0: *1 test location: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 using configuration "=/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http cl:-1 max:1048576
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 3
2015/05/01 11:59:46 [debug] 112241#0: *1 post rewrite phase: 4
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 5
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 6
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 7
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 8
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 9
2015/05/01 11:59:46 [debug] 112241#0: *1 post access phase: 10
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 11
2015/05/01 11:59:46 [debug] 112241#0: *1 open index "/opt/www/static/index.html"
2015/05/01 11:59:46 [debug] 112241#0: *1 internal redirect: "/index.html?"
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 1
2015/05/01 11:59:46 [debug] 112241#0: *1 test location: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 using configuration "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http cl:-1 max:1048576
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 3
2015/05/01 11:59:46 [debug] 112241#0: *1 post rewrite phase: 4
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 5
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 6
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 7
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 8
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 9
2015/05/01 11:59:46 [debug] 112241#0: *1 post access phase: 10
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 11
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 12
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 13
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 14
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 15
2015/05/01 11:59:46 [debug] 112241#0: *1 http filename: "/opt/www/resource/index.html"
再次更新到post我nginx.conf.
的全部内容
worker_processes 1;
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
location = / {
root /opt/www/static/;
index index.html;
}
location / {
root /opt/www/resource/;
}
}
}
经过一些研究 google,我找到了自己解决这个问题的方法 :-)。使用 try_files
指令替换 index
指令,当找到 static/index.html
时它不会触发内部重定向。最终定义为:
location = / {
root /opt/www/static/;
#index index.html;
try_files /index.html =404;
}
location / {
root /opt/www/resource/;
}
我想使用 'location = /' 规则为“/”请求定义一个完全匹配,并指定 'index.html' 作为该请求的响应。但为什么我的设置不起作用?
我定义了两个位置如下(已更新:我也在底部post我的nginx.conf的全部内容。):
location = / {
root /opt/www/static/;
index index.html;
}
location / {
root /opt/www/resource/;
}
而我的“/opt/www”目录中的文件如下。 (# The content is:
后面的注释描述了其中的文件内容。)
/opt/www
├── resource
│ ├── hello.html # The content is: The hello.html from /resource
│ └── index.html # The content is: The index.html from /resource
└── static
└── index.html # The content is: The index.html from /static
但是当我访问以下网址时,输出是:
- http://localhost or http://localhost/ - 响应是:
The index.html from /resource
. - http://localhost/index.html - 响应为:
The index.html from /resource
. - http://localhost/hello.html - 响应是:
The hello.html from /resource
.
我认为#2 和#3 的结果是正确的,但是对于#1,为什么它 returns 而 resource/index.html
作为响应而不是 static/index.html
?因为我认为,根据 location 的定义,响应应该来自 static/index.html
文件。
using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison.
另一个问题是,如何更改我的 conf 文件以使用精确匹配将 static/index.html
指定为 http://localhost or http://localhost/ 的响应?
已更新
我在使用 error_log logs/error.log debug;
打开 nginx 调试日志后找到了窍门。根据日志,对于#1,请求与第一条规则 location = /
完全匹配,并且 /opt/www/static/index.html
已打开。但是后来,请求被内部重定向到/index.html
,然后匹配第二个规则,结果使用了/opt/www/resource/index.html
。
但我的问题是,为什么在确切规则(第一个)已经匹配并且找到 /opt/www/static/index.html
时将请求重定向到 /index.html
?我可以通过一些配置或其他指令停止内部重定向吗?
nginx的日志是(我的nginx版本是1.4.6):
2015/05/01 11:59:46 [debug] 112241#0: *1 http process request line
2015/05/01 11:59:46 [debug] 112241#0: *1 http request line: "GET / HTTP/1.1"
2015/05/01 11:59:46 [debug] 112241#0: *1 http uri: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http args: ""
2015/05/01 11:59:46 [debug] 112241#0: *1 http exten: ""
*** omit some logs to process request header and others ***
2015/05/01 11:59:46 [debug] 112241#0: *1 event timer del: 3: 1430452846698
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 0
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 1
2015/05/01 11:59:46 [debug] 112241#0: *1 test location: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 using configuration "=/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http cl:-1 max:1048576
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 3
2015/05/01 11:59:46 [debug] 112241#0: *1 post rewrite phase: 4
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 5
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 6
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 7
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 8
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 9
2015/05/01 11:59:46 [debug] 112241#0: *1 post access phase: 10
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 11
2015/05/01 11:59:46 [debug] 112241#0: *1 open index "/opt/www/static/index.html"
2015/05/01 11:59:46 [debug] 112241#0: *1 internal redirect: "/index.html?"
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 1
2015/05/01 11:59:46 [debug] 112241#0: *1 test location: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 using configuration "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http cl:-1 max:1048576
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 3
2015/05/01 11:59:46 [debug] 112241#0: *1 post rewrite phase: 4
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 5
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 6
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 7
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 8
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 9
2015/05/01 11:59:46 [debug] 112241#0: *1 post access phase: 10
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 11
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 12
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 13
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 14
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 15
2015/05/01 11:59:46 [debug] 112241#0: *1 http filename: "/opt/www/resource/index.html"
再次更新到post我nginx.conf.
的全部内容worker_processes 1;
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
location = / {
root /opt/www/static/;
index index.html;
}
location / {
root /opt/www/resource/;
}
}
}
经过一些研究 google,我找到了自己解决这个问题的方法 :-)。使用 try_files
指令替换 index
指令,当找到 static/index.html
时它不会触发内部重定向。最终定义为:
location = / {
root /opt/www/static/;
#index index.html;
try_files /index.html =404;
}
location / {
root /opt/www/resource/;
}