rewrite
//语法
rewrite regex replacement flag;
//例如
location /test {
rewrite ^/test/(.*)$ /test2/index.html break;
}
location /test2 {
root html;
}
[root@client conf]# cd ../html/
[root@client html]# mkdir test2
[root@client html]# cd test2/
[root@client test2]# echo '666' > index.html
[root@client test2]# cat index.html
666
[root@client ~]# curl 192.168.129.137/test2/index.html
666
[root@client ~]# curl 192.168.129.137/test/index.html
666
常见的flag
flag | 作用 |
---|
last | 基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程 |
break | 中止Rewrite,不再继续匹配一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求,且不再会被当前location内的任何rewrite规则所检查 |
redirect | 以临时重定向的HTTP状态302返回新的URL |
permanent | 以永久重定向的HTTP状态301返回新的URL0 |
标识符 | 意义 |
---|
^ | 必须以^后的实体开头 |
$ | 必须以$前的实体结尾 |
. | 匹配任意字符 |
[] | 匹配指定字符集内的任意字符 |
[^] | 匹配任何不包括在指定字符集内的任意字符串 |
| | 匹配 | 之前或之后的实体 |
() | 分组,组成一组用于匹配的实体,通常会有 | 来协助 |