首页 > 代码库 > Nginx学习笔记17rewrite之(四)last

Nginx学习笔记17rewrite之(四)last

1.1.1. last

last标志跟break标志的作用差不多,区别在于break标志处理之后,通常将不再匹配其它的location,即能够匹配rewrite目标地址的location中的proxy_pass等不会执行;last标志则会继续对rewrite的目标地址进行其它location的匹配,并执行其中的proxy_pass等动作。

 

Nginx配置文件:

 location / {

            root   html;

            index  index.html;

        }

 

        location ~  ^/hello/ {

             proxy_pass  http://tomcat101.coe2coe.me:8080;

             proxy_set_header X-Real-IP  $remote_addr;

             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 

            access_log logs/http_hello_access.log  my_access_log;

 

       }

 

 

      location ~ ^/app/ {

        rewrite ^/app/(.*)$  /hello/$1  last;

     }

 

 

     location ~ ^/app2/ {

        rewrite ^/app2/(.*)$  /app/$1  last;

     }

 

 

(1)访问/app/

 

运行结果:

使用curl -v  http://ng.coe2coe.me:8000/app/访问时,curl并未显示有任何的HTTP 301HTTP 302的重定向操作,使用浏览器访问时,地址栏URL无任何变化。

 

 

http_error.log错误日志:

2017/07/09 20:07:35 [notice] 3817#0: *97 "^/app/(.*)$" matches "/app/", client: 192.168.197.101, server: ng.coe2coe.me, request: "GET /app/ HTTP/1.1", host: "ng.coe2coe.me:8000"

2017/07/09 20:07:35 [notice] 3817#0: *97 rewritten data: "/hello/", args: "", client: 192.168.197.101, server: ng.coe2coe.me, request: "GET /app/ HTTP/1.1", host: "ng.coe2coe.me:8000"

2017/07/09 20:07:35 [info] 3817#0: *97 client 192.168.197.101 closed keepalive connection

 

(2)访问/app2/

运行结果:

curl -v  http://ng.coe2coe.me:8000/app2/,没有重定向操作,浏览器地址栏URL无任何变化。

 技术分享

 

http_access.loghttp_hello_access.log访问日志:

$cat http_access.log

[d@192.168.197.101:/opt/nginx/logs]$cat http_hello_access.log

192.168.197.101,-,09/Jul/2017:20:16:50 +0800,GET /app2/ HTTP/1.1,200,3182,-,curl/7.35.0

访问日志中只有/hello/对应的location中定义的访问日志记录了本次对/app2/的访问。

 

http_error.log错误日志:

[d@192.168.197.101:/opt/nginx/logs]$cat http_error.log

2017/07/09 20:16:50 [notice] 3906#0: *106 "^/app2/(.*)$" matches "/app2/", client: 192.168.197.101, server: ng.coe2coe.me, request: "GET /app2/ HTTP/1.1", host: "ng.coe2coe.me:8000"

2017/07/09 20:16:50 [notice] 3906#0: *106 rewritten data: "/app/", args: "", client: 192.168.197.101, server: ng.coe2coe.me, request: "GET /app2/ HTTP/1.1", host: "ng.coe2coe.me:8000"

2017/07/09 20:16:50 [notice] 3906#0: *106 "^/app/(.*)$" matches "/app/", client: 192.168.197.101, server: ng.coe2coe.me, request: "GET /app2/ HTTP/1.1", host: "ng.coe2coe.me:8000"

2017/07/09 20:16:50 [notice] 3906#0: *106 rewritten data: "/hello/", args: "", client: 192.168.197.101, server: ng.coe2coe.me, request: "GET /app2/ HTTP/1.1", host: "ng.coe2coe.me:8000"

2017/07/09 20:16:50 [info] 3906#0: *106 client 192.168.197.101 closed keepalive connection

 

本文小结:

last标志和break标志有不同点,last不会阻止对rewrite目标地址进行其它location的匹配,而break通常会阻止对其它location的匹配。二者也有相同点,即二者都不会因为rewrite而产生http 301http302的重定向操作,浏览器地址栏的URL不会产生变化。

这个相同点,也是lastbreak标志跟permanentredirect标志的一个区别。

 

1.1.2. 补充说明

 

rewritengx_http_rewrite_module模块的功能,完整介绍请参考官方网站相关页面:

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

 

Nginx学习笔记17rewrite之(四)last