先说区别
- last,重写后的规定,会持续用重写后的值去匹配上面的location。
- break,重写后的规定,不会去匹配上面的location。应用新的规定,间接发动一次http申请了。
Nginx 配置文件
server { listen 88; server_name _; location /break { # location 1 rewrite ^/break/(.*)$ /bak/$1 break; } location /last { # location 2 rewrite ^/last/(.*)$ /bak/$1 last; } location /bak { # location 3 default_type text/html; return 200 $uri; }}
拜访 http://rumenz.com:88/break/one
命中location1,浏览器地址栏没有变,间接去找/nginx/html/bak/one
文件,因为没有这个文件所以返回404。
浏览器
Nginx谬误(error.log)日志
/nginx/html/bak/one failed (2: No such file or directory)
break
示意重写后进行不再匹配location
块。
拜访 http://rumenz.com:88/last/one
命中location2,浏览器地址栏没有变,从新匹配到location3
last示意重写后跳到location块再次用重写后的地址匹配
break
和last
的应用场景
break
文件下载,暗藏爱护实在文件服务器。
location /down { rewrite ^/down/(.*)$ https://rumenz.com/file/$1 break;}
last
接口地址改写,将https://rumenz.com/api/list
改写成https://rumenz.com/newapi/list
location /api { rewrite ^/api/(.*)$ /newapi/$1 last;}location /newapi { default_type Application/json; return 200 '{"code":200,"msg":"ok","data":["JSON.IM","json格式化"]}';}
关注微信公众号:【入门小站】,解锁更多知识点