匹配規則
- 前言
- 一. nginx location
- 1.語法規則和優先級
- 2.匹配順序
- 二. nginx rewrite
- 案例1:rewrite url實作跳轉
- 案例2:rewrite中使用正則
- 案例3:主機名重定向
- 案例4:域名重定向
- 案例5:php網站登錄跳轉
- 案例6:http://alice.liang.com ==> http://www.liang.com/alice
- 案例7:訪問的.sh結尾的檔案則回傳403操作拒絕錯誤
- 案例8:http 轉換 https
- 案例9:last、break、redirect、permanent標記
前言
nginx中強大的功能,,如rewrite、proxy_pass都離不開他的匹配規則,搞懂nginx,學會高級的nginx用法,各種匹配優先級、匹配語法一定要知道
一. nginx location
1.語法規則和優先級
location [=|~|~*|!~|!~*|^~] /uri/ {
...
}
= > ^~ > ~|~*|!~|!*~** > /
精確匹配 > 字符開頭 > 正則匹配 > 通配
server {
listen 192.168.93.136;
root /abcd;
index index.html;
location = / { index a.html; }
location ~ / { index b.html; }
location / { index c.html; }
}
# 測驗頁面 根據頁面的不同內容來觀察優先級
2.匹配順序
除了優先級以外,還有一種少見的根據優先匹配順序來決定使用哪個匹配,舉個例子

圖示:根據不同的匹配轉發到不同的tomcat應用,都是用的~正則匹配
此時有一個url,如:http://ip:port/gwaf/report,可以看到,這個url同時滿足兩個匹配,那么該請求是轉發到server2,還是server3呢,
答案是server2,同級別匹配規則,走先匹配到的匹配規則,從組態檔角度來看,就是從上到下的順序,這里也就是匹配到了 ~ /(console|report),所以轉發到server2,
那么就會出現一個問題,如若這個url是請求的server3里面的介面,應該轉發到server3處理的,但卻轉發到server2,這時nginx會回傳404,因為server2處理不了這個介面,這里有兩種方法:可以嘗試將~ /gwaf匹配寫到~ /(console|report) 匹配的前面,讓~ /gwaf 優先匹配,轉發到server3,或者使用^~ /gwaf ,使用開頭匹配增加匹配優先級,
二. nginx rewrite
rewrite 將用戶訪問的url 重定向到一個新的url
下面通過幾個rewrite匹配案例來看一下
案例1:rewrite url實作跳轉
當訪問http://192.168.93.136/aaa/a/1.html 時,重定向至http://192.168.93.136/ccc/bbb/1.html
mkdir -p /usr/share/nginx/html/ccc/bbb
mkdir -p /usr/share/nginx/html/aaa/a
vim /usr/share/nginx/html/aaa/a/1.html
vim /usr/share/nginx/html/ccc/bbb/1.html
vim /etc/nginx/conf.d/default.conf
location /aaa {
rewrite .* /ccc/bbb/1.html permanent; # url含有/aaa就會被重定向到 /ccc/bbb/1.html
}
測驗頁面 http://192.168.100.10/aaa/a/1.html 訪問的url中含有"/aaa"欄位就可以
permanent引數:
如果用了permanent 會直接顯示新的url 新的網頁內容,如果不使用 還是顯示原來的url但是頁面內容還是會顯示轉發后的,
以下匹配方法是否可行:
location = /aaa http://192.168.100.10/aaa/123.html是否可行
不可行,因為需要完全匹配
location ~ /aaa http://192.168.100.10/aaa/123.html是否可行
可行,因為部分匹配即可
location ^~ /aaa http://192.168.100.10/aaa/123.html是否可行
可行,因為部分匹配即可
案例2:rewrite中使用正則
將http://192.168.93.136/2017/a/1.html 換http://192.168.93.136/2018/a/2.html
mkdir -p 2018/a/
echo '2018' 2018/a/1.html
mkdir -p 2019/a
echo '2019' 2019/a/2.html
vim /etc/nginx/conf.d/default.conf
location /2018{
rewrite ^/2018/(.*)$ /2019/$1 permanent; # $1是指前面()里的內容,和shell的正則一樣
}
訪問測驗 http://192.168.93.136/2018/a/1.html
案例3:主機名重定向
將http://liang.com 換http://kong.com
vim /etc/nginx/conf.d/default.conf
if ( $host ~* liang.com ) { # $host nginx內置變數
rewrite .* http://kong.com permanent;
}
訪問測驗 訪問liang.com
案例4:域名重定向
將http://web.liang.com/a/1.html 換成 http://kong.com/a/1.html
建立兩個虛擬主機 分別進行域名決議 創建2個主機的主頁內容
echo 'web.liang' >a/1.html
echo 'kong' >a/1.html
if ( $host ~* liang.com ) {
rewrite .* http://kong.com$request_uri permanent;
}
訪問測驗 web.liang.com/a/1.html
案例5:php網站登錄跳轉
http://www.liang.com/login/liang.html 轉為 http://www.liang.com/reg/login.php?user=liang
location /login {
rewrite ^/login/(.*)\.html$ /reg/login.php?user=$1;
}
案例6:http://alice.liang.com ==> http://www.liang.com/alice
http://jack.liang.com ==> http://www.liang.com/jack
if ($host ~* "^www.liang.com$") {
break; # 跳出匹配
}
if ($host ~* "^(.*)\.liang\.com$" ) {
set $user $1; # 設定變數
rewrite .* http://www.liang.com/$user permanent;
}
mkdir /usr/share/nginx/html/{jack,alice}
echo "jack" > /usr/share/nginx/html/jack/index.html
echo "alice" > /usr/share/nginx/html/alice/index.html
案例7:訪問的.sh結尾的檔案則回傳403操作拒絕錯誤
location ~* \.sh$ {
return 403;
#return 301 http://www.liang.com;
}
案例8:http 轉換 https
(1)云主機 購買域名 申請CA證書
(2)把CA證書下載下來 解壓證書
(3)在組態檔中開啟443 server
server {
listen 443;
server_name web.xxxx.com; # 域名
ssl on;
ssl_certificate /etc/pki/tls/certs/server.crt;
ssl_certificate_key /etc/pki/tls/certs/server.key; # 指定證書所存放的路徑
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # 密碼指定為OpenSSL支持的格式
ssl_prefer_server_ciphers on;
location / {
root /liang/html;
index index.html index.htm;
}
}
(4)啟動證書
server {
listen 8080;
server_name 域名;
return 301 https://域名$request_uri;
}
(5)訪問http 自動轉到 https
案例9:last、break、redirect、permanent標記
mkdir /usr/share/nginx/html/test
echo 'break' > /usr/share/nginx/html/test/break.html
echo 'last' > /usr/share/nginx/html/test/last.html
echo 'test' > /usr/share/nginx/html/test/test.html
server {
listen 80;
server_name 192.168.93.136;
location / {
root /usr/share/nginx/html;
index index.html index.php;
}
location /break {
rewrite .* /test/break.html break;
root /usr/share/nginx/html;
}
location /last {
rewrite .* /test/last.html last;
root /usr/share/nginx/html;
}
location /test {
rewrite .* /test/test.html break;
root /usr/share/nginx/html;
}
}
訪問測驗 http://192.168.93.136/break 訪問原站點
訪問測驗 http://192.168.93.136/last 自動跳轉到下一個站點
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/333559.html
標籤:其他
上一篇:JVM學習-運行時資料區2
下一篇:河師大拼團專案總結2.0
