一、常用的Nginx正则表达式
^:匹配输入字符串的起始位置
$:匹配输入字符串的结束为止
*: 匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”等
+:匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”,但不能匹配“o”
?:匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,“?”等效于“{0,1}”
.:匹配除“\n”之外的任何单个字符,如要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式
\ :将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$”
\d :匹配纯数字
\w :匹配字母或数字或下划线或汉字
\s :匹配任意的空白符
\b :匹配单词的开始或结束
{n} :重复 n 次
{n,} :重复 n 次或更多次
{n,m} :重复 n 到 m 次
[] :定义匹配的字符范围
[c] :匹配单个字符 c
[a-z] :匹配 a-z 小写字母的任意一个
[a-zA-Z0-9] :匹配所有大小写字母或数字
() :表达式的开始和结束位置
| :或运算符
二、location匹配
从功能看,rewrite和location似乎很像,都能实现跳转,主要区别在于 rewrite是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器
1、匹配规则格式
格式 | 规则 |
精准匹配 | location = / {...} |
一般匹配 | location / {...} |
正则匹配 | location ~ / {...} |
2、常用的匹配规则
= :进行普通字符精确匹配,也就是完全匹配。
^~ :表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其它 location。
~ :区分大小写的匹配。
~* :不区分大小写的匹配。
!~ :区分大小写的匹配取非。
!~* :不区分大小写的匹配取非。
3、匹配的优先级
首先精确匹配 =
其次前缀匹配^~
再其次是按文件中顺序的正则匹配 ~或~*
然后匹配不带任何修饰的前缀匹配(一般匹配)
最后是交给/通用匹配
优先级总结:
(1)(location = 完整路径)>(location ^~路径)>(location ~,~* 正则顺序)>(location 部分起始部分)>(location /)
(2)优先级相同,正则看上下顺序,上面的优先;一般匹配看长度,最长匹配优先
4、示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | (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 相比,正则优先级更高 |
5、在实际网站使用中,至少有三个匹配规则定义
(1)第一个必选规则匹配
1 2 3 4 5 6 | 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网 这里是直接转发给后端应用服务器了,也可以是一个静态首页 location = / { proxy_pass http: //tomcat_server/; } |
(2)第二个必选规则匹配
1 2 3 4 5 6 7 8 9 | 处理静态文件请求,这是nginx作为http服务器的强项 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用 location ^~ / static / { root /webroot/ static /; } location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/; } |
(3)第三个必选规则匹配
1 2 3 4 5 | 通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器 非静态文件请求就默认是动态请求 location / { proxy_pass http: //tomcat_server; } |
三、rewrite重写
1、简介
(1)rewrite功能就是使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记实现URL重写以及重定向。 例如:更换域名后需要保持旧的域名能够转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求
(2)rewrite只能放在server{},location{},if{}中,并且默认只能对域名后面的除去传递的参数外的字符串起作用。 例如:http://www.123.com/g/xd/index.php?a=1&b=2 只对/g/xd/index.php重写
2、rewrite跳转实现
![](https://img-blog.csdnimg.cn/img_convert/d88f9d348ae39890c3816bdd43552d5f.png)
3、rewrite执行顺序
(1)执行server块里面的rewrite指令
(2)执行location匹配
(3)执行选定的location中的rewrite指令
4、rewrite语法格式
rewrite 《regex》 《replacement》 [flag]
regex:表示正则匹配规则
replacement:表示跳转后的内容
flag:表示rewrite支持的flag标记
(1)last:本条规则匹配完成后,继续向下匹配新的location的URL规则,一般用在server和if中
(2)break:本条规则匹配完成后即终止,不再匹配后面的任何规则,一般用在location中
(3)redirect:返回临时重定向的HTTP状态302,浏览器地址会显示跳转后的URL地址
(4)permanent:返回永久重定向的HTTP状态301,浏览器地址会显示跳转后的URL地址
四、rewrite跳转
1、基于域名的跳转
(1)需求
公司旧域名www.gxd.com有业务需求变更,需要使用新域名www.ggg.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变
(2)配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.123.com; #域名修改 charset utf-8; access_log / var /log/nginx/www.123.com-access.log; #日志修改 location / { #添加域名重定向 if ($host = 'www.123.com' ){ #$host为rewrite全局变量,代表请求主机头字段或主机名 rewrite ^/(.*)$ http: //www.aaa.com/$1 permanent #$1为正则匹配的内容,即域名后边的字符串 } root html; index index.html index.htm; } } systemctl restart nginx |
![](https://img-blog.csdnimg.cn/img_convert/9142814df71807c5dbf5183760f625e0.png)
(3)浏览器验证
![](https://img-blog.csdnimg.cn/img_convert/4bbffe230f6ba9b03f22b23ac225314c.png)
2、基于客户端IP访问跳转
(1)需求
公司业务新版本上线,要求所有IP访问都显示一个固定维护页面,只有公司IP:192.168.80.11访问正常
(2)配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.123.com; #域名修改 charset utf-8; access_log / var /log/nginx/www.123.com-access.log; #日志修改 #设置是否合法的IP标记 set $rewrite true ; #设置变量$rewrite,变量值为boole值 true #判断是否为合法IP if ($remote_addr = "192.168.142.5" ){ #当客户端IP为192.168.142.5时,将变量值设为 false ,不进行重写 set $rewrite false ; } #除了合法IP,其它都是非法IP,进行重写跳转维护页面 if ($rewrite = true ){ #当变量值为 true 时,进行重写 rewrite (.+) /weihu.html; #重写在访问IP后边插入/weihu.html } location = /weihu.html { root / var /www/html; #网页返回/ var /www/html/weihu.html的内容 } location / { root html; index index.html index.htm; } } systemctl restart nginx |
![](https://img-blog.csdnimg.cn/img_convert/fab0bce3b1f622073f63b87448ed54e8.png)
![](https://img-blog.csdnimg.cn/img_convert/e38f08018cd010b325d1da154258ff24.png)
(3)浏览器验证
![](https://img-blog.csdnimg.cn/img_convert/f33fc501bb81a57be83d8ce94c4860ae.png)
![](https://img-blog.csdnimg.cn/img_convert/20eb11bf93d3a1964c67f0e07d28860f.png)
3、基于旧域名跳转到新域名后面加目录
(1)需求
我们访问 http://www.123.com/aaa/,现在需要将这个域名下面的访问都跳转到http://www.ggg.com/bbb/aaa
(2)配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name bbs.123.com; #域名修改 charset utf-8; access_log logs/123.com-access.log; #添加 location /post { rewrite (.+) http: //www.123.com/bbs$1 permanent; #这里的$1为位置变量,代表/post } location / { root html; index index.html index.htm; } } systemctl restart nginx |
![](https://img-blog.csdnimg.cn/img_convert/8354a1aa4057b7d353b32920ce1e5458.png)
![](https://img-blog.csdnimg.cn/img_convert/adcbf7a1652164b499a9c3f20e4e8452.png)
(3)浏览器验证
![](https://img-blog.csdnimg.cn/img_convert/1e47962cb52677ab0a960361ac9a95e0.png)
4、基于参数匹配的跳转
(1)需求
访问http://www.123.com/100-100-1.html跳转到www.123.com首页
(2)配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.123.com; #域名修改 charset utf-8; access_log / var /log/nginx/www.123.com-access.log; if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { rewrite (.+) http: //www.123.com permanent; } location / { root html; index index.html index.htm; } } systemctl restart nginx |
![](https://img-blog.csdnimg.cn/img_convert/e2b7ac51241f53b2ea2f46fd63759443.png)
(3)浏览器验证
![](https://img-blog.csdnimg.cn/img_convert/73260a004a9161e326c6b29a4a3277fa.png)
5、基于目录下所有php结尾的文件跳转
(1)需求
访问一个具体页面如http://www.123.com/upload/1.php跳转到首页
(2)配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.123.com; #域名修改 charset utf-8; access_log / var /log/nginx/www.123.com-access.log; location ~* /upload/.*\.php$ { rewrite (.+) http: //www.123.com permanent; } location / { root html; index index.html index.htm; } } systemctl restart nginx |
![](https://img-blog.csdnimg.cn/img_convert/8f71a7092d3b3fda660291a67fa2f8f5.png)
![](https://img-blog.csdnimg.cn/img_convert/8534f2d8dcf66ee991d14cec7e2a8ce7.png)