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