Nginx正则表达式locationrewrite
Nginx正則表達式&&location&&rewrite
- 前言
- 一、Nginx中的正則表達式
- 1.1 常用的Nginx 正則表達式
- 二、訪問路由location
- 2.1 location的分類
- 2.2 location 常用的匹配規則:
- 2.3 location 優先級:
- 2.4 location 示例說明:
- 2.5 實際網站使用中的三個匹配規則定義:
- 2.5.1 第一個必選規則
- 2.5.2 第二個必選規則是處理靜態文件請求,
- 2.5.3 第三個規則就是通用規則,
- 三、訪問重新rewrite
- 3.1 rewrite的概述
- 3.2 rewrite 執行順序如下:
- 3.3 rewrite 示例:
- 方法1:基于域名的跳轉
- 方法2:基于客戶端 IP 訪問跳轉
- 方法3:基于舊域名跳轉到新域名后面加目錄
- 方法4:基于參數匹配的跳轉
- 方法5:基于目錄下所有 php 結尾的文件跳轉
- 方法6:基于最普通一條 url 請求的跳轉
- 總結
前言
一、Nginx中的正則表達式
1.1 常用的Nginx 正則表達式
^ :匹配輸入字符串的起始位置 $ :匹配輸入字符串的結束位置* :匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”* :匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o” ? :匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}” . :匹配除“\n”之外的任何單個字符,若要匹配包括“\n”在內的任意字符,請使用諸如“[.\n]”之類的模式 \ :將后面接著的字符標記為一個特殊字符或一個原義字符或一個向后引用。如“\n”匹配一個換行符,而“$”則匹配“$” \d :匹配純數字 \s :匹配空的(空格或者制表符) {n} :重復 n 次 {n,} :重復 n 次或更多次 {n,m} :重復 n 到 m 次 [ ] :定義匹配的字符范圍 [c] :匹配單個字符 c [a-z] :匹配 a-z 小寫字母的任意一個 [a-zA-Z0-9] :匹配所有大小寫字母或數字 () :表達式的開始和結束位置 | :或運算符二、訪問路由location
2.1 location的分類
location 大致可以分為三類:
- 精準匹配:location = / {}
- 一般匹配:location / {}
- 正則匹配:location ~ / {}
2.2 location 常用的匹配規則:
= :進行普通字符精確匹配,也就是完全匹配。^~ :表示普通字符匹配。使用前綴匹配。如果匹配成功,則不再匹配其它 location。~ :區分大小寫的匹配。~* :不區分大小寫的匹配。!~ :區分大小寫的匹配取非。!~* :不區分大小寫的匹配取非。2.3 location 優先級:
- 首先精確匹配 =
- 其次前綴匹配 ^~
- 其次是按文件中順序的正則匹配 ~ 或 ~*
- 然后匹配不帶任何修飾的前綴匹配
- 最后是交給 / 通用匹配
2.4 location 示例說明:
(1)location = / {}
=為精確匹配 / ,主機名后面不能帶任何字符串,比如訪問 / 和 /data,則 / 匹配,/data 不匹配 再比如 location = /abc,則只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,則即匹配/abc 、/abcd/ 同時也匹配 /abc/。(2)location / {}
因為所有的地址都以 / 開頭,所以這條規則將匹配到所有請求 比如訪問 / 和 /data, 則 / 匹配, /data 也匹配, 但若后面是正則表達式會和最長字符串優先匹配(最長匹配)(3)location /documents/ {}
匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續往下搜索其它 location 只有其它 location后面的正則表達式沒有匹配到時,才會采用這一條(4)location /documents/abc {}
匹配任何以 /documents/abc 開頭的地址,匹配符合以后,還要繼續往下搜索其它 location 只有其它 location后面的正則表達式沒有匹配到時,才會采用這一條(5)location ^~ /images/ {}
匹配任何以 /images/ 開頭的地址,匹配符合以后,停止往下搜索正則,采用這一條(6)location ~* .(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 結尾的請求 然而,所有請求 /images/ 下的圖片會被 location ^~ /images/ 處理,因為 ^~ 的優先級更高,所以到達不了這一條正則(7)location /images/abc {}
最長字符匹配到 /images/abc,優先級最低,繼續往下搜索其它 location,會發現 ^~ 和 ~ 存在(8)location ~ /images/abc {}
匹配以/images/abc 開頭的,優先級次之,只有去掉 location ^~ /images/ 才會采用這一條(9)location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正則 ~ /images/abc/1.html 相比,正則優先級更高優先級總結:
(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ,* 正則順序) > (location 部分起始路徑) > (location /
2.5 實際網站使用中的三個匹配規則定義:
2.5.1 第一個必選規則
直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理,比如說官網。
這里是直接轉發給后端應用服務器了,也可以是一個靜態首頁
2.5.2 第二個必選規則是處理靜態文件請求,
這是nginx作為http服務器的強項!
有兩種配置模式,目錄匹配或后綴匹配,任選其一或搭配使用
2.5.3 第三個規則就是通用規則,
比如用來轉發帶.php、.jsp后綴的動態請求到后端應用服務器
非靜態文件請求就默認是動態請求
三、訪問重新rewrite
3.1 rewrite的概述
rewrite功能就是,使用nginx提供的全局變量或自己設置的變量,結合正則表達式和標志位實現url重寫以及重定向。
rewrite只能放在server{},location{},if{}中,并且默認只能對域名后邊的除去傳遞的參數外的字符串起作用,
例如:
http://www.clj.com/a/we/index.php?id=1&u=str 只對/a/we/index.php重寫。
3.2 rewrite 執行順序如下:
(1) 執行 server 塊里面的 rewrite 指令。
(2) 執行 location 匹配。
(3) 執行選定的 location 中的 rewrite 指令。
語法:rewrite <regex> <replacement> [flag];
regex :表示正則匹配規則。
replacement :表示跳轉后的內容。
flag :表示 rewrite 支持的 flag 標記。
flag標記說明
last :本條規則匹配完成后,繼續向下匹配新的location URI規則,一般用在 server 和 if 中。
break :本條規則匹配完成即終止,不再匹配后面的任何規則,一般使用在 location 中。
redirect:返回302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址。
permanent:返回301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址。
3.3 rewrite 示例:
方法1:基于域名的跳轉
現在公司舊域名www.zs.com有業務需求變更,需要使用新域名www.dj.com代替,但是舊域名不能廢除,需要跳轉到新域名上,而且后面的參數保持不變。
vim /usr/local/nginx/conf/nginx.conf server {listen 80;server_name www.zs.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.zs.com-access.log main; #日志修改location / { #添加域名重定向if ($host = 'www.zs.com'){ #$host為rewrite全局變量,代表請求主機頭字段或主機名rewrite ^/(.*)$ http://www.dj.com/$1 permanent; #$1為正則匹配的內容,即域名后邊的字符串}root html;index index.html index.htm;} } echo "192.168.237.200 www.zs.com www.dj.com" >> /etc/hostssystemctl restart nginx #重啟服務瀏覽器輸入模擬訪問 http://www.zs.com/test/1.html(雖然這個請求內容是不存在的)
會跳轉到www.dj.com/test/1.html,查看元素可以看到返回301,實現了永久重定向跳轉,而且域名后的參數也正常跳轉。
方法2:基于客戶端 IP 訪問跳轉
要求:今天公司業務新版本上線,要求所有 IP 訪問任何內容都顯示一個固定維護頁面,只有公司 IP :192.168.237.10訪問正常。
vim /usr/local/nginx/conf/nginx.conf server {listen 80;server_name www.zs.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.zs.com-access.log main; #日志修改#設置是否合法的IP標記set $rewrite true; #設置變量$rewrite,變量值為boole值true#判斷是否為合法IPif ($remote_addr = "192.168.237.200"){ #當客戶端IP為192.168.80.200時,將變量值設為false,不進行重寫set $rewrite false;}#除了合法IP,其它都是非法IP,進行重寫跳轉維護頁面if ($rewrite = true){ #當變量值為true時,進行重寫rewrite (.+) /weihu.html; #重寫在訪問IP后邊插入/weihu.html,例如192.168.80.11/weihu.html}location = /weihu.html {root /var/www/html; #網頁返回/var/www/html/weihu.html的內容}location / {root html;index index.html index.htm;} } mkdir -p /var/www/html/ echo "<h1>We are busy now!</h1>" > /var/www/html/weihu.html systemctl restart nginx方法3:基于舊域名跳轉到新域名后面加目錄
現在訪問的是 http://mail.zs.com/post,現在需要將這個域名下面的訪問都跳轉到http://www.zs.com/bbs
vim /usr/local/nginx/conf/nginx.conf server {listen 80;server_name bbs.zs.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.zs.com-access.log main;#添加location /post {rewrite (.+) http://www.zs.com/mail$1 permanent; #這里的$1為位置變量,代表/post}location / {root html;index index.html index.htm;} } mkdir -p /usr/local/nginx/html/bbs echo "this is 1.html" >> /usr/local/nginx/html/mail/1.html echo "192.168.237.11 bbs.zs.com" >> /etc/hosts systemctl restart nginx使用瀏覽器訪問 http://bbs.zs.com/ post/1.html 跳轉到 http://www.zs.com/mail/post/1.html
方法4:基于參數匹配的跳轉
訪問http://www.zs.com/100-(100|200)-100.html 跳轉到http://www.zs.com頁面。
vim /usr/local/nginx/conf/nginx.conf server {listen 80;server_name www.zs.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.zs.com-access.log main;if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {rewrite (.*) http://www.zs.com permanent;}location / {root html;index index.html index.htm;} } systemctl restart nginx使用瀏覽器訪問 http://www.zs.com/100-200-100.html 或 http://www.zs.com/100-100-100.html 跳轉到http://www.zs.com頁面。
方法5:基于目錄下所有 php 結尾的文件跳轉
要求訪問 http://www.zs.com/upload/123.php 跳轉到首頁。
vim /usr/local/nginx/conf/nginx.conf server {listen 80;server_name www.zs.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.zs.com-access.log main;location ~* /upload/.*\.php$ {rewrite (.+) http://www.zs.com permanent;}location / {root html;index index.html index.htm;} } systemctl restart nginx瀏覽器訪問 http://www.zs.com/upload/123.php 跳轉到http://www.zs.com頁面。
方法6:基于最普通一條 url 請求的跳轉
要求訪問一個具體的頁面如 http://www.zs.com/abc/123.html 跳轉到首頁
vim /usr/local/nginx/conf/nginx.conf server {listen 80;server_name www.zs.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.zs.com-access.log main;location ~* ^/abc/123.html {rewrite (.+) http://www.zs.com permanent;}location / {root html;index index.html index.htm;} } systemctl restart nginx瀏覽器訪問 http://www.zs.com/abc/123.html 跳轉到http://www.zs.com頁面。
總結
總結
以上是生活随笔為你收集整理的Nginx正则表达式locationrewrite的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sql Server常用命令整理篇:生成
- 下一篇: Nginx系列教程(07) - Loca