Nginx的rewrite功能支持正則匹配重寫,即將URL地址臨時或永久重新指向某個新的位置,類似于重定向。這個特性有利用當(dāng)網(wǎng)站結(jié)構(gòu)做出重大調(diào)整,如之前的網(wǎng)站mp3資源使用URL為www.site1.org/mp3進(jìn)行訪問,而現(xiàn)在服務(wù)器上mp3目錄已經(jīng)被使用music目錄替換,那rewrite這個功能則能夠輕松實(shí)現(xiàn)。其次如可以將site1.org強(qiáng)制調(diào)整到www.site1.org,反之亦可。這個指令位于ngx_http_rewrite_module模塊。本文主要描述這個指令的用法并給出演示。
一、rewrite指令語法描述
句法: rewrite regex replacement [flag];
默認(rèn): -
語境: server,location,if
如果指定的正則表達(dá)式與請求URI匹配,則URI將按照replacement字符串中的指定進(jìn)行更改。
該rewrite指令在其在配置文件中出現(xiàn)的順序順序地執(zhí)行。可以使用標(biāo)志終止對偽指令的進(jìn)一步處理。
如果替換字符串以“ http://”,“ https://”或“ $scheme” 開頭,則處理停止,并將重定向返回給客戶端。
flag標(biāo)志的作用是用于控制當(dāng)匹配到對應(yīng)的rewrite規(guī)則后是否繼續(xù)檢查后續(xù)的rewrite規(guī)則
可選flag參數(shù)可以是以下之一:
last
一旦被當(dāng)前規(guī)則匹配并重寫后立即停止檢查后續(xù)的其它rewrite的規(guī)則,而后通過重寫后的規(guī)則重新發(fā)起請求;
break
一旦被當(dāng)前規(guī)則匹配并重寫后立即停止后續(xù)的其它rewrite的規(guī)則,而后繼續(xù)由nginx進(jìn)行后續(xù)操作;
redirect
如果替換字符串不以“ http://”,“ https://”或“ $scheme” 開頭,則使用,返回302臨時重定向;
permanent
返回301永久重定向;
注意:一般將rewrite寫在location中時都使用break標(biāo)志,或者將rewrite寫在if上下文中;
其他指令
rewrite_log on|off
是否把重寫過程記錄在錯誤日志中;默認(rèn)為notice級別;默認(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 http://www.site1.org/images/Waves.jpg
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 http://www.site1.org/images/Waves.jpg
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 http://www.site1.org/images/Waves.jpg
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 http://www.site1.org/imgs/Waves.jpg ##這種方式可以訪問
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錯誤
再次對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 http://www.site1.org/imgs/Waves.jpg
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 http://www.site1.org/images/Waves.jpg
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
通過上述的測試可知,出現(xiàn)了死循環(huán)導(dǎo)致的500錯誤。
Nginx官方給出的參考樣例:
server {
... ##rewrite指令位于server上下文
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
##將/download目錄中包含media目錄下的任意文件請求重定向?yàn)閐onwload/任意/mp3/任意.mp3
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;
##將/download目錄中包含audio目錄下的任意文件請求重定向?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個周期返回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狀態(tài)碼
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
Windows環(huán)境測試
通過修改Windows機(jī)器Host文件后,添加如下條目
192.168.1.175 centos7-router.com
192.168.1.175 www.centos7-router.com
打開瀏覽器,通過域名的方式進(jìn)行訪問http://site1.org會自動跳轉(zhuǎn)到http://www.site1.org(演示略)
四、將http重寫至https
在非全站https時,對于有些敏感的數(shù)據(jù)需有走h(yuǎn)ttps,那也可以通過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;
}
上述演示略
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。