您好,登錄后才能下訂單哦!
這篇文章主要介紹Nginx中如何實現rewrite正則匹配重寫,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Nginx的rewrite功能支持正則匹配重寫,即將URL地址臨時或永久重新指向某個新的位置,類似于重定向。這個特性有利用當網站結構做出重大調整,如之前的網站mp3資源使用URL為www.site1.org/mp3進行訪問,而現在服務器上mp3目錄已經被使用music目錄替換,那rewrite這個功能則能夠輕松實現。其次如可以將site1.org強制調整到www.site1.org,反之亦可。這個指令位于ngx_http_rewrite_module模塊。
一、rewrite指令語法描述
句法: rewrite regex replacement [flag];
默認: -
語境: server,location,if
如果指定的正則表達式與請求URI匹配,則URI將按照replacement字符串中的指定進行更改。
該rewrite指令在其在配置文件中出現的順序順序地執行。可以使用標志終止對偽指令的進一步處理。
如果替換字符串以“ http://”,“ https://”或“ $scheme” 開頭,則處理停止,并將重定向返回給客戶端。
flag標志的作用是用于控制當匹配到對應的rewrite規則后是否繼續檢查后續的rewrite規則
可選flag參數可以是以下之一:
last
一旦被當前規則匹配并重寫后立即停止檢查后續的其它rewrite的規則,而后通過重寫后的規則重新發起請求;
break
一旦被當前規則匹配并重寫后立即停止后續的其它rewrite的規則,而后繼續由nginx進行后續操作;
redirect
如果替換字符串不以“ http://”,“ https://”或“ $scheme” 開頭,則使用,返回302臨時重定向;
permanent
返回301永久重定向;
注意:一般將rewrite寫在location中時都使用break標志,或者將rewrite寫在if上下文中;
其他指令
rewrite_log on|off
是否把重寫過程記錄在錯誤日志中;默認為notice級別;默認為off;
return code:
用于結束rewrite規則,并且為客戶返回狀態碼;可以使用的狀態碼有204, 400, 402-406, 500-504等;
二、基于location上下文rewrite功能演示
本機環境 # 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 ##這種方式可以訪問 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導致的http 500錯誤 再次對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 通過上述的測試可知,出現了死循環導致的500錯誤。 Nginx官方給出的參考樣例: server { ... ##rewrite指令位于server上下文 rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; ##將/download目錄中包含media目錄下的任意文件請求重定向為donwload/任意/mp3/任意.mp3 rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; ##將/download目錄中包含audio目錄下的任意文件請求重定向為donwload/任意/mp3/任意.mp3 return 403; ... } location /download/ { ##rewrite指令位于location上下文 rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; ##該last標志應該被替換 break,否則nginx將使10個周期返回500個錯誤 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 本地測試(修改本地host文件) # curl http://site1.org <html> ##返回301狀態碼 <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環境測試 通過修改Windows機器Host文件后,添加如下條目 192.168.1.175 centos7-router.com 192.168.1.175 www.centos7-router.com 打開瀏覽器,通過域名的方式進行訪問http://site1.org會自動跳轉到http://www.site1.org(演示略)
四、將http重寫至https
在非全站https時,對于有些敏感的數據需有走https,那也可以通過rewrite方式實現
如下示例,假定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中如何實現rewrite正則匹配重寫”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。