nginx rewrite
Nginx Rewrite
Nginx Rewrite概述
Rewrite跳轉場景
- URL看起來更規範、合理
- 企業會將動態URL地址偽裝成靜態地址提供服務
- 網址換新域名後,讓舊的訪問跳轉到新的域名上
- 服務段某些業務調整
Rewrite 實際場景
-
Nginx跳轉需求的實現方式
使用rewrite進行匹配跳轉
使用if匹配全域性變數後跳轉
使用location匹配再跳轉 -
rewrite放在server{},if{},location{}段中
location只對域名後邊的出去傳遞引數外的字串起作用 -
對域名或引數字串
使用if全域性變數匹配
使用proxy_pass反向代理
Nginx正規表示式
常用的正規表示式說明
字元 | 說明 |
---|---|
^ | 匹配輸入字串的其實位置 |
$ | 匹配輸入字串的結束位置 |
* | 匹配前面的字元零次或多次 |
+ | 匹配前面的字元一次或多次 |
? | 匹配前面的字元零次或一次 |
. | 匹配差除\n之外的任何單個字元 |
\ | 將後面接著的字元標記為一個特殊字元或一個原義字元或一個向後引用 |
\d | 匹配純數字 |
{n} | 重複n次 |
{n,} | 重複n次或更多次 |
[c] | 匹配單個字元c |
[a-z] | 匹配a-z小寫字母的任意一個 |
[a-zA-Z] | 匹配a-z小寫字母或A-Z大寫字母的任意一個 |
Rewrite命令
-
Rewrite命令語法
rewrite <regex> <replacement> [flag]; 正則 跳轉後內容(目標) rewrite支援flag標記
-
flag標記說明
標記 | 說明 |
---|---|
last | 相當於Apache的[L]標記,標識完成rewrite |
break | 本條規則匹配完成即終止,不再匹配後面的任何規則 |
redirect | 返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址,爬蟲不會更新url |
permanent | 返回301永久重定向,瀏覽器位址列會顯示跳轉後的URL地址,爬蟲更新url |
- last和break比較
last | breal | |
---|---|---|
使用場景 | 一般在server和if中 | 一般使用在location中 |
URL匹配 | 不總之重寫後的url匹配 | 終止重寫後的url匹配 |
location分類
-
分類
location = patt {} [精準匹配]
location patt {} [一般匹配]
location ~ patt {} [正則匹配] -
正則匹配常用表示式
標記 | 說明 |
---|---|
~ | 執行一個正則匹配,區分大小寫 |
~* | 執行一個正則匹配,不區分大小寫 |
!~ | 執行一個正則匹配,區分大小寫不匹配 |
!~* | 執行一個正則匹配,不區分大小寫不匹配 |
^~ | 普通字元匹配,使用字首匹配。如果匹配成功,則不再匹配其他location |
= | 普通字元精通匹配,也就是完全匹配 |
@ | 定義一個命名的location,使用在內部定向時 |
location 優先順序
-
相同型別的表示式,字串長的會優先匹配
-
按優先順序排列
=型別
^~型別表示式
正規表示式(和*)型別
常用字串匹配型別、按字首匹配
通用匹配(/),如果沒有其它匹配,任何請求都會匹配到 -
Location優先順序示例
location = / {
[configuration A]
} 精確匹配/,主機名後面不能帶任何字串
location = /documents/abc {
[configuration B]
} 匹配任何以/documents/abc開頭的地址,當後面的正規表示式沒有匹配到時,才會起作用
location = /documents {
[configuration C]
} 匹配任何以/documents開頭的地址,當後面的正規表示式沒有匹配到時,才會起作用
location / {
[configuration D]
} 所有的地址都以 / 開頭,這條規則將匹配到所有請求,但正則和最長字串會優先匹配
Location優先順序規則
- 匹配某個具體檔案
(location = 完整路徑)> (location ^~ 完整路徑) > (location ~* 完整路徑)= (location ~ 完整路徑)> (location 完整路徑) > (location /)
- 用目錄做匹配訪問某個檔案
(location = 目錄)> (location ^~ 目錄/)> (location ~ 目錄) = (location ~* 目錄)> (location 目錄) > (location /)
比較rewrtie和location
-
相同點
都能實現跳轉 -
不同點
rewrite是在同一域名內更改獲取資源的路徑
location是對一類路徑做控制訪問或反向代理,還可以proxy_pass到其它機器 -
rewrite會寫在location裡,執行順序
執行server塊裡面的rewrite指令
執行location匹配
執行選定的location中的rewrite指令
Rewrite應用例項
基於域名的跳轉
不能廢除舊域名
從舊域名跳轉到新域名,切保持其引數不變
1.修改預設站點配置檔案(nginx.conf)
server {
listen 80;
server_name www.test.com;
if ($host = 'www.test.com')
{
rewrite ^/(.*)$ http://www.newtest.com/$1 permanent;
}
charset utf-8;
access_log logs/test.com.access.log main;
server {
listen 80;
server_name www.newtest.com;
charset utf-8;
access_log /var/log/nginx/www.newtest.com.access.log main;
location / {
root /usr/share/nginx/newhtml;
index index.html index.htm;
}
}
在瀏覽器上查結果
基於客戶端IP地址的跳轉
應用場景,其它IP訪問任何內容都會顯示一個固定維護頁面,只有設定IP可以正常訪問
- 修改預設站點配置檔案(nginx.conf)
set $rewrite true;
if ($remote_addr = "20.0.0.18"){
set $rewrite false;
}
if ($rewrite = true) {
rewrite (.+) /maintenance.html;
}
location = /maintenance.html {
root /usr/local/nginx/html;
}
-
新增新域名到站點位置,並在瀏覽器上測試
echo "this web is Maintaining,please visit later this pen apple" >> /usr/local/nginx/html/maintenance.html
- 修改本機IP,在客戶機訪問新IP 20.0.0.10 訪問 20.0.0.15
基於舊新域名的跳轉並加目錄
- 在 /usr/local/nginx/html/abc/post/建立1.html
cd /usr/local/nginx/html/
mkdir -p abc/post
cd abc/post/
echo "this is 20.0.0.18" >> 1.html
-
修改預設站點配置檔案(nginx.conf)
location /post { rewrite (.+) http://www.test.com/bbs$1 permanent; } location / { root html; index index.html index.htm; }
- 新增新域名到站點位置
server {
listen 80;
server_name www.newtest.com;
charset utf-8;
access_log /var/log/nginx/www.newtest.com.access.log main;
location / {
root html;
index index.html index.htm;
}
}
- 測試
基於匹配引數的跳轉
-
修改預設站點配置檔案(nginx.conf)
匹配100-100-任意數字的.html結尾的檔案,重定向到/www.test.comif ($request_uri ~ ^/100-(100|200)-(\d+).html$) { rewrite (.*) http://www.test.com permanent; }
- 測試www.test.com/100-100-100.html
相關文章
- nginx rewriteNginx
- Nginx rewrite 詳解Nginx
- nginx rewrite語法格式Nginx
- nginx thinkphp rewrite配置項NginxPHP
- nginx之rewrite匹配需求Nginx
- Nginx Rewrite規則初探Nginx
- nginx的rewrite設定Nginx
- nginx高階篇rewriteNginx
- Nginx 實現 Rewrite 跳轉Nginx
- nginx location匹配及rewrite規則Nginx
- Nginx location匹配及Rewrite重寫Nginx
- Nginx Rewrite實際應用配置解析Nginx
- Nginx 學習總結(4)—— Rewrite 模組Nginx
- nginx通過rewrite方式處理路由Nginx路由
- Nginx常用Rewrite偽靜態規則Nginx
- Nginx的Rewrite規則與例項Nginx
- nginx學習-ngx_http_rewrite_module模組NginxHTTP
- NGINX使用rewrite實現http 跳轉 httpsNginxHTTP
- Nginx rewrite 規則 與 proxy_pass 實現Nginx
- 一篇文章說透Nginx的rewrite模組Nginx
- nginx配置location總結及rewrite規則寫法Nginx
- 第七章:nginx的rewrite規則詳解Nginx
- Yii框架在Nginx下的rewrite配置(偽靜態配置)框架Nginx
- rewrite
- ubuntu15 安裝nginx 報錯:the HTTP rewrite module requires the PCRE library.UbuntuNginxHTTPUI
- rewrite規則中引數多於9個的處理方式 apache nginxApacheNginx
- nginx(二):進階配置介紹–rewrite用法,壓縮,https虛擬主機等NginxHTTP
- Apache Rewrite詳解Apache
- mod_rewrite模組
- Clang -rewrite-objcOBJ
- nginx負載均衡(5種方式)、rewrite重寫規則及多server反代配置梳理Nginx負載Server
- MySQL Rewriter Query Rewrite PluginMySqlPlugin
- Rewrite %{HTTP_HOST}用法HTTP
- Rewrite %{REQUEST_FILENAME}用法
- 雲端計算運維學習---Nginx服務中rewrite引數的死迴圈問題運維Nginx
- Rewrite %{HTTP_USER_AGENT}用法HTTP
- Rewrite %{QUERY_STRING}用法
- Rewrite重寫教程前言