您好,登錄后才能下訂單哦!
本篇文章為大家展示了break與last在Nginx中的區(qū)別是什么,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
先說區(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格式化"]}'; }
上述內(nèi)容就是break與last在Nginx中的區(qū)別是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。