如何使try_files指令起作用? [英] How can I make this try_files directive work?

查看:163
本文介绍了如何使try_files指令起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解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中是否有一个名为如果找到,它将首先为您服务.

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.

编辑:我忘了提一下,如果您已定义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调用它

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆