您好,登錄后才能下訂單哦!
這篇文章主要介紹Nginx中如何實(shí)現(xiàn)rewrite正則匹配重寫,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
Nginx的rewrite功能支持正則匹配重寫,即將URL地址臨時(shí)或永久重新指向某個(gè)新的位置,類似于重定向。這個(gè)特性有利用當(dāng)網(wǎng)站結(jié)構(gòu)做出重大調(diào)整,如之前的網(wǎng)站mp3資源使用URL為www.site1.org/mp3進(jìn)行訪問(wèn),而現(xiàn)在服務(wù)器上mp3目錄已經(jīng)被使用music目錄替換,那rewrite這個(gè)功能則能夠輕松實(shí)現(xiàn)。其次如可以將site1.org強(qiáng)制調(diào)整到www.site1.org,反之亦可。這個(gè)指令位于ngx_http_rewrite_module模塊。
一、rewrite指令語(yǔ)法描述
句法: rewrite regex replacement [flag];
默認(rèn): -
語(yǔ)境: server,location,if
如果指定的正則表達(dá)式與請(qǐng)求URI匹配,則URI將按照replacement字符串中的指定進(jìn)行更改。
該rewrite指令在其在配置文件中出現(xiàn)的順序順序地執(zhí)行??梢允褂脴?biāo)志終止對(duì)偽指令的進(jìn)一步處理。
如果替換字符串以“ http://”,“ https://”或“ $scheme” 開(kāi)頭,則處理停止,并將重定向返回給客戶端。
flag標(biāo)志的作用是用于控制當(dāng)匹配到對(duì)應(yīng)的rewrite規(guī)則后是否繼續(xù)檢查后續(xù)的rewrite規(guī)則
可選flag參數(shù)可以是以下之一:
last
一旦被當(dāng)前規(guī)則匹配并重寫后立即停止檢查后續(xù)的其它rewrite的規(guī)則,而后通過(guò)重寫后的規(guī)則重新發(fā)起請(qǐng)求;
break
一旦被當(dāng)前規(guī)則匹配并重寫后立即停止后續(xù)的其它rewrite的規(guī)則,而后繼續(xù)由nginx進(jìn)行后續(xù)操作;
redirect
如果替換字符串不以“ http://”,“ https://”或“ $scheme” 開(kāi)頭,則使用,返回302臨時(shí)重定向;
permanent
返回301永久重定向;
注意:一般將rewrite寫在location中時(shí)都使用break標(biāo)志,或者將rewrite寫在if上下文中;
其他指令
rewrite_log on|off
是否把重寫過(guò)程記錄在錯(cuò)誤日志中;默認(rèn)為notice級(jí)別;默認(rèn)為off;
return code:
用于結(jié)束rewrite規(guī)則,并且為客戶返回狀態(tài)碼;可以使用的狀態(tài)碼有204, 400, 402-406, 500-504等;
二、基于location上下文rewrite功能演示
本機(jī)環(huán)境 # more /etc/redhat-release CentOS Linux release 7.2.1511 (Core) # nginx -v nginx version: nginx/1.12.2 配置nginx # vim /etc/nginx/conf.d/rewrite.conf server { listen 80; server_name site1.orag www.site1.org; location / { root /www/site1.org; index index.html index.htm; } } # mkdir -pv /www/site1.org/images # echo "This is a rewrite test page.">/www/site1.org/index.html # cp /usr/share/backgrounds/gnome/*.jpg /www/site1.org/images/ # vim /etc/hosts 192.168.1.175 site1.org 192.168.1.175 www.site1.org # curl http://www.site1.org This is a rewrite test page. # curl -I https://cache.yisu.com/upload/information/20200622/115/52724 HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 01 Nov 2017 03:47:58 GMT Content-Type: image/jpeg Content-Length: 458818 Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT Connection: keep-alive ETag: "59f942f4-70042" Accept-Ranges: bytes 修改rewrite.conf文件,添加rewrite指令 location / { root /www/site1.org; index index.html index.htm; rewrite ^/images/(.*)$ /imgs/$1 last; } # systemctl reload nginx # curl -I https://cache.yisu.com/upload/information/20200622/115/52724 HTTP/1.1 404 Not Found Server: nginx/1.12.2 Date: Wed, 01 Nov 2017 04:02:38 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive # mkdir -pv /www/site1.org/imgs # mv /www/site1.org/images/Waves.jpg /www/site1.org/imgs/. # curl -I https://cache.yisu.com/upload/information/20200622/115/52724 HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 01 Nov 2017 04:05:07 GMT Content-Type: image/jpeg Content-Length: 458818 Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT Connection: keep-alive ETag: "59f942f4-70042" Accept-Ranges: bytes # curl -I https://cache.yisu.com/upload/information/20200622/115/52725 ##這種方式可以訪問(wèn) HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 01 Nov 2017 04:06:17 GMT Content-Type: image/jpeg Content-Length: 458818 Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT Connection: keep-alive ETag: "59f942f4-70042" Accept-Ranges: bytes 模擬rewrite導(dǎo)致的http 500錯(cuò)誤 再次對(duì)rewrite.conf文件做如下修改, location / { root /www/site1.org; index index.html index.htm; rewrite ^/images/(.*)$ /imgs/$1 last; rewrite ^/imgs/(.*)$ /images/$1 ; } # systemctl restart nginx # curl -I https://cache.yisu.com/upload/information/20200622/115/52725 HTTP/1.1 500 Internal Server Error Server: nginx/1.12.2 Date: Wed, 01 Nov 2017 05:23:16 GMT Content-Type: text/html Content-Length: 193 Connection: close # curl -I https://cache.yisu.com/upload/information/20200622/115/52724 HTTP/1.1 500 Internal Server Error Server: nginx/1.12.2 Date: Wed, 01 Nov 2017 05:23:28 GMT Content-Type: text/html Content-Length: 193 Connection: close 通過(guò)上述的測(cè)試可知,出現(xiàn)了死循環(huán)導(dǎo)致的500錯(cuò)誤。 Nginx官方給出的參考樣例: server { ... ##rewrite指令位于server上下文 rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; ##將/download目錄中包含media目錄下的任意文件請(qǐng)求重定向?yàn)閐onwload/任意/mp3/任意.mp3 rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; ##將/download目錄中包含audio目錄下的任意文件請(qǐng)求重定向?yàn)閐onwload/任意/mp3/任意.mp3 return 403; ... } location /download/ { ##rewrite指令位于location上下文 rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; ##該last標(biāo)志應(yīng)該被替換 break,否則nginx將使10個(gè)周期返回500個(gè)錯(cuò)誤 rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; return 403; }
三、基于if條件判斷rewrite功能演示
# vi /etc/nginx/conf.d/rewrite.conf server { listen 80; server_name site1.orag www.site1.org; if ($host != 'www.site1.org' ) { rewrite ^/(.*)$ http://www.site1.org/$1 permanent; } location / { ##Author : Leshami root /www/site1.org; ##Blog : http://blog.csdn.net/leshami index index.html index.htm; rewrite ^/images/(.*)$ /imgs/$1 last; rewrite ^/imgs/(.*)$ /images/$1 ; } } # systemctl reload nginx.service 本地測(cè)試(修改本地host文件) # curl http://site1.org <html> ##返回301狀態(tài)碼 <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h2>301 Moved Permanently</h2></center> <hr><center>nginx/1.12.2</center> </body> </html> Windows環(huán)境測(cè)試 通過(guò)修改Windows機(jī)器Host文件后,添加如下條目 192.168.1.175 centos7-router.com 192.168.1.175 www.centos7-router.com 打開(kāi)瀏覽器,通過(guò)域名的方式進(jìn)行訪問(wèn)http://site1.org會(huì)自動(dòng)跳轉(zhuǎn)到http://www.site1.org(演示略)
四、將http重寫至https
在非全站https時(shí),對(duì)于有些敏感的數(shù)據(jù)需有走h(yuǎn)ttps,那也可以通過(guò)rewrite方式實(shí)現(xiàn)
如下示例,假定https://www.site1.org/user目錄下包含敏感信息,按可按如下方式rewrite
location ^~ /user { rewrite ^/ https://www.site1.org$request_uri? permanent; } 全站https server { listen 80; server_name site1.orag www.site1.org; access_log /var/log/nginx/http-access.log; error_log /var/log/nginx/http-error.log; rewrite ^/ https://www.site1.org$request_uri; }
以上是“Nginx中如何實(shí)現(xiàn)rewrite正則匹配重寫”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。