nginx Rewrite重寫地址的實現
目錄
- 一、Nginx正則表達式
- 二、location
- 2.1、location大致可以分為三類
- 2.2、 location 常用的匹配規則
- 2.3、 location優先級
- 2.4、location示例說明
- 2.5、location 匹配
- 2.6、實際網站使用中,至少有三個匹配規則定義
- 三、rewrite
- 3.1、rewrite跳轉實現
- 3.2、rewrite執行順序
- 3.3、語法格式
- 3.4、flag標記說明
- 四、rewrite 示例
- 4.1、基于域名的跳轉
- 4.2、基于客戶端 IP 訪問跳轉
- 4.3、基于舊域名跳轉到新域名后面加目錄
- 4.4、基于參數匹配的跳轉
- 4.5、基于目錄下所有 php 結尾的文件跳轉
- 4.6、基于最普通一條 url 請求的跳轉
- 五、總結
一、Nginx正則表達式
常用的正則表達式元字符
從功能看 rewrite 和 location 似乎有點像,都能實現跳轉,主要區別在于 rewrite 是在同一域名內更改獲取資源的路徑,而 location 是對一類路徑做控制訪問或反向代理,還可以proxy_pass 到其他機器。
二、location
2.1、location大致可以分為三類
- 精確匹配:location = / { … }
- 一般匹配:location / { … }
- 正則匹配:location ~ / { … }
2.2、 location 常用的匹配規則
2.3、 location優先級
- 首先精確匹配 =
- 其次前綴匹配 ^~
- 其次是按文件中順序的正則匹配 ~ 或 ~*
- 然后匹配不帶任何修飾的前綴匹配
- 最后是交給 / 通用匹配
2.4、location示例說明
(1)location = / {}
=為精確匹配 / ,主機名后面不能帶任何字符串,比如訪問 / 和 /data,則 / 匹配,/data 不匹配
再比如 location = /abc,則只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,則即匹配/abc 、/abcd/ 同時也匹配 /abc/。
(2)location / {}
因為所有的地址都以 / 開頭,所以這條規則將匹配到所有請求 比如訪問 / 和 /data, 則 / 匹配, /data 也匹配,
但若后面是正則表達式會和最長字符串優先匹配(最長匹配)
(3)location /documents/ {}
匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續往下搜索其它 location
只有其它 location后面的正則表達式沒有匹配到時,才會采用這一條
(4)location /documents/abc {} www.baidu.com
匹配任何以 /documents/abc 開頭的地址,匹配符合以后,還要繼續往下搜索其它 location
只有其它 location后面的正則表達式沒有匹配到時,才會采用這一條
(5)location ^~ /images/ {}
匹配任何以 /images/ 開頭的地址,匹配符合以后,停止往下搜索正則,采用這一條
(6)location ~* \.(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 結尾的請求
然而,所有請求 /images/ 下的圖片會被 location ^~ /images/ 處理,因為 ^~ 的優先級更高,所以到達不了這一條正則
(7)location /images/abc {}
最長字符匹配到 /images/abc,優先級最低,繼續往下搜索其它 location,會發現 ^~ 和 ~ 存在
(8)location ~ /images/abc {}
匹配以/images/abc 開頭的,優先級次之,只有去掉 location ^~ /images/ 才會采用這一條
(9)location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正則location ~ /images/abc/1.html 相比,正則優先級更高
優先級總結:
(location = 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (location /)
2.5、location 匹配
- 首先看 優先級:精確>前綴>正則>一般>通用
- 優先級相同:正則看上下順序,上面的優先;一般匹配看長度,最長匹配的優先
- 精確、前綴、正則、一般 都沒有匹配到,最后再看通用匹配
2.6、實際網站使用中,至少有三個匹配規則定義
1.第一個必選規則
- 直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理,比如說官網。
- 可以是一個靜態首頁,也可以直接轉發給后端應用服務器
location = / { root html; index index.html index.htm;}
2.第二個必選規則
是處理靜態文件請求,這是nginx作為http服務器的強項 有兩種配置模式,目錄匹配或后綴匹配,任選其一或搭配使用
location ^~ /static/ { root /webroot/static/;}location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/;}
3.第三個規則
就是通用規則,比如用來轉發帶.php、.jsp后綴的動態請求到后端應用服務器
非靜態文件請求就默認是動態請求
location / { proxy_pass http://tomcat_server;}
三、rewrite
rewrite功能就是,使用nginx提供的全局變量或自己設置的變量,結合正則表達式和標記位實現URL重寫以及重定向。
rewrite只能放在server{},location{},if{}中,并且默認只能對域名后邊的除去傳遞的參數外的字符串起作用, 例如 http://www.kgc.com/abc/bbs/index.php?a=1&b=2只對/abc/bbs/index.php重寫。
URL:就是具體路徑/位置
URl:指的是一個擁有相同類型/特性的對象集合
3.1、rewrite跳轉實現
- Nginx:通過ngx_http_rewrite_module 模塊支持URL重寫、支持if條件判斷,但不支持else
- 跳轉:從一個 location跳轉到另一個location,循環最多可以執行10次,超過后nginx將返回500錯誤
- PCRE支持:perl兼容正則表達式的語法規則匹配
- 重寫模塊 set 指令:創建新的變量并設其值
3.2、rewrite執行順序
- 執行 server 塊里面的 rewrite 指令。
- 執行 location 匹配。
- 執行選定的 location 中的 rewrite 指令。
3.3、語法格式
rewrite <regex> <replacement> [flag];
- regex :表示正則匹配規則。
- replacement :表示跳轉后的內容。
- flag :表示 rewrite 支持的 flag 標記。
3.4、flag標記說明
- last :本條規則匹配完成后,繼續向下匹配新的location URL規則,一般用在 server 和 if 中。
- break :本條規則匹配完成即終止,不再匹配后面的任何規則,一般使用在 location 中。
- redirect :返回302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址。
- permanent :返回301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址。
四、rewrite 示例
4.1、基于域名的跳轉
現在公司舊域名www.kgc.com有業務需求變更,需要使用新域名www.benet.com代替,但是舊域名不能廢除,需要跳轉到新域名上,而且后面的參數保持不變。
vim /usr/local/nginx/conf/nginx.confserver { listen 80; server_name www.gc.com; charset utf-8; access_log /var/log/nginx/www.gc.com.access.log; location / {if ($host = "www.gc.com"){ rewrite ^/(.*)$ http://www.qqq.com/$1 permanent;}root html;index index.html index.htm; }}echo "192.168.132.6 www.gc.com www.qqq.com" >> /etc/hostsmkdir -p /var/log/nginx/systemctl restart nginxcd /usr/local/nginx/html/testvim 1.html
將對應的ip和域名寫入etc下hosts文件
瀏覽器輸入模擬訪問 http://www.gc.com/test/1.html(雖然這個請求內容是不存在的)
會跳轉到www.qqq.com/test/1.html,查看元素可以看到返回301,實現了永久重定向跳轉,而且域名后的參數也正常跳轉。
4.2、基于客戶端 IP 訪問跳轉
今天公司業務新版本上線,要求所有 IP 訪問任何內容都顯示一個固定維護頁面,只有公司 IP :192.168.80.10訪問正常。
vim /usr/local/nginx/conf/nginx.confserver { listen 80; server_name www.gc.com; #域名修改 charset utf-8; access_log /var/log/nginx/www.gc.com-access.log; #日志修改 #設置是否合法的IP標記 set $rewrite true; #設置變量$rewrite,變量值為boole值true #判斷是否為合法IP if ($remote_addr = "192.168.190.52"){ #當客戶端IP為192.168.190.52時,將變量值設為false,不進行重寫set $rewrite false; } #除了合法IP,其它都是非法IP,進行重寫跳轉維護頁面 if ($rewrite = true){ #當變量值為true時,進行重寫rewrite (.+) /weihu.html; #將域名后邊的路徑重寫成/weihu.html,例如www.kgc.com/weihu.html } location = /weihu.html {root /var/www/html; #網頁返回/var/www/html/weihu.html的內容 } location / {root html;index index.html index.htm; }}mkdir -p /var/www/html/echo "<h1>正在維護!!!等會再來</h1>" > /var/www/html/weihu.htmlsystemctl restart nginx
只有 IP 為 192.168.190.52 能正常訪問,其它地址都是維護頁面
換一個臺服務器訪問 顯示 訪問失敗
4.3、基于舊域名跳轉到新域名后面加目錄
現在訪問的是 http://bbs.gc.com/post/,現在需要將這個域名下面的訪問都跳轉到http://www.gc.com/bbs/post/
vim /usr/local/nginx/conf/nginx.confserver { listen 80; server_name bbs.gc.com; #域名修改 charset utf-8; access_log /var/log/nginx/www.gc.com-access.log; #添加 location /post {rewrite (.+) http://www.gc.com/bbs$1 permanent; #這里的$1為位置變量,代表/post } location / {root html;index index.html index.htm; }}mkdir -p /usr/local/nginx/html/bbs/postecho "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.htmlecho "192.168.190.52 bbs.gc.com www.gc.com" >> /etc/hostssystemctl restart nginx
http://bbs.gc.com/post/1.html 跳轉到 http://www.gc.com/bbs/post/1.html
4.4、基于參數匹配的跳轉
現在訪問http://www.gc.com/100-(100|200)-100(任意數字).html 跳轉到http://www.gc.com頁面。
vim /usr/local/nginx/conf/nginx.confserver { listen 80; server_name www.gc.com; charset utf-8; access_log /var/log/nginx/www.gc.com.access.log; if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {rewrite (.+) http://www.gc.com permanent; } location / {root html;index index.html index.htm; }}echo "192.168.190.52 www.gc.com" >> /etc/hostssystemctl restart nginx瀏覽器訪問http://www.gc.com/100-200-100.html 或 http://www.gc.com/100-100-100.html 跳轉到http://www.gc.com頁面。
4.5、基于目錄下所有 php 結尾的文件跳轉
要求訪問 http://www.gc.com/upload/abc.php 跳轉到首頁。
vim /usr/local/nginx/conf/nginx.confserver { listen 80; server_name www.gc.com; charset utf-8; access_log /var/log/nginx/www.gc.com.access.log; location ~* /upload/.*\.php$ { rewrite (.+) http://www.gc.com permanent;}location / { root html; index index.html index.htm;}}echo "192.168.190.52 www.gc.com" >> /etc/hostssystemctl restart nginx瀏覽器訪問http://www.gc.com/upload/abc.php 跳轉到http://www.gc.com頁面。
4.6、基于最普通一條 url 請求的跳轉
?要求訪問一個具體的頁面如 http://www.gc.com/abc/123.html 跳轉到首頁
vim /usr/local/nginx/conf/nginx.confserver { listen 80; server_name www.gc.com; #域名修改 charset utf-8; access_log /var/log/nginx/www.gc.com-access.log; location ~* ^/abc/123.html {rewrite (.+) http://www.gc.com permanent; } location / {root html;index index.html index.htm; }}systemctl restart nginx瀏覽器訪問 http://www.gc.com/abc/123.html 跳轉到http://www.gc.com頁面。
五、總結
本章內容講述了Rewrite跳轉實現、nginx中正則的表達方法以及關于location的匹配。更多相關nginx Rewrite重寫地址內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!