Http 为什么nginx中的默认配置需要location和try_files指令?
我正在Debian9.0上运行nginx1.10.3 这就是我在Http 为什么nginx中的默认配置需要location和try_files指令?,http,nginx,configuration,Http,Nginx,Configuration,我正在Debian9.0上运行nginx1.10.3 这就是我在/etc/nginx/sites enabled/default中的默认nginx配置的外观(为了清晰起见,删除了大部分注释) “我的文档根目录”仅包含一个默认主页文件 $ ls -l /var/www/html/ total 4 -rw-r--r-- 1 root root 867 Jun 13 02:34 index.nginx-debian.html 通过此设置,我可以得到预期的响应,即加载主页 成功访问并尝试访问任何其他内
/etc/nginx/sites enabled/default
中的默认nginx配置的外观(为了清晰起见,删除了大部分注释)
“我的文档根目录”仅包含一个默认主页文件
$ ls -l /var/www/html/
total 4
-rw-r--r-- 1 root root 867 Jun 13 02:34 index.nginx-debian.html
通过此设置,我可以得到预期的响应,即加载主页
成功访问并尝试访问任何其他内容将导致HTTP404
$ sudo systemctl reload nginx
$ curl -I http://localhost/
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:47:25 GMT
Content-Type: text/html
Content-Length: 867
Last-Modified: Mon, 12 Jun 2017 21:04:50 GMT
Connection: keep-alive
ETag: "593f01f2-363"
Accept-Ranges: bytes
$ curl -I http://localhost/foo
HTTP/1.1 404 Not Found
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:47:31 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
$ curl -I http://localhost/foo/
HTTP/1.1 404 Not Found
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:47:32 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
现在,我从默认配置中删除位置配置。
现在我的配置看起来像这样,没有位置配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
通过这种设置,我仍然得到类似的响应
$ sudo systemctl reload nginx
$ curl -I http://localhost/
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:54:44 GMT
Content-Type: text/html
Content-Length: 867
Last-Modified: Mon, 12 Jun 2017 21:04:50 GMT
Connection: keep-alive
ETag: "593f01f2-363"
Accept-Ranges: bytes
$ curl -I http://localhost/foo
HTTP/1.1 404 Not Found
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:54:45 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
$ curl -I http://localhost/foo/
HTTP/1.1 404 Not Found
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:54:47 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
我的问题是:
location
和try\u files
指令有什么用处位置
和try\u文件
指令的配置与没有这两个指令的配置会产生不同的结果默认情况下,
try\u files
语句的第二个参数未实现。但除此之外,这只是一个模板。如果需要,还可以删除索引
和服务器名称
指令,因为它们也有合理的默认值。@RichardSmithtry\u files
的第二个参数是什么意思?$uri/
参数?如果默认情况下没有实现,那么为什么第二个示例中的输出与第一个示例中的输出类似?它测试是否存在目录,因此名为foo
的目录需要存在。
$ sudo systemctl reload nginx
$ curl -I http://localhost/
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:54:44 GMT
Content-Type: text/html
Content-Length: 867
Last-Modified: Mon, 12 Jun 2017 21:04:50 GMT
Connection: keep-alive
ETag: "593f01f2-363"
Accept-Ranges: bytes
$ curl -I http://localhost/foo
HTTP/1.1 404 Not Found
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:54:45 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
$ curl -I http://localhost/foo/
HTTP/1.1 404 Not Found
Server: nginx/1.10.3
Date: Thu, 13 Jul 2017 04:54:47 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive