Nginx rewrite地址重寫的詳細(xì)解析
1. 什么是 Rewrite
Rewrite在nginx中也叫URL Rewrite,即URL重寫,就是把傳入Web的請(qǐng)求重定向到其他URL的過(guò)程
從安全角度考慮,使用Rewrite在Nginx中具有一些重要的作用和優(yōu)勢(shì),包括:
隱藏真實(shí)目錄結(jié)構(gòu):
- 使用Rewrite可以隱藏服務(wù)器上的真實(shí)文件路徑和目錄結(jié)構(gòu),防止攻擊者通過(guò)直接訪問(wèn)文件路徑來(lái)獲取敏感信息。這增加了安全性,使攻擊者更難確定服務(wù)器上的實(shí)際文件組織方式。
規(guī)范化URL:
- 強(qiáng)制規(guī)范化URL格式可以避免一些常見的安全問(wèn)題,如路徑遍歷攻擊(Directory Traversal)或路徑參數(shù)欺騙。通過(guò)使用Rewrite,可以確保URL格式的一致性,減少潛在的安全漏洞。
防止盜鏈:
- 通過(guò)Rewrite可以實(shí)施防盜鏈策略,防止其他網(wǎng)站直接鏈接到本站的資源。這有助于減輕服務(wù)器負(fù)載,防止不法分子盜用網(wǎng)站的帶寬,并提高資源的安全性。
HTTP到HTTPS的強(qiáng)制重定向:
- 通過(guò)Rewrite可以實(shí)現(xiàn)將HTTP請(qǐng)求強(qiáng)制重定向到HTTPS,確保數(shù)據(jù)在傳輸過(guò)程中的安全性。這是保障通信安全的一種有效手段,尤其對(duì)于涉及用戶敏感信息的網(wǎng)站至關(guān)重要。
條件性重寫:
- 可以根據(jù)請(qǐng)求中的條件來(lái)選擇是否進(jìn)行重寫,例如,只有特定IP范圍的請(qǐng)求才允許進(jìn)行某種操作。這有助于實(shí)現(xiàn)訪問(wèn)控制和強(qiáng)化安全性。
跨站點(diǎn)腳本(XSS)防護(hù):
- 通過(guò)Rewrite可以對(duì)請(qǐng)求參數(shù)進(jìn)行過(guò)濾或修改,防止惡意用戶通過(guò)注入腳本來(lái)進(jìn)行XSS攻擊。對(duì)URL和參數(shù)進(jìn)行適當(dāng)?shù)闹貙懣梢詼p輕XSS攻擊的風(fēng)險(xiǎn)。
統(tǒng)一資源標(biāo)識(shí)符(URI)規(guī)范化:
- 通過(guò)強(qiáng)制規(guī)范化URI,可以防止攻擊者嘗試混淆或繞過(guò)安全策略。規(guī)范化的URI有助于提高應(yīng)用程序的安全性,防范一些常見的攻擊手法。
避免敏感信息泄露:
- 通過(guò)Rewrite可以限制對(duì)某些敏感信息或文件的訪問(wèn),確保只有授權(quán)用戶能夠獲取特定內(nèi)容。這有助于防止敏感信息泄露和未授權(quán)訪問(wèn)。
2. Rewrite 相關(guān)指令
Nginx Rewrite 相關(guān)指令有 if
、rewrite
、set
、return
2.1. if 語(yǔ)句
應(yīng)用環(huán)境:server,location
語(yǔ)法:
if (condition) { … } if 可以支持如下條件判斷匹配符號(hào) ~ 正則匹配 (區(qū)分大小寫) ~* 正則匹配 (不區(qū)分大小寫) !~ 正則不匹配 (區(qū)分大小寫) !~* 正則不匹配 (不區(qū)分大小寫) -f 和!-f 用來(lái)判斷是否存在文件 -d 和!-d 用來(lái)判斷是否存在目錄 -e 和!-e 用來(lái)判斷是否存在文件或目錄 -x 和!-x 用來(lái)判斷文件是否可執(zhí)行 #在匹配過(guò)程中可以引用一些Nginx的全局變量 $args 請(qǐng)求中的參數(shù); $document_root 針對(duì)當(dāng)前請(qǐng)求的根路徑設(shè)置值; $host 請(qǐng)求信息中的"Host",如果請(qǐng)求中沒(méi)有Host行,則等于設(shè)置的服務(wù)器名; $limit_rate 對(duì)連接速率的限制; $request_method 請(qǐng)求的方法,比如"GET"、"POST"等; $remote_addr 客戶端地址; $remote_port 客戶端端口號(hào); $remote_user 客戶端用戶名,認(rèn)證用; $request_filename 當(dāng)前請(qǐng)求的文件路徑名(帶網(wǎng)站的主目錄/usr/local/nginx/html/images/a.jpg) $request_uri 用于表示客戶端請(qǐng)求的完整 URI,也就是請(qǐng)求地址,它記錄了客戶端發(fā)起的請(qǐng)求地址 $query_string 與$args相同; $scheme 用的協(xié)議,比如http或者是https $server_protocol 請(qǐng)求的協(xié)議版本,"HTTP/1.0"或"HTTP/1.1"; $server_addr 服務(wù)器地址,如果沒(méi)有用listen指明服務(wù)器地址,使用這個(gè)變量將發(fā)起一次系統(tǒng)調(diào)用以取得地址(造成資源浪費(fèi)); $server_name 請(qǐng)求到達(dá)的服務(wù)器名; $document_uri 與$uri一樣,URI地址; $server_port 請(qǐng)求到達(dá)的服務(wù)器端口號(hào);
2.2. Rewrite flag
rewrite 指令根據(jù)表達(dá)式來(lái)重定向URI,或者修改字符串??梢詰?yīng)用于server、ocation、if 環(huán)境下,每行rewrite指令最后跟一個(gè)flag標(biāo)記
支持的flag標(biāo)記有:
last 相當(dāng)于Apache里的[L]標(biāo)記,表示完成rewrite。默認(rèn)為last。(last標(biāo)記位的location,永遠(yuǎn)匹配不上,相當(dāng)于這個(gè)location備注掉了)
break 本條規(guī)則匹配完成后,終止匹配,不再匹配后面的規(guī)則
redirect 返回302臨時(shí)重定向,瀏覽器地址會(huì)顯示跳轉(zhuǎn)后的URL地址
permanent 返回301永久重定向,瀏覽器地址會(huì)顯示跳轉(zhuǎn)后URL地址
redirect 和 permanent 區(qū)別是返回的不同方式的重定向:
- 對(duì)于客戶端來(lái)說(shuō)一般狀態(tài)下是沒(méi)有區(qū)別的。
- 而對(duì)于搜索引擎,相對(duì)來(lái)說(shuō)301的重定向更加友好,如果我們把一個(gè)地址采用301跳轉(zhuǎn)方式跳轉(zhuǎn)的話,搜索引擎會(huì)把老地址的相關(guān)信息帶到新地址,同時(shí)在搜索引擎索引庫(kù)中徹底廢棄掉原先的老地址。
- 使用302重定向時(shí),搜索引擎(特別是google)有時(shí)會(huì)查看跳轉(zhuǎn)前后哪個(gè)網(wǎng)址更直觀,然后決定顯示哪個(gè),如果它覺的跳轉(zhuǎn)前的URL更好的話,也許地址欄不會(huì)發(fā)生更改。
2.3. permanent 匹配示例
所有例子都記得要修改本地解析host文件
例1:http://www.testpm.com/a/1.html ——> http://www.testpm.com/b/2.html
[root@localhost ~]# mkdir /html [root@localhost ~]# cd /html/ [root@localhost html]# mkdir a b [root@localhost html]# ls a b [root@localhost html]# cd a/ [root@localhost a]# echo "this is a" >1.html [root@localhost a]# cd ../b [root@localhost b]# echo "this is b" >2.html? [root@localhost b]# cd /etc/nginx/conf.d/ [root@localhost conf.d]# vim ab.conf server { listen 80; server_name www.testpm.com; location /a { root /html; index 1.html index.htm; rewrite .* /b/2.html permanent; } ? location /b { root /html; index 2.html index.htm; } } [root@localhost conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost conf.d]# systemctl restart nginx ? //本地windows做好host文件的解析 C:\Windows\System32\drivers\etc\hosts 192.168.221.136 www.testpm.com //瀏覽器訪問(wèn): 訪問(wèn) http://www.testpm.com/a 會(huì)跳轉(zhuǎn)到——> http://www.testpm.com/b/2.html
?例2:http://www.testpm.com/2019/a/1.html ==> http://www.testpm.com/2018/a/1.html
[root@localhost ~]# mkdir /var/www/html -p [root@localhost html]# pwd /var/www/html [root@localhost html]# ls 2018 2019 [root@localhost html]# mkdir 2018/a [root@localhost html]# mkdir 2019/a [root@localhost html]# echo "2018" > 2018/a/1.html [root@localhost html]# echo "2019" > 2019/a/1.html [root@localhost conf.d]# vim 20182019.conf server { listen 80; server_name www.erling.com; ? location /2019/a { root /var/www/html; index 1.html index.htm; rewrite ^/2019/(.*)$ /2018/$1 permanent; #(.*)$以任意結(jié)尾;$1就是前面的(.*)位置參數(shù) } ? location /2018/a { root /var/www/html; index 1.html index.htm; } } [root@localhost conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost conf.d]# systemctl restart nginx //本地host文件添加解析 192.168.221.136 www.erling.com ?//瀏覽器訪問(wèn): 訪問(wèn) http://www.erling.com/2019/a 會(huì)跳轉(zhuǎn)到——> http://www.erling.com/2018/a/
?例3:http://www.xf.com ==> http://jd.com
[root@localhost conf.d]# vim xf-jd.conf server{ listen 80; server_name www.xf.com; ? location / { root /html; if ($host ~* www.xf.com) { rewrite .* https://www.jd.com permanent; #永久重定向到j(luò)d.com } } } ? 參數(shù)解釋: $host ~* www.xf.com $主機(jī)名,~*正則匹配不區(qū)分大小寫 //本地host文件添加解析 192.168.221.136 www.xf.com ?//瀏覽器訪問(wèn) 訪問(wèn) www.xf.com 會(huì)跳轉(zhuǎn)到——> https://www.jd.com/
例4:http://www.xf-html.com/a/1.html ==> http://jd.com/a/1.html
[root@localhost conf.d]# cp xf-jd.conf xf-html-jd.conf [root@localhost conf.d]# vim xf-html-jd.conf server{ listen 80; server_name www.xf-html.com; ? location /a { root /html; index 1.html index.htm; if ($host ~* www.xf-html.com) { rewrite .* https://www.jd.com$request_uri permanent; } } } //本地host文件添加解析 192.168.221.136 www.xf-html.com ?//瀏覽器訪問(wèn) 訪問(wèn) www.xf-html.com/a/1.html 會(huì)跳轉(zhuǎn)到——> https://www.jd.com/a/1.html(由于該頁(yè)面沒(méi)有,所以會(huì)跳轉(zhuǎn)到https://www.jd.com/error2.aspx)
例5: 在訪問(wèn)目錄后添加/ (如果目錄后已有/,則不加/)
[root@localhost conf.d]# mkdir -p /usr/share/nginx/html/a/b/c [root@localhost conf.d]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/a/b/c/ [root@localhost conf.d]# vim root.conf server { listen 80; server_name www.xfroot.com; ? location /a/b/c { root /usr/share/nginx/html; index index.html index.htm; if (-d request_filename) { #判斷目錄 rewrite ^(.*)([^/])$ http://$1$2/ permanent; #以任意字符開頭,不以/結(jié)尾——>跳轉(zhuǎn)到前面一樣的路徑,但是在最后加上/ } } } 參數(shù)解釋: http://www.xf.com/a/b/c $1: /a/b/ $2: c http://$host$1$2/ ? [root@localhost conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost conf.d]# systemctl restart nginx ?//本地host文件添加解析 192.168.221.136 www.xfroot.com ?//瀏覽器訪問(wèn) 訪問(wèn) http://www.xfroot.com/a/b/c 會(huì)跳轉(zhuǎn)到——> http://www.xfroot.com/a/b/c/
例6:重定向后,在最后添加一個(gè)user=
http://www.xf.com/login/xf.html ==> http://www.xf.com/reg/login.html?user=xf
[root@localhost conf.d]# vim user.conf server{ listen 80; server_name www.xfuser.com; ? location /login{ #匹配到login root /usr/share/nginx/html; index index.html index.htm; rewrite ^/login/(.*)\.html$ http://$host/reg/login.html?user=$1; #以login開頭的,以任意字符串.html結(jié)尾的 重定向到http://前面的主機(jī)名/reg/login.html?user=主機(jī)名,$1是一個(gè)位置參數(shù) } ? location /reg{ #匹配reg root /usr/share/nginx/html; index login.html index.htm; } } [root@localhost html]# pwd /usr/share/nginx/html [root@localhost html]# mkdir reg [root@localhost html]# echo "login" > reg/login.html [root@localhost html]# mkdir login [root@localhost html]# cp index.html login/ //本地host文件添加解析 192.168.221.136 www.xfuser.com ?//瀏覽器訪問(wèn) 訪問(wèn) http://www.xfuser.com/login/index.html 會(huì)跳轉(zhuǎn)到——> http://www.xfuser.com/reg/login.html?user=index
user=$1,$1對(duì)應(yīng)的是index,為什么是index而不是login呢?
因?yàn)椋?1匹配的是正則中的第一個(gè)子匹配部分
^/login/ 匹配/login/
(.*) 匹配index
.html$ 匹配.html
$1是匹配正則中第一個(gè)子匹配的,所以就是index
例7:數(shù)字重定向成 數(shù)字/
http://www.xf.com/xf/11-22-33/1.html ==> http://www.xf.com/xf/11/22/33/1.html
[root@localhost conf.d]# pwd /etc/nginx/conf.d [root@localhost conf.d]# vim int.conf server { listen 80; server_name www.xfint.com; ? location /xf{ root /html; rewrite ^/xf/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /xf/$1/$2/$3$4 permanent; //匹配以xf開頭/0-9出現(xiàn)一個(gè)或者多個(gè),匹配三次,最后以任意字符結(jié)尾 ——>永久重定向到/xf開頭/前面的1234位置參數(shù),因?yàn)榍懊娴?*已經(jīng)包含了/所以重定向的時(shí)候就不用寫/了 } ? location /xf/11/22/33{ root /html; index 1.html index.htm; } } [root@localhost html]# cd /html/ [root@localhost html]# mkdir -p xf/11/22/33 [root@localhost html]# echo "hello world" > xf/11/22/33/1.html ?//本地host文件添加解析 192.168.221.136 www.xfint.com ?//瀏覽器訪問(wèn) 訪問(wèn) www.xfint.com/xf/11-22-33/1.html 會(huì)跳轉(zhuǎn)到 http://www.xfint.com/xf/11/22/33/1.html最終顯示hello world
2.4. set 指令
set 指令是用于定義一個(gè)變量,并且賦值
應(yīng)用范圍:server、location、if
應(yīng)用示例
例8:http://alice.testpm.com ==> http://www.testpm.com/alice
http://jack.testpm.com ==> http://www.testpm.com/jack
[root@localhost conf.d]# cd /usr/share/nginx/html/ [root@localhost html]# mkdir jack alice [root@localhost html]# echo "jack.." >> jack/index.html [root@localhost html]# echo "alice.." >> alice/index.html ? ?//編輯配置文件: [root@localhost conf.d]# vim alice_jack.conf server { listen 80; server_name www.testpm.com; ? location / { root /usr/share/nginx/html; index index.html index.htm; if ( $host ~* ^www.testpm.com$) { break; } if ( $host ~* "^(.*)\.testpm\.com$" ) { set $user $1; rewrite .* http://www.testpm.com/$user permanent; } } location /jack { root /usr/share/nginx/html; index index.html index.hml; } location /alice { root /usr/share/nginx/html; index index.html index.hml; } } [root@localhost conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost conf.d]# systemctl restart nginx ? 本地host文件添加解析 192.168.221.136 www.testpm.com 192.168.221.136 jack.testpm.com 192.168.221.136 alice.testpm.com ? 瀏覽器訪問(wèn) 訪問(wèn)www.testpm.com時(shí),會(huì)出現(xiàn)nginx的歡迎界面 訪問(wèn) jack.testpm.com 會(huì)跳轉(zhuǎn)到 http://www.testom.com/jack 訪問(wèn) alice.testpm.com 會(huì)跳轉(zhuǎn)到 http://www.testom.com/alice
2.5. return 指令
return 指令用于返回狀態(tài)碼給客戶端
應(yīng)用范圍:server、location、if
應(yīng)用示例:
例9:如果訪問(wèn)的.sh結(jié)尾的文件則返回403操作拒絕錯(cuò)誤
[root@localhost conf.d]# vim return.conf server { listen 80; server_name www.testpmxf.cn; #access_log /var/log/nginx/http_access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location ~* \.sh$ { return 403; } } ?//本地host文件添加解析 192.168.221.136 www.testpmxf.cn ?//瀏覽器訪問(wèn) http://www.testpmxf.cn/test.sh 會(huì)報(bào)403 Forbidden
例10:80端口 轉(zhuǎn) 443端口(需要有ssl證書才能行)?
server { listen 80; server_name www.testpm.cn; access_log /var/log/nginx/http_access.log main; return 301 https://www.testpm.cn$request_uri; #返回301永久重定向 } server { listen 443 ssl; server_name www.testpm.cn; access_log /var/log/nginx/https_access.log main; ? #ssl on; ssl_certificate /etc/nginx/cert/2447549_www.testpm.cn.pem; ssl_certificate_key /etc/nginx/cert/2447549_www.testpm.cn.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; ? location / { root /usr/share/nginx/html; index index.html index.htm; } }
[root@localhost ~]# curl -I http://www.testpm.cn HTTP/1.1 301 Moved Permanently Server: nginx/1.16.0 Date: Wed, 03 Jul 2019 13:52:30 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive Location: https://www.testpm.cn/
2.6. last、break詳解
- last:停止匹配當(dāng)前所在的location,繼續(xù)匹配下一組location。
- ?break:終止nginx在當(dāng)前請(qǐng)求中的所有l(wèi)ocation的匹配,直接使用當(dāng)前l(fā)ocation的配置當(dāng)作最終的處理請(qǐng)求,不會(huì)繼續(xù)往下進(jìn)行匹配。
- ?使用 proxy_pass 指令時(shí),盡量使用break
[root@localhost ~]# vim /etc/nginx/conf.d/last_break.conf server { listen 80; server_name www.testlast_break.com; access_log /var/log/nginx/last.access.log main; ? location / { root /usr/share/nginx/html; index index.html index.htm; } location /break/ { root /usr/share/nginx/html; rewrite .* /test/break.html break; } location /last/ { root /usr/share/nginx/html; rewrite .* /test/last.html last; } location /test/ { root /usr/share/nginx/html; rewrite .* /test/test.html break; } } [root@localhost ~]# cd /usr/share/nginx/html/ [root@localhost html]# mkdir last break test [root@localhost html]# echo "last" > test/last.html [root@localhost html]# echo "break" > test/break.html [root@localhost html]# echo "test" > test/test.html
瀏覽器訪問(wèn)
http://www.testlast_break.com/break/ ——> 會(huì)被重定向到break.html,會(huì)顯示break
http://www.testlast_break.com/last/ ——> 會(huì)被重定向到last.html,跳出本個(gè)location匹配,匹配下一組location,所以最終會(huì)顯示test
http://www.testlast_break.com/test/ ——> 會(huì)被重定向到test.html,顯示test,然后終止后續(xù)的匹配
拓展:當(dāng)last后面沒(méi)有l(wèi)ocation資源匹配時(shí),會(huì)last自己location的資源進(jìn)行匹配,也就是會(huì)訪問(wèn)到http://www.testlast_break.com/last/last.index;可以將實(shí)驗(yàn)中test的location資源注釋掉驗(yàn)證效果。
2.7. Nginx https rewrite(擴(kuò)展)
(證書購(gòu)買官網(wǎng)有標(biāo)準(zhǔn)配置教程)
server { listen 80; server_name *.vip9999.top vip9999.top; if ($host ~* "^www.vip9999.top$|^vip9999.top$" ) { return 301 https://www.vip9999.top$request_uri; } } ? # Settings for a TLS enabled server. server { listen 443 ssl; server_name www.vip9999.top; ssl_certificate cert/214025315060640.pem; ssl_certificate_key cert/214025315060640.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ? #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
到此這篇關(guān)于Nginx rewrite地址重寫的詳細(xì)解析的文章就介紹到這了,更多相關(guān)Nginx rewrite地址重寫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx反向代理proxy_pass遇到的死循環(huán)問(wèn)題
這篇文章主要介紹了nginx反向代理proxy_pass遇到的死循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01CentOS7安裝Nginx并配置自動(dòng)啟動(dòng)的方法步驟
這篇文章主要介紹了CentOS7安裝Nginx并配置自動(dòng)啟動(dòng)的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Nginx?請(qǐng)求超時(shí)的實(shí)現(xiàn)
Nginx請(qǐng)求超時(shí)是服務(wù)器無(wú)法在規(guī)定時(shí)間內(nèi)完成對(duì)客戶端請(qǐng)求的響應(yīng),本文就來(lái)介紹一下Nginx?請(qǐng)求超時(shí)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02windows系統(tǒng)下安裝Nginx及簡(jiǎn)單使用過(guò)程
Nginx是一個(gè)很強(qiáng)大的高性能Web和反向代理服務(wù),也是一種輕量級(jí)的Web服務(wù)器,可以作為獨(dú)立的服務(wù)器部署網(wǎng)站,應(yīng)用非常廣泛,特別是現(xiàn)在前后端分離的情況下,這篇文章主要介紹了windows系統(tǒng)下安裝Nginx以及簡(jiǎn)單使用,需要的朋友可以參考下2024-04-04nginx實(shí)現(xiàn)指定url轉(zhuǎn)發(fā)詳解
這篇文章主要為大家詳細(xì)介紹了nginx實(shí)現(xiàn)指定url轉(zhuǎn)發(fā)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11