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


需要注意的是,如果rewrite后,不要它再去匹配其它的location,则需要加上break,如:



折叠复制代码




  1. location ^~ /html/l/

  2. {

  3. root /opt/docparser;

  4. rewrite "^/html/l/(.*)$" /$1;

  5. #注意,需要有break,否则rewrite后还会继续匹配下面的location

  6. break;

  7. }

  8. location ~ /(.*)$ {

  9. proxy_pass http://127.0.0.1:8080;

  10. }




rewrite可以和proxy_pass配合使用:



折叠复制代码




  1. location ^~ /address/

  2. {

  3. rewrite /address/(.+)$ /$1 break;

  4. proxy_pass http://mail_addr;

  5. }




注意,rewrite后的url是包含后面的get参数的,不需要手工加上?$args的


另外,rewrite后,再转发时,使用proxy_pass指令时,里面似乎不能带参数