我怎样才能使这个 try_files 指令工作? [英] How can I make this try_files directive work?
问题描述
我想了解 Nginx 的 try_files 指令是如何工作的.Nginx 在我的服务器上运行并提供位于 /usr/share/nginx/html/index.html
的默认页面.
I am trying to understand how Nginx's try_files directive works. Nginx is running on my server and serving up the default page located at /usr/share/nginx/html/index.html
.
但是,我在 /var/www/test/index.html
的文件系统中有一个简单的 HTML 页面.以下配置文件不会导致该文件得到服务.我想了解为什么不这样做,以及我需要进行哪些更改才能提供服务.
However, I have a simple HTML page located in the filesystem at /var/www/test/index.html
. The following config file is not causing that file to get served. I'd like to understand why not, and what change I need to make so that it will be served.
这是default.conf
的相关部分:
server {
listen 80;
server_name localhost;
root /var/www;
try_files /test/index.html /;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
推荐答案
一个非常常见的 try_files 行,可以应用于您的条件是
a very common try_files line which can be applied on your condition is
location / {
try_files $uri $uri/ /test/index.html;
}
你可能理解第一部分,location/
匹配所有位置,除非它匹配更具体的位置,例如 location/test
you probably understand the first part, location /
matches all locations, unless it's matched by a more specific location, like location /test
for example
第二部分( try_files
)意味着当您收到与此块匹配的 URI 时,首先尝试 $uri
,例如 http://example.com/images/image.jpg
nginx 将尝试检查 /images
中是否有名为 image.jpg
的文件,如果找到它会首先提供它.
The second part ( the try_files
) means when you receive a URI that's matched by this block try $uri
first, for example http://example.com/images/image.jpg
nginx will try to check if there's a file inside /images
called image.jpg
if found it will serve it first.
第二个条件是 $uri/
这意味着如果您没有找到第一个条件 $uri
尝试将 URI 作为目录,例如 http://example.com/images/
, ngixn 将首先检查名为 images
的文件是否存在,然后它不会找到它,然后进行第二次检查 $uri/
并查看是否存在名为 images
的目录,然后它会尝试为它提供服务.
Second condition is $uri/
which means if you didn't find the first condition $uri
try the URI as a directory, for example http://example.com/images/
, ngixn will first check if a file called images
exists then it wont find it, then goes to second check $uri/
and see if there's a directory called images
exists then it will try serving it.
旁注:如果您没有autoindex on
,您可能会收到 403 禁止错误,因为默认情况下禁止目录列表.
Side note: if you don't have autoindex on
you'll probably get a 403 forbidden error, because directory listing is forbidden by default.
EDIT:我忘了说,如果你定义了 index
,nginx 会在之前尝试检查该文件夹中是否存在索引尝试目录列表.
EDIT: I forgot to mention that if you have
index
defined, nginx will try to check if the index exists inside this folder before trying directory listing.
第三个条件 /test/index.html
被认为是一个回退选项,(你需要使用至少 2 个选项,一个和一个回退),你可以尽可能多地使用(以前从未读过压缩),nginx 将在文件夹 test
中查找文件 index.html
并在它存在时提供它.
Third condition /test/index.html
is considered a fall back option, (you need to use at least 2 options, one and a fall back), you can use as much as you can (never read of a constriction before), nginx will look for the file index.html
inside the folder test
and serve it if it exists.
如果第三个条件也失败,那么nginx将提供404错误页面.
If the third condition fails too, then nginx will serve the 404 error page.
还有一种叫做命名位置的东西,就像这样
Also there's something called named locations, like this
location @error {
}
你可以像这样用 try_files
调用它
You can call it with try_files
like this
try_files $uri $uri/ @error;
提示:如果您只想提供 1 个条件,例如在文件夹 images
中,您只想提供图像或转到 404 错误,你可以这样写一行
TIP: If you only have 1 condition you want to serve, like for example inside folder images
you only want to either serve the image or go to 404 error, you can write a line like this
location /images {
try_files $uri =404;
}
这意味着要么提供文件要么提供 404 错误,您不能在没有 =404
的情况下仅使用 $uri
,因为您需要回退选项.
您还可以选择所需的错误代码,例如:
which means either serve the file or serve a 404 error, you can't use only $uri
by it self without =404
because you need to have a fallback option.
You can also choose which ever error code you want, like for example:
location /images {
try_files $uri =403;
}
如果图像不存在,这将显示禁止错误,或者如果您使用 500,它将显示服务器错误等..
This will show a forbidden error if the image doesn't exist, or if you use 500 it will show server error, etc ..
这篇关于我怎样才能使这个 try_files 指令工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!