[Nginx] Compare rewrite last break
syntax
rewrite regex replacement [ flag ]
- last – completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.
- break – completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.
- redirect – returns temporary redirect with code 302; it is used if the substituting line begins with http:// (デフォルト)
- permanent – returns permanent redirect with code 301
break last
/etc/nginx/conf.d/default.conf
root /usr/share/nginx/html; rewrite_log on; location /rewrite { rewrite ^/rewrite /foo; rewrite ^/foo /bar; add_header H-rewrite dummy; } location /last { rewrite ^/last /foo last; rewrite ^/foo /bar last; add_header H-last dummy; } location /break { rewrite ^/break /foo break; rewrite ^/foo /bar break; add_header H-break dummy; } location /foo { add_header H-foo dummy; } location /bar { add_header H-bar dummy; } location / { index index.html index.htm; add_header H-index dummy; } |
# echo foo > /usr/share/nginx/html/foo # echo bar > /usr/share/nginx/html/bar |
Test rewrite
$ curl -D - http://localhost/rewrite HTTP/1.1 200 OK Server: nginx/1.4.4 Date: Sun, 16 Feb 2014 06:05:42 GMT Content-Type: application/octet-stream Content-Length: 4 Last-Modified: Sat, 15 Feb 2014 15:12:05 GMT Connection: keep-alive ETag: "52ff83c5-4" H-bar: dummy Accept-Ranges: bytes bar |
nginx log
2014/02/16 15:05:42 [notice] 21738#0: *4 "^/rewrite" matches "/rewrite", client: 127.0.0.1, server: localhost, request: "GET /rewrite HTTP/1.1", host: "localhost" 2014/02/16 15:05:42 [notice] 21738#0: *4 rewritten data: "/foo", args: "", client: 127.0.0.1, server: localhost, request: "GET /rewrite HTTP/1.1", host: "localhost" 2014/02/16 15:05:42 [notice] 21738#0: *4 "^/foo" matches "/foo", client: 127.0.0.1, server: localhost, request: "GET /rewrite HTTP/1.1", host: "localhost" 2014/02/16 15:05:42 [notice] 21738#0: *4 rewritten data: "/bar", args: |
Test break rewrite
$ curl -D - http://localhost/break HTTP/1.1 200 OK Server: nginx/1.4.4 Date: Sun, 16 Feb 2014 06:06:14 GMT Content-Type: application/octet-stream Content-Length: 4 Last-Modified: Sat, 15 Feb 2014 15:12:01 GMT Connection: keep-alive ETag: "52ff83c1-4" H-break: dummy Accept-Ranges: bytes foo |
nginx log
2014/02/16 15:06:14 [notice] 21738#0: *5 "^/break" matches "/break", client: 127.0.0.1, server: localhost, request: "GET /break HTTP/1.1", host: "localhost" 2014/02/16 15:06:14 [notice] 21738#0: *5 rewritten data: "/foo", args: "", client: 127.0.0.1, server: localhost, request: "GET /break HTTP/1.1", host: "localhost" |
Test last rewrite
$ curl -D - http://localhost/last HTTP/1.1 200 OK Server: nginx/1.4.4 Date: Sun, 16 Feb 2014 06:06:38 GMT Content-Type: application/octet-stream Content-Length: 4 Last-Modified: Sat, 15 Feb 2014 15:12:01 GMT Connection: keep-alive ETag: "52ff83c1-4" H-foo: dummy Accept-Ranges: bytes foo |
nginx log
2014/02/16 15:06:38 [notice] 21738#0: *6 "^/last" matches "/last", client: 127.0.0.1, server: localhost, request: "GET /last HTTP/1.1", host: "localhost" 2014/02/16 15:06:38 [notice] 21738#0: *6 rewritten data: "/foo", args: "", client: 127.0.0.1, server: localhost, request: "GET /last HTTP/1.1", host: "localhost" |
References
- nginx : Module ngx_http_rewrite_module
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html - nginx : Converting rewrite rules
http://nginx.org/en/docs/http/converting_rewrite_rules.html - server fault : nginx url rewriting: difference between break and last
http://serverfault.com/questions/131474/nginx-url-rewriting-difference-between-break-and-last - Mastering nginx(packt pub.) : Appendix B: Rewrite Rule Guide
Nhận xét
Đăng nhận xét