http{#ngx_http_rewrite_module模块中的指令的优先级比ngx_http_core_module模块中的指令高。server{#访问127.0.0.1/b3时,依次顺序执行ngx_http_rewrite_module指令,执行break执行后,将无法在执行后面的rewrite^/b3/test/b3指令,故会返回404。rewrite^/b1/test/b1;rewrite^/(b2)/test/$1;#执行到break指令,将不会执行下面的rewrite,因为break指令会终止ngx_http_rewrite_module模块中的指令执行break;rewrite^/b3/test/b3;location/test/b3{add_headerContent-Type'text/html; charset=utf-8';return200"Hello My is test-b3";}location/test{#注意break只能终止ngx_http_rewrite_module指令集合,所以执行break指令后,return不会在执行,但是会执行pass_proxybreak;return200"this test break ";pass_proxy:127.0.0.1/xxx/}location/break{#通过break语句,控制改走return还是走pass_proxyif($uri=='/break/xx'){break;}#上面的if指令满足后,执行break指令,会终止return执行,然后运行pass_proxy执行,如果条件不满足,不执行break则会执行return语句直接返回,就会执行不到pass_proxy指令return200"this test break ";pass_proxy:127.0.0.1/break}}}
set$var0;location/test{add_headerContent-Type'text/html; charset=utf-8';if($var){return200"this is true";}return200"this is flase";}
变量和字符串比较只支持 = 和 != 符号
location/test{add_headerContent-Type'text/html; charset=utf-8';if($request_method=POST){return200"this is POST method";}#变量支持在字符串中使用return200"unkown $request_method method";}
location/exists{add_headerContent-Type'text/html; charset=utf-8';if(-fC:/DevTools/nginx-1.22.1/html/index.html){return200"file is exists";}return200"file is not exists";}
支持使用 -d 和 !-d 符号检查目录是否存在
location/exists{add_headerContent-Type'text/html; charset=utf-8';if(-dC:/DevTools/nginx-1.22.1/html){return200"dir is exists";}return200"dir is not exists";}
支持使用 -e 和 !-e 运算符检查文件、目录或符号链接是否存在
location/exists{add_headerContent-Type'text/html; charset=utf-8';if(-eC:/DevTools/nginx-1.22.1/html/index.html){return200"dir is exists";}return200"dir is not exists";}
支持使用 -x 和 -x 运算符检查可执行文件
location/exec{add_headerContent-Type'text/html; charset=utf-8';if(-xC:/DevTools/nginx-1.22.1/nginx.exe){return200"file is exec";}return200"file is not exec";}
不支持与或逻辑符号判断,但是可以通过变量拼接字符串的形式实现
set$flag"";location/test{if($request_method=POST){set$flag"$flag1";}if($protocol=HTTP){set$flag"$flag1";}if($flag="11"){return200"this is and ";}return200"hello world"}
If the specified regular expression matches a request URI, URI is changed as specified in the replacement string. The rewrite directives are executed sequentially in order of their appearance in the configuration file. It is possible to terminate further processing of the directives using flags. If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client.
last:表示 regex 正则表达式匹配到 URI 后立即停止处理当前 ngx_http_rewrite_module 指令集合,并用 replactment 后的 URI 匹配查找 location
break:表示 regex 正则表达式匹配到 URI 停止处理当前 ngx_http_rewrite_module 指令集合,如同处理 break 指令一样
redirect:返回一个状态是 302 的临时重定向
permanent:返回一个状态是 301 的永久重定向
永久重定向和临时重定向的区别是:浏览器会缓存永久重定向会记录,比如浏览器第一次访问 A 链接,如果返回 301 永久重定向到 B 链接,浏览器就会缓存 A 链接到 B 链接重定向记录,当浏览器再访问 A 链接时,如果缓存中有重定向记录则会直接重定向到 B 链接。如果第一次访问返回的 302 临时重定到 B 链接,浏览器每次都会重新 A 链接由 nginx 判断是不是重定向。即永久重定向会读缓存减轻服务器压力,临时重定向不会缓存,因为是临时的,临时说明随时会改变的所以不能缓存起来。
rewrite 指令在 server 指令语句块的例子说明:
http{#注意,在同一指令语句块中所有的rewrite指令是一个集合,nginx会按顺序匹配,并且优选高于location,在server指令中使用last和break符号,其效果是一样的。server{#没有使用last或者break标识符,如访问127.0.0.1/test会按rewrite指令的顺序去匹配/test,匹配到^/test后,还会继续使用重写后的URI重写匹配,一直往下匹配到test5,然后在使用test5去匹配查找locationrewrite^/test/test1;rewrite^/test1/test2;rewrite^/test2/test3;rewrite^/test3/test4;rewrite^/test4/test5;#如访问127.0.0.1/last2时,nginx会按顺序从上面的^/test开头的rewirte一直到^/last2时匹配成功,重写URI为/last3因为last标识符结尾,所以直接终止匹配,然后使用/last3去匹配location,#如果能匹配查找到location则返回结果,否则直接返回404rewrite^/last/last1last;rewrite^/last1/last2last;rewrite^/last2/last3last;#注意,如果重写的URI使用的是http协议开头,则相当于临时重定向,会直接返回给浏览器,浏览器在重定向请求新的URL。只有重写请求的URI部分,nginx才会去重新执行后续的rewrite或者匹配查找locationrewrite^/redirecthttp://127.0.0.1/test.com;rewrite^/redirect/lasthttp://127.0.0.1/test.comlast;rewrite^/redirect/breakhttp://127.0.0.1/test.combreak;#如访问访问127.0.0.1/hello也是先匹配rewrite集合如果匹配不到,再匹配查找location,如果都匹配不到的话,nginx会直接返回404location/hello{}location/last3{add_headerContent-Type'text/html; charset=utf-8';return200"this is /last2 rewrite /last3"}location/test5{add_headerContent-Type'text/html; charset=utf-8';return200"this is /test rewrite /test5"}}}
在 location 指令中的 rewrite 指令例子:
location/break{#location中的rewrite使用break标识,如果匹配了,就会去访问nginx安装目录下html目录的test/page文件rewrite^/break/test/pagebreak;#如果rewrite不匹配,执行returnadd_headerContent-Type'text/html; charset=utf-8';return200"can't match rewrite, so return";}location/last{#location中的rewrite如果使用last标识,根server指令中的效果一致rewrite^/break/testlast;#匹配不通过就走return,因为return的优先级比pass_proxy高pass_proxy127.0.0.1/hello;return200"Hello this is last";}
#单独返回状态码location/return0{return404;}#返回字符串location/return1{add_headerContent-Type'text/html; charset=utf-8';return200"request is success";}#302重定向请求location/return2{returnhttps://baidu.com;#或者return302https://baidu.com;}#返回内置变量location/return3{return200"请求的 URI : $uri ,请求的IP地址:$remote_addr";}
set 指令
set 指令用于定义变量,其官方语法如下:
Syntax: set $variable value;
Default: —
Context: server, location, if