nginx配置url重寫

url重寫是指通過配置conf文件,以讓網站的url中達到某種狀態時則定向/跳轉到某個規則,比如常見的偽靜態、301重定向、瀏覽器定向等

rewrite

語法

在配置文件的server塊中寫,如:

server { rewrite 規則 定向路徑 重寫類型; }

  • 規則:可以是字元串或者正則來表示想匹配的目標url
  • 定向路徑:表示匹配到規則後要定向的路徑,如果規則里有正則,則可以使用$index來表示正則里的捕獲分組
  • 重寫類型:
    • last :相當於Apache里德(L)標記,表示完成rewrite,瀏覽器地址欄URL地址不變
    • break;本條規則匹配完成後,終止匹配,不再匹配後面的規則,瀏覽器地址欄URL地址不變
    • redirect:返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址
    • permanent:返回301永久重定向,瀏覽器地址欄會顯示跳轉後的URL地址

簡單例子

server {  # 訪問 /last.html 的時候,頁面內容重寫到 /index.html 中  rewrite /last.html /index.html last;  # 訪問 /break.html 的時候,頁面內容重寫到 /index.html 中,並停止後續的匹配  rewrite /break.html /index.html break;  # 訪問 /redirect.html 的時候,頁面直接302定向到 /index.html中  rewrite /redirect.html /index.html redirect;  # 訪問 /permanent.html 的時候,頁面直接301定向到 /index.html中  rewrite /permanent.html /index.html permanent;  # 把 /html/*.html => /post/*.html ,301定向  rewrite ^/html/(.+?).html$ /post/$1.html permanent;  # 把 /search/key => /search.html?keyword=key  rewrite ^/search/([^/]+?)(/|$) /search.html?keyword=$1 permanent;  }

last和break的區別

因為301和302不能簡單的只返回狀態碼,還必須有重定向的URL,這就是return指令無法返回301,302的原因了。這裡 last 和 break 區別有點難以理解:

  • last一般寫在server和if中,而break一般使用在location中
  • last不終止重寫後的url匹配,即新的url會再從server走一遍匹配流程,而break終止重寫後的匹配
  • break和last都能組織繼續執行後面的rewrite指令

location里一旦返回break則直接生效並停止後續的匹配location

server {  location / {  rewrite /last/ /q.html last;  rewrite /break/ /q.html break;  }  location = /q.html {  return 400;  }  }
  • 訪問/last/時重寫到/q.html,然後使用新的uri再匹配,正好匹配到locatoin = /q.html然後返回了400
  • 訪問/break時重寫到/q.html,由於返回了break,則直接停止了

if判斷

只是上面的簡單重寫很多時候滿足不了需求,比如需要判斷當文件不存在時、當路徑包含xx時等條件,則需要用到if

語法

if (表達式) { }

  • 當表達式只是一個變數時,如果值為空或任何以0開頭的字元串都會當做false
  • 直接比較變數和內容時,使用=或!=
  • ~正則表達式匹配,~*不區分大小寫的匹配,!~區分大小寫的不匹配

一些內置的條件判斷:

  • -f和!-f用來判斷是否存在文件
  • -d和!-d用來判斷是否存在目錄
  • -e和!-e用來判斷是否存在文件或目錄
  • -x和!-x用來判斷文件是否可執行

內置的全局變數

  • $args :這個變數等於請求行中的參數,同$query_string
  • $content_length : 請求頭中的Content-length欄位。
  • $content_type : 請求頭中的Content-Type欄位。
  • $document_root : 當前請求在root指令中指定的值。
  • $host : 請求主機頭欄位,否則為伺服器名稱。
  • $http_user_agent : 客戶端agent資訊
  • $http_cookie : 客戶端cookie資訊
  • $limit_rate : 這個變數可以限制連接速率。
  • $request_method : 客戶端請求的動作,通常為GET或POST。
  • $remote_addr : 客戶端的IP地址。
  • $remote_port : 客戶端的埠。
  • $remote_user : 已經經過Auth Basic Module驗證的用戶名。
  • $request_filename : 當前請求的文件路徑,由root或alias指令與URI請求生成。
  • $scheme : HTTP方法(如http,https)。
  • $server_protocol : 請求使用的協議,通常是HTTP/1.0或HTTP/1.1。
  • $server_addr : 伺服器地址,在完成一次系統調用後可以確定這個值。
  • $server_name : 伺服器名稱。
  • $server_port : 請求到達伺服器的埠號。
  • $request_uri : 包含請求參數的原始URI,不包含主機名,如:」/foo/bar.php?arg=baz」。
  • $uri : 不帶請求參數的當前URI,$uri不包含主機名,如」/foo/bar.html」。
  • $document_uri : 與$uri相同。

如:

  • 訪問鏈接是:http://localhost:88/test1/test2/test.php
  • 網站路徑是:/var/www/html
  • $host:localhost
  • $server_port:88
  • $request_uri:http://localhost:88/test1/test2/test.php
  • $document_uri:/test1/test2/test.php
  • $document_root:/var/www/html
  • $request_filename:/var/www/html/test1/test2/test.php

例子

# 如果文件不存在則返回400  if (!-f $request_filename) {  return 400;  }  # 如果host不是xuexb.com,則301到xuexb.com中  if ( $host != 'xuexb.com' ){  rewrite ^/(.*)$ https://xuexb.com/$1 permanent;  }  # 如果請求類型不是POST則返回405  if ($request_method = POST) {  return 405;  }  # 如果參數中有 a=1 則301到指定域名  if ($args ~ a=1) {  rewrite ^ http://example.com/ permanent;  }

在某種場景下可結合location規則來使用,如:

# 訪問 /test.html 時  location = /test.html {  # 默認值為xiaowu  set $name xiaowu;  # 如果參數中有 name=xx 則使用該值  if ($args ~* name=(w+?)(&|$)) {  set $name $1;  }  # 301  rewrite ^ /$name.html permanent;  }

上面表示:

  • /test.html => /xiaowu.html
  • /test.html?name=ok => /ok.html?name=ok

location

語法

server塊中使用,如:

server { location 表達式 { } }

location表達式類型

  • 如果直接寫一個路徑,則匹配該路徑下的
  • ~ 表示執行一個正則匹配,區分大小寫
  • ~* 表示執行一個正則匹配,不區分大小寫
  • ^~ 表示普通字元匹配。使用前綴匹配。如果匹配成功,則不再匹配其他location。
  • = 進行普通字元精確匹配。也就是完全匹配。

優先順序

  1. 等號類型(=)的優先順序最高。一旦匹配成功,則不再查找其他匹配項。
  2. ^~類型表達式。一旦匹配成功,則不再查找其他匹配項。
  3. 正則表達式類型(~ ~*)的優先順序次之。如果有多個location的正則能匹配的話,則使用正則表達式最長的那個。
  4. 常規字元串匹配類型。按前綴匹配。

例子 – 假地址掩飾真地址

server {  # 用 xxoo_admin 來掩飾 admin  location / {  # 使用break拿一旦匹配成功則忽略後續location  rewrite /xxoo_admin /admin break;  }  # 訪問真實地址直接報沒許可權  location /admin {  return 403;  }  }
轉自:https://xuexb.com/post/nginx-url-rewrite.html