nginx 的precontent阶段的ngx_http_try_files_module模块与mirrors模块介绍

指令介绍

Syntax: try_files file ... uri;
try_files file ... =code;
Default: —
Context: server, location

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

server {
        server_name  try_files.com;
        root html/;
        default_type text/plain;
        location /first {
                try_files /system/maintenance.html  #找磁盘文件;找不到磁盘文件是
                $uri $uri/index.html $uri.html#找$URI也就是去找html/first有没有这个文件;匹配不到是;找first/下面有没index.html;
                @lasturi; #最后匹配这个
        }
        location @lasturi {
                return 200 'lasturi!
';
        }
        location /second {
                try_files $uri $uri/index.html $uri.html =404;
        }
}

  测试

[root@python vhast]# curl try_files.com/first
lasturi!

  mirrors 模块流量拷贝

功能:处理请求时生成子请求对子请求的返回结果不做处理

指令

Syntax: mirror uri | off;  #uri表示将请求复制到另一个地址
Default: mirror off; 
Context: http, server, location
Syntax: mirror_request_body on | off;  #mo
Default: mirror_request_body on; 
Context: http, server, location

  实现

[root@python vhast]# cat mirrors.conf 
server {
	server_name mirrors.com;
	error_log logs/mirrors_error.log debug;
	location /{
		mirror /mirror;
		mirror_request_body off;
	}
	location = /mirror {
		internal; #指定为内部请求
		proxy_pass http://127.0.0.1:10020$request_uri;
		proxy_set_header Content-Length "";
		proxy_set_header X-Original-URI $request_uri;
	}
}

[root@python vhast]# cat test-2.conf 
server {
	listen 10020;
	location / {
		return 200 "mirror response!";
	}
}

  测试

[root@python vhast]# echo "mirror" > ../../html/mirror.txt
[root@python vhast]# curl mirrors.com/mirror.txt
mirror
[root@python vhast]# tail ../../logs/access.log 
127.0.0.1 - - [10/Jul/2019:11:56:22 +0800] "GET / HTTP/1.0" 200 16 "-" "curl/7.29.0"
192.168.183.4 - - [10/Jul/2019:11:56:22 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0"
127.0.0.1 - - [10/Jul/2019:11:59:07 +0800] "GET /mirror.txt HTTP/1.0" 200 16 "-" "curl/7.29.0"
192.168.183.4 - - [10/Jul/2019:11:59:07 +0800] "GET /mirror.txt HTTP/1.1" 200 7 "-" "curl/7.29.0"
127.0.0.1 - - [10/Jul/2019:11:59:08 +0800] "GET /mirror.txt HTTP/1.0" 200 16 "-" "curl/7.29.0"
192.168.183.4 - - [10/Jul/2019:11:59:08 +0800] "GET /mirror.txt HTTP/1.1" 200 7 "-" "curl/7.29.0"
127.0.0.1 - - [10/Jul/2019:11:59:09 +0800] "GET /mirror.txt HTTP/1.0" 200 16 "-" "curl/7.29.0"
192.168.183.4 - - [10/Jul/2019:11:59:09 +0800] "GET /mirror.txt HTTP/1.1" 200 7 "-" "curl/7.29.0"
127.0.0.1 - - [10/Jul/2019:12:30:55 +0800] "GET /mirror.txt HTTP/1.0" 200 16 "-" "curl/7.29.0"
192.168.183.4 - - [10/Jul/2019:12:30:55 +0800] "GET /mirror.txt HTTP/1.1" 200 7 "-" "curl/7.29.0"

  

草都可以从石头缝隙中长出来更可况你呢
原文地址:https://www.cnblogs.com/rdchenxi/p/11165374.html