Nginx的Rewrite指令
Rewrite指令
rewrite regex replacement flag
后面的flag可以是:
last - completes processing of rewrite directives, after which searches for corresponding URI and location
break - completes processing of rewrite directives
redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
permanent - returns permanent redirect with code 301
后面的flag可以是:
last - completes processing of rewrite directives, after which searches for corresponding URI and location
break - completes processing of rewrite directives
redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
permanent - returns permanent redirect with code 301
需要注意的是,如果rewrite后,不要它再去匹配其它的location,则需要加上break,如:
折叠复制代码
- location ^~ /html/l/
- {
- root /opt/docparser;
- rewrite "^/html/l/(.*)$" /$1;
- #注意,需要有break,否则rewrite后还会继续匹配下面的location
- break;
- }
- location ~ /(.*)$ {
- proxy_pass http://127.0.0.1:8080;
- }
rewrite可以和proxy_pass配合使用:
折叠复制代码
- location ^~ /address/
- {
- rewrite /address/(.+)$ /$1 break;
- proxy_pass http://mail_addr;
- }
注意,rewrite后的url是包含后面的get参数的,不需要手工加上?$args的
另外,rewrite后,再转发时,使用proxy_pass指令时,里面似乎不能带参数