Nginx 为什么try_files指令在根和文件名之间缺少斜杠?

Nginx 为什么try_files指令在根和文件名之间缺少斜杠?,nginx,nginx-location,Nginx,Nginx Location,我试图将Nginx设置为反向代理,但也可以自己处理单个静态页面(问候语): root /usr/share/project/; location = / { try_files index.html =404; } 此配置始终返回404。当我试图弄清楚到底发生了什么时,我重写了try_files指令以使其失败: try_files index.html index.html; 并对我在error.log中看到的内容感到惊讶: 2019/05/07 17:30:39[错误]9393#9

我试图将Nginx设置为反向代理,但也可以自己处理单个静态页面(问候语):

root /usr/share/project/;
location = / {
    try_files index.html =404;
}
此配置始终返回404。当我试图弄清楚到底发生了什么时,我重写了try_files指令以使其失败:

try_files index.html index.html;
并对我在error.log中看到的内容感到惊讶:

2019/05/07 17:30:39[错误]9393#9393:*1 open()“/usr/share/projectindex.html”失败(2:没有这样的文件或目录),客户端:10.25.88.214,服务器:,请求:“GET/index.html HTTP/1.1”

如您所见,结果文件名为projectindex.html。斯莱什被错过了。我试图在不同的地方添加/和/但没有任何结果

最后,我用以下内容替换配置:

root /usr/share/project/;
location = / {
    try_files /index.html =404;
}
location = /index.html {
}
它是有效的

我不明白第一个配置有什么问题。我也不理解空位置的含义:

location = /index.html {
}
以及为什么它能正常工作


也许有更好的方法可以做到这一点?

root
可以选择使用尾随的
/
-这不重要,它会被忽略

try\u files
语句的文件元素(与Nginx中的所有uri一样)需要一个前导的
/
。有关详细信息,请参阅

例如:

root /usr/share/project;
location = / {
    try_files /index.html =404;
}
location / {
    proxy_pass ...;
}
root /usr/share/project;
location = / {
    index index.html;
}
location = /index.html {
}
location / {
    proxy_pass ...;
}
这是因为URI在内部被重写为
/index.html
,并在相同的位置进行处理


如果使用
index
指令,URI将在内部重写为
/index.html
,Nginx将搜索匹配的位置以处理请求。在这种情况下,您需要另一个位置来处理请求

例如:

root /usr/share/project;
location = / {
    try_files /index.html =404;
}
location / {
    proxy_pass ...;
}
root /usr/share/project;
location = / {
    index index.html;
}
location = /index.html {
}
location / {
    proxy_pass ...;
}
空位置块从外部块继承
root
的值。
index
语句无论如何都是默认值,因此严格来说,您也不需要指定该语句。请注意,
索引
指令的值不需要前导的
/
。有关详细信息,请参阅