Nginx http_try_files_module try_files指令

http_try_files_module


http_try_files_module 模块只提供了try_files这一个指令,该模块对于我们使用反向代理的时候有很大的帮助,该模块是默认编译进nginx框架当中的,无法将其取消。

在搭建wordpress站点的时候可以使用到try_files指令。该指令用于反向代理达到场景非常有用,可以先去尝试在nginx上直接获取磁盘上的内容,如果没有这个文件再反向代理到上游服务。

 

try_files两种语法


Syntax:

try_files file ... uri;  --放文件路径的名称,可以放多个。也就是说访问请求的时候,会依次尝试这些文件是否可以返回,只要有一个文件可以返回就立刻返回该文件内容。如果所有的文件都访问不了就按照最后一个url访问的结果返回给用户

try_files file ... =code;  --最后一个url也可以是一个code,用来指定返回的错误码
Default: —

Context: server, location

功能:依次访问多个url对应的文件(由root或者alias指令指定),当文件存在时直接返回文件内容,如果所有文件不存在,则按照最后一个URL结果或者code返回

 

举几个例子如下


(1) 

Nginx的try_files作用:按选项所指定的顺序去检查用户请求的文件是否存在,如果本地存在的话则返回该请求;不存在的话将该请求转发到指定的其他路径

location / {
    try_files $uri  @java_page;
}
 

location @java_page {
    proxy_pass  http://127.0.0.1:8080
}

(2)

server {
 
    listen 80;
    server_name  api.xxx.com;
 
    root /mnt/try;
 
    location / {
        add_header  Content-Type 'text/html; charset=utf-8';
        #echo $uri;
        try_files $uri @default;
    }
 
    location @default {
        root /mnt/default;
    }
}

location @xxx解释:定义一个location段,不能被外部请求所访问,只能用于nginx内部配置指令使用,比如 try_files、error_page。
$uri解释:URI代表资源的名称,浏览器访问 http://api.xxx.com/abc/index.html 时,当前的$uri值为/abc/index.html

try_files作用:try_files会先尝试去/mnt/try目录下找abc目录下的index.html,如果有,直接返回,没有的话则跳转到@default部分(内部重定向)。在default部分会去/mnt/default目录下找abc目录下的index.html,有,直接返回,没有就返回404错误。try_files可以理解为实现rewrite的作用。

(3)

server {
	error_log  logs/myerror.log  info;
	root html/;
	default_type text/plain;
	
	location /first {
    		try_files /system/maintenance.html  #当访问frist指令,会去看看是否存在maintenance.html 这样一个文件。
如果这个文件找不到
             $uri $uri/index.html $uri.html  --如果这些资源都没有会去访问@lasturl
             @lasturl;
	}


	location @lasturl {
    		return 200 'lasturl!\n';
	}


	location /second {
		try_files $uri $uri/index.html $uri.html =404;  --会尝试访问里面所有的文件,当所有文件都找不到返回404
	}

}


[root@www ~]# curl 192.168.179.99/first
lasturl!
[root@www ~]# curl 192.168.179.99/second
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

(4)

location / {
              root html/;
              try_files  $uri $uri/index.html $uri.html
              @lasturl;
           }

location @lasturl {
              return 200 'lasturl!\n';
        }



[root@www html]# curl 192.168.179.99  --如果可以访问到资源,直接返回资源
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章