目錄
一、location
1.location 匹配規(guī)則介紹
2.?實(shí)際網(wǎng)站使用中匹配規(guī)則
2.1第一個(gè)必選規(guī)則
2.2第二個(gè)必選規(guī)則是處理靜態(tài)文件請(qǐng)求,這是nginx作為http服務(wù)器的強(qiáng)項(xiàng)
2.3第三個(gè)規(guī)則就是通用規(guī)則
3.location 匹配規(guī)則演示
2.1一般前綴匹配
2.2正則匹配
2.3正則前綴匹配
2.4精準(zhǔn)匹配
二、rewrite
1.rewrite介紹
1.1rewrite功能?
1.2rewrite重寫跳轉(zhuǎn)?
2.rewrite命令的執(zhí)行
2.1rewrite執(zhí)行順序?
2.2rewrite語法格式
3.rewrite 示例
3.1基于域名的跳轉(zhuǎn)
3.2基于客戶端IP訪問跳轉(zhuǎn)
3.3?基于舊域名跳轉(zhuǎn)到新域名后面加目錄
3.4?基于參數(shù)匹配的跳轉(zhuǎn)
一、location
1.location 匹配規(guī)則介紹
location 匹配規(guī)則分類:
- 精準(zhǔn)匹配:location = / { ……}
- 一般匹配:location / {……}
- 正則匹配:location ~ / {……}
location 常用匹配規(guī)則:
- =:進(jìn)行普通字符精準(zhǔn)匹配,也就是完全匹配即匹配路徑與訪問路徑完全一致。
- ^~:表示普通字符匹配。使用前綴匹配,如果匹配成功,則不再匹配其它正則匹配 location
- ~:區(qū)分大小寫的匹配。
- ~*:不區(qū)分大小寫的匹配。
- !~:區(qū)分大小寫的匹配取非。
- !~*:不區(qū)分大小寫的匹配取非。?
location 優(yōu)先級(jí):
- 首先精準(zhǔn)匹配 =
- 其次前綴匹配 ^~
- 其次是按文件中順序的正則 ~ 或~*,正則匹配到以后,不再向下匹配
- 然后匹配不帶任何修飾符的一般前綴匹配
- 最后是交給 / 通用匹配?
總結(jié):
- 精準(zhǔn)匹配= > 正則前綴匹配^~ > 正則匹配 > 一般前綴匹配 > 通用匹配?
- 正則匹配:一旦匹配到,則不再向下匹配
location 示例說明:
- location = / {}:=為精確匹配 / ,主機(jī)名后面不能帶任何字符串,比如訪問 / 和 /data,則 / 匹配,/data 不匹配;再比如?location = /abc,則只匹配/abc ,/abc/或 /abcd不匹配。
- location / {}:因?yàn)樗械牡刂范家?/ 開頭,所以這條規(guī)則將匹配到所有請(qǐng)求 比如訪問 / 和 /data, 則 / 匹配, /data 也匹配,但后面前綴路徑會(huì)和最長(zhǎng)字符串優(yōu)先匹配(最長(zhǎng)匹配)
- location /documents/ {}:匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續(xù)往下搜索其它 location;只有其它 location后面的前綴路徑?jīng)]有匹配到時(shí),才會(huì)采用這一條
- location /documents/abc {}:匹配任何以 /documents/abc 開頭的地址,匹配符合以后,還要繼續(xù)往下搜索其它 location;只有其它 location后面的前綴路徑?jīng)]有匹配到時(shí),才會(huì)采用這一條
- location ^~ /images/ {}:匹配任何以 /images/ 開頭的地址,匹配符合以后,停止往下搜索正則,采用這一條
- location ~* \.(gif|jpg|jpeg)$ {}:匹配所有以 gif、jpg或jpeg 結(jié)尾的請(qǐng)求。然而,所有請(qǐng)求 /images/ 下的圖片會(huì)被 location ^~ /images/ 處理,因?yàn)?^~ 的優(yōu)先級(jí)更高,所以到達(dá)不了這一條正則
- location /images/abc {}:最長(zhǎng)字符匹配到 /images/abc,優(yōu)先級(jí)最低,繼續(xù)往下搜索其它 location,會(huì)發(fā)現(xiàn) ^~ 和 ~ 存在
- location ~ /images/abc {}:匹配以/images/abc 開頭的,優(yōu)先級(jí)次之,只有去掉 location ^~ /images/ 才會(huì)采用這一條
- location /images/abc/1.html {}:匹配/images/abc/1.html 文件,如果和正則location ~ /images/abc/1.html 相比,正則優(yōu)先級(jí)更高
2.?實(shí)際網(wǎng)站使用中匹配規(guī)則
2.1第一個(gè)必選規(guī)則
直接匹配網(wǎng)站根目錄首頁,通過域名訪問網(wǎng)站首頁比較頻繁,使用這個(gè)會(huì)加速處理,比如說官網(wǎng)。
可以是一個(gè)靜態(tài)首頁,也可以直接轉(zhuǎn)發(fā)給后端應(yīng)用服務(wù)器
location = /index.html {
????root ? html;
?? ?index ?index.html index.htm;
}
2.2第二個(gè)必選規(guī)則是處理靜態(tài)文件請(qǐng)求,這是nginx作為http服務(wù)器的強(qiáng)項(xiàng)
有兩種配置模式,目錄匹配或后綴匹配,任選其一或搭配使用
location ^~ /static/ {
????root /webroot/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
????root /webroot/res/;
}
2.3第三個(gè)規(guī)則就是通用規(guī)則
比如用來轉(zhuǎn)發(fā)帶.php、.jsp后綴的動(dòng)態(tài)請(qǐng)求到后端應(yīng)用服務(wù)器
非靜態(tài)文件請(qǐng)求就默認(rèn)是動(dòng)態(tài)請(qǐng)求
location / {
????proxy_pass http://tomcat_server;
}
3.location 匹配規(guī)則演示
2.1一般前綴匹配
#將原本配置文件備份
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
#還原配置文件
mv /usr/local/nginx/conf/nginx.conf.default /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
location / { #通用配置
root html;
index index.html index.htm;
}
location /abc { #一般前綴匹配
root /var/www;
}
location /abc/test { #一般前綴匹配
root /var/data;
}
systemctl restart nginx.service
#匹配到 /abc 則訪問結(jié)果:this is test web
mkdir -p /var/www/abc/test
echo '<h1>this is test web</h1>' > 123.html
#匹配到 /abc/test 則訪問結(jié)果:welcome to test web
mkdir -p /var/data/abc/test
echo '<h1>welcome to test web</h1>' > 123.html
瀏覽器訪問驗(yàn)證:
http://192.168.88.40/abc/test/123.html
?
2.2正則匹配
vim /usr/local/nginx/conf/nginx.conf
location / { #通用配置
root html;
index index.html index.htm;
}
location ~ ^/abc { #正則匹配
root /var/www;
}
location ~ ^/abc/test { #正則匹配
root /var/data;
}
systemctl restart nginx.service
#匹配到 /abc 則訪問結(jié)果:this is test web
mkdir -p /var/www/abc/test
echo '<h1>this is test web</h1>' > 123.html
#匹配到 /abc/test 則訪問結(jié)果:welcome to test web
mkdir -p /var/data/abc/test
echo '<h1>welcome to test web</h1>' > 123.html
瀏覽器訪問驗(yàn)證:
http://192.168.88.40/abc/test/123.html
?
2.3正則前綴匹配
vim /usr/local/nginx/conf/nginx.conf
location / { #通用配置
root html;
index index.html index.htm;
}
location ~ ^/abc { #正則匹配
root /var/www;
}
location ^~ /abc/test { #正則前綴匹配
root /var/data;
}
systemctl restart nginx.service
#匹配到 /abc 則訪問結(jié)果:this is test web
mkdir -p /var/www/abc/test
echo '<h1>this is test web</h1>' > 123.html
#匹配到 /abc/test 則訪問結(jié)果:welcome to test web
mkdir -p /var/data/abc/test
echo '<h1>welcome to test web</h1>' > 123.html
瀏覽器訪問驗(yàn)證:
http://192.168.88.40/abc/test/123.html
?
2.4精準(zhǔn)匹配
vim /usr/local/nginx/conf/nginx.conf
location / { #通用配置
root html;
index index.html index.htm;
}
location ~ ^/abc { #正則匹配
root /var/www;
}
location ^~ /abc/test { #正則前綴匹配
root /var/data;
}
location = /abc/test/123.html { #精準(zhǔn)匹配
root html;
}
systemctl restart nginx.service
#匹配到 /abc 則訪問結(jié)果:this is test web
mkdir -p /var/www/abc/test
echo '<h1>this is test web</h1>' > 123.html
#匹配到 /abc/test 則訪問結(jié)果:welcome to test web
mkdir -p /var/data/abc/test
echo '<h1>welcome to test web</h1>' > 123.html
#匹配到 /abc/test/123.html 則訪問結(jié)果:hello, test web
mkdir -p /usr/local/nginx/html/abc/test
echo '<h1>hello, test web</h1>' > 123.html
瀏覽器訪問驗(yàn)證:
http://192.168.88.40/abc/test/123.html
?
二、rewrite
1.rewrite介紹
1.1rewrite功能?
rewrite功能:使用nginx提供的全局變量或自己設(shè)置的變量,結(jié)合正則表達(dá)式和標(biāo)記位實(shí)現(xiàn)URL重寫以及重定向。比如:更換域名后需要保持舊的域名能跳轉(zhuǎn)到新的域名上、某網(wǎng)頁發(fā)生改變需要跳轉(zhuǎn)到新的頁面、網(wǎng)站防盜鏈等需求。
rewrite 與 location 的區(qū)別:
- rewrite:對(duì)訪問的域名或域名內(nèi)的URL路徑地址重寫
- location:對(duì)訪問的路徑做控制或代理轉(zhuǎn)發(fā)
- 從功能看,rewrite 和 location 似乎有點(diǎn)像,都能實(shí)現(xiàn)跳轉(zhuǎn),主要區(qū)別在于 rewrite 是在同一域名內(nèi)更改獲取資源的路徑,而 location 是對(duì)一類路徑做控制訪問或反向代理,還可以proxy_pass 到其他機(jī)器。
1.2rewrite重寫跳轉(zhuǎn)?
rewrite位置:?rewrite只能放在server{},location{},if{}中,并且默認(rèn)只能對(duì)域名后邊的除去傳遞的參數(shù)外的字符串起作用。
例如?http://www.kgc.com/abc/bbs/index.php?a=1&b=2?只對(duì)/abc/bbs/index.php重寫。
rewrite跳轉(zhuǎn)實(shí)現(xiàn):
- Nginx:通過ngx_http_rewrite_module 模塊支持URL重寫、支持if條件判斷,但不支持else
- 跳轉(zhuǎn):從一個(gè) location跳轉(zhuǎn)到另一個(gè)location,循環(huán)最多可以執(zhí)行10次,超過后nginx將返回500錯(cuò)誤
- PCRE支持:perl兼容正則表達(dá)式的語法規(guī)則匹配
- 重寫模塊 set 指令:創(chuàng)建新的變量并設(shè)其值
2.rewrite命令的執(zhí)行
2.1rewrite執(zhí)行順序?
rewrite 執(zhí)行順序如下:
- 執(zhí)行 server 塊里面的 rewrite 指令。
- 執(zhí)行 location 匹配。
- 執(zhí)行選定的 location 中的 rewrite 指令。?
2.2rewrite語法格式
語法格式:rewrite <regex> <replacement> [flag];
regex :表示正則匹配規(guī)則。
replacement :表示跳轉(zhuǎn)后的內(nèi)容。
flag :表示 rewrite 支持的 flag 標(biāo)記。
#flag標(biāo)記說明
last :本條規(guī)則匹配完成后,不終止重寫后的url匹配,一般用在 server 和 if 中。
break :本條規(guī)則匹配完成即終止,終止重寫后的url匹配,一般使用在 location 中。
redirect :返回302臨時(shí)重定向,瀏覽器地址會(huì)顯示跳轉(zhuǎn)后的URL地址。
permanent :返回301永久重定向,瀏覽器地址欄會(huì)顯示跳轉(zhuǎn)后的URL地址。
3.rewrite 示例
3.1基于域名的跳轉(zhuǎn)
現(xiàn)在公司舊域名www.kgc.com有業(yè)務(wù)需求變更,需要使用新域名www.benet.com代替,但是舊域名不能廢除,需要跳轉(zhuǎn)到新域名上,而且后面的參數(shù)保持不變。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com;
# 添加域名重定向
if ($host = 'www.kgc.com' ) {
#$host為rewrite全局變量,代表請(qǐng)求主機(jī)頭字段或主機(jī)名
rewrite ^/(.*)$ http://www.benet.com/$1 permanent;
# $1:為正則匹配的內(nèi)容,即“域名/”之后的字符串
}
location / {
root html;
index index.html index.htm;
}
}
cd /usr/local/nginx/html
echo '<h1>this is test web</h1>' > test.html
echo "192.168.88.40 www.kgc.com www.benet.com" >> /etc/hosts
systemctl restart nginx
瀏覽器訪問驗(yàn)證:
http:www.kgc.com/test.html 會(huì)跳轉(zhuǎn)到 www.benet.com/test.html
?
?
3.2基于客戶端IP訪問跳轉(zhuǎn)
今天公司業(yè)務(wù)新版本上線,要求所有 IP 訪問任何內(nèi)容都顯示一個(gè)固定維護(hù)頁面,只有公司 IP :192.168.88.40訪問正常。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com;
#設(shè)置是否合法的IP標(biāo)記
set $rewrite true; #設(shè)置變量$rewrite,變量值為boole值true
#判斷是否為合法IP
if ( $remote_addr = "192.168.88.40") { #當(dāng)客戶端IP為192.168.88.40時(shí),將變量值設(shè)為false,不進(jìn)行重寫
set $rewrite false;
}
#除了合法IP,其它都是非法IP,進(jìn)行重寫跳轉(zhuǎn)維護(hù)頁面
if ($rewrite = true) { #當(dāng)變量值為true時(shí),進(jìn)行重寫
rewrite ^/ /weihu.html; #將域名后邊的路徑重寫成/weihu.html后轉(zhuǎn)發(fā),例如www.kgc.com/weihu.html
}
#網(wǎng)頁返回/var/www/html/weihu.html的內(nèi)容
location = /weihu.html {
root /var/www;
}
location / {
root html;
index index.html index.htm;
}
}
cd /usr/local/nginx/html
echo '<h1>the web is updating!</h1>' > weihu.html
systemctl restart nginx.service
瀏覽器訪問驗(yàn)證:
只有 IP 為 192.168.88.40 能正常訪問,其它地址都是維護(hù)頁面
###注意事項(xiàng)
如果rewrite ^/ /weihu.html; 換成rewrite ^/ /weihu.html permanent; 的話,
若不是 192.168.80.10 的主機(jī)訪問會(huì)使瀏覽器修改請(qǐng)求訪問的 URL 成 http://www.kgc.com/weihu.html 再請(qǐng)求訪問,
這樣就會(huì)進(jìn)入一直在 rewrite 的死循環(huán),訪問請(qǐng)求會(huì)一直被重寫成 http://www.kgc.com/weihu.html 再請(qǐng)求訪問
?
3.3?基于舊域名跳轉(zhuǎn)到新域名后面加目錄
現(xiàn)在訪問的是 http://bbs.kgc.com/post/,現(xiàn)在需要將這個(gè)域名下面的訪問都跳轉(zhuǎn)到http://www.kgc.com/bbs/post/
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name bbs.kgc.com www.kgc.com;
location /bbs {
root /var/www;
}
location /post {
rewrite (.+) http://www.kgc.com/bbs$1 permanent;
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /var/www/bbs/post
cd /var/www/bbs/post
echo '<h1>hello,world!</h1>' > 123.html
echo '192.168.88.40 bbs.kgc.com' >> /etc/hosts
systemctl restart nginx.service
使用瀏覽器訪問 http://bbs.kgc.com/post/123.html 跳轉(zhuǎn)到 http://www.kgc.com/bbs/post/123.html
?
?
3.4?基于參數(shù)匹配的跳轉(zhuǎn)
現(xiàn)在訪問http://www.kgc.com/100-(100|200)-100.html 跳轉(zhuǎn)到http://www.kgc.com頁面。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.+) http://www.kgc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
systemctl restart nginx.service
使用瀏覽器訪問 http://www.kgc.com/100-200-100.html 或 http://www.kgc.com/100-100-100.html 跳轉(zhuǎn)到http://www.kgc.com頁面。
- $request_uri:包含請(qǐng)求參數(shù)的原始URI,不包含主機(jī)名,如:http://www.kgc.com/abc/bbs/index.html?a=1&b=2 中的 /abc/bbs/index.php?a=1&b=2
- $uri:這個(gè)變量指當(dāng)前的請(qǐng)求URI,不包括任何參數(shù),如:/abc/bbs/index.html
- $document_uri:與$uri相同,這個(gè)變量指當(dāng)前的請(qǐng)求URI,不包括任何傳遞參數(shù),如:/abc/bbs/index.html?
?文章來源:http://www.zghlxwxcb.cn/news/detail-471323.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-471323.html
到了這里,關(guān)于Nginx rewrite的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!