溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

break與last在Nginx中的區(qū)別是什么

發(fā)布時間:2021-01-18 14:43:53 來源:億速云 閱讀:178 作者:Leah 欄目:開發(fā)技術

本篇文章為大家展示了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。

瀏覽器

break與last在Nginx中的區(qū)別是什么

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 

break與last在Nginx中的區(qū)別是什么 

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è)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI