Nginx中break與last的區(qū)別詳析
先說區(qū)別
- last,重寫后的規(guī)則,會繼續(xù)用重寫后的值去匹配下面的location。
- break,重寫后的規(guī)則,不會去匹配下面的location。使用新的規(guī)則,直接發(fā)起一次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
命中l(wèi)ocation1,瀏覽器地址欄沒有變,直接去找 /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
命中l(wèi)ocation2,瀏覽器地址欄沒有變,重新匹配到 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格式化"]}';
}
總結(jié)
到此這篇關(guān)于Nginx中break與last區(qū)別的文章就介紹到這了,更多相關(guān)Nginx中break與last區(qū)別內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信