Nginx中的匹配规则

Nginx匹配规则与rewrite案例详解

nginx中强大的功能,,如rewrite、proxy_pass都离不开他的匹配规则。

1. nginx location

1.语法规则和优先级

location [=|~|~|!~|!~|^~] /uri/ { ... } = > ^~ > ~|~|!~|!~** > / 精确匹配 > 字符开头 > 正则匹配 > 通配

  • = 表示精确匹配
  • ^~ 表示uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格,即所见即所得)。
  • ~ 正则匹配(区分大小写)
  • ~* 正则匹配(不区分大小写)
  • !~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配 的正则
  • / 任何请求都会匹配
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 ,使用开头匹配增加匹配优先级。

3. nginx rewrite

rewrite 将用户访问的url 重定向到一个新的url

下面通过几个rewrite匹配案例来看一下

案例1:rewrite url实现跳转

当访问http://192.168.9.13/aaa/a/1.html 时,重定向至http://192.168.9.13/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.9.13/aaa/a/1.html 访问的url中含有"/aaa"字段就可以

permanent参数:

如果用了permanent 会直接显示新的url 新的网页内容,如果不使用 还是显示原来的url但是页面内容还是会显示转发后的。

以下匹配方法是否可行:

location = /aaa         http://192.168.9.13/aaa/123.html

不可行。因为需要完全匹配

location ~ /aaa         http://192.168.9.13/aaa/123.html

可行。因为部分匹配即可

location ^~ /aaa         http://192.168.9.13/aaa/123.html

可行。因为部分匹配即可

案例2:rewrite中使用正则

http://192.168.93.136/2017/a/1.htmlhttp://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.comhttp://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

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

if ($host ~* "^www.liang.com$") {
break;        # 跳出匹配
}
if ($host ~* "^(.*).liang.com$" ) {
set $user $1;       # 设置变量

rewrite .* http://www.liang.com/$user permanent;

}

案例7:访问的.sh结尾的文件则返回403操作拒绝错误

location ~* .sh$ {
return 403;
#return 301 http://www.liang.com;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值