Nginx Rewrite

83 篇文章 0 订阅
69 篇文章 0 订阅


从功能看rewrite 和location 似乎有点像,都能实现跳转,主要区别在于rewrite 是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,还可以proxy_ pass 到其他机器。

一、常用的Nginx正则表达式

正则表达式说明
^匹配输入字符串的起始位置
$匹配输入字符串的结束位置
*匹配前面的字符零次或多次。如“ol*" 能匹配“o"及“ol”、“oll"
+匹配前面的字符- - 次或多次。如“o1+” 能匹配"ol"及“ol1”、“o11l”, 但不能匹配"o”
匹配前面的字符零次或一-次,例如“do (es) ?“能匹配“do"或者“does”,”?”等效于"{0,1}”
.匹配除"\n”之外的任何单个字符,若要匹配包括"\n”在内的任意字符,请使用诸如“[.\n]"之类的模式
\将后面接着的字符标记为一个特殊字符或个 原义字符或一个向后引用。如“\n”匹配一个换行符, 而“\$”则匹配"$”
\d匹配纯数字
{n}重复n次
{n,}重复n次或更多次
{n,m}重复n到m次
[]定义匹配的字符范围
[c]匹配单个字符c
[a-z]匹配a-z小写字母的任意–个
[a-zA-20-9]匹配所有大小写字母或数字
()表达式的开始和结束位置
或运算符
\s

二、location

location大致可以分为三类

精准匹配: location = / {…}

一般匹配: location

正则匹配: location ~ / {…}

location常用的匹配规则

符号匹配规则
=进行普通字符精确匹配,也就是完全匹配
^~表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其它location
~区分大小写的匹配
~*不区分大小写的匹配
!~区分大小写的匹配取非
!~*不区分大小写的匹配取非

location优先级

优先级
首先精确匹配 =
其次前缀匹配 ^~
其次是按文件中顺序的正则匹配 ~ 或 ~*
然后匹配不带任何修饰的前缀匹配
最后是交给 / 通用匹配

精确匹配、前缀匹配 匹配及停止 不在往下匹配
匹配选择最长匹配原则

实际网站使用中,至少有三个匹配规则定义

第一个必选规则

直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网。
可以是一个静态首页,也可以直接转发给后端应用服务器

location = / {
root      html;
index     index.html index.htm;
}

第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项

有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用

location ^~ /static/ {
        root /webroot/static/ ;
}

location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
         root /webroot/res/ ;
}

第三个规则就是通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器

非静态文件请求就默认是动态请求

location / {
proxy_pass http:// tomcat_serveri ;
}

三、rewrite

rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。
比如:更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盜链等等需求。

rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用,例如http:/ /www . kgc. com/ abc/ bbs/ index. php?a=1&b=2只对/ abc/bbs/ index. php重写。

rewrite跳转实现
Nginx:通过ngx_http_rewrite_module模块支持URL重写、支持if条件判断,但不支持else
跳转:从一个location跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误
PCRE支持:perl兼容正则表达式的语法规则匹配
重写模块set指令:创建新的变量并设其值

rewrite执行顺序如下
(1) 执行server 块里面的rewrite 指令。
(2) 执行location 匹配。
(3) 执行选定的location 中的rewrite指令。

语法格式: rewrite [flag] ;
regex:表示正则匹配规则。
replacement :表示跳转后的内容。
flag :表示rewrite支持的flag标记。

在这里插入图片描述

flag标记说明
last :本条规则匹配完成后,继续向下匹配新的location URL规则,一般用在server和if中。
break:本条规则匹配完成即终止,不再匹配后面的任何规则,一般使用在location中。
redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址。
permanent :返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。

四、rewrite示例

1基于域名的跳转

现在公司旧域名www.kgc.com有业务需求变更,需要使用新域名www.benet.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。

vim /usr/local/nginx/conf/nginx.conf

server {
listen         80;
server_name    www.kgc.com;                          #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log;      #日志修改

location / {    
#添加域名重定向
if ($host = 'www.kgc.com') {                             #$host为rewrite全局变量,代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www. benet。com/$1 permanent;     #$1为正则匹配的内容,即“域名/”之后的字符串
}
root        html ;
index index.html index.htm;
}
}

echo “192.168.100.10 www.kgc.com www.benet.com” >> /etc/hosts
systemctl restart nginx
浏览器输入模拟访问http://www. kgc.com/test/1.html (虽然这个请求内容是不存在的)
会跳转到www.benet.com/test/1.html,查看元素可以看到返回301,实现了永久重定向跳转,而且域名后的参数也正常跳转。

2基于客户端IP访问跳转

今天公司业务新版本上线,要求所有IP访问任何内容都显示一个固定维护页面, 只有公司IP : 192.168. 100.10访问正常。

vim /usr/ 1ocal/nginx/conf/nginx. conf

server {
listen          80;
server_name www.kgc.com;                 #域名修改
charset utf-8;
access_log/var/log/nginx/ www.kgc.com-access.log;     #日志修改

#设置是否合法的IP标记
set $rewrite true;
#设置变量$rewrite,变量值为boole值true

#判断是否为合法IP
if ($remote_addr = "192.168.100.10") {     
#当客户端IP为192.168.100.10时,将变量值设为false,不进行重写
set Srewrite false;           
}

#除了合法IP,其它都是非法IP,进行重写跳转维护页面
if ($rewrite = true) {        #当变量值为true时,进行重写
rewrite (.+) /weihu. html;
#将域名后边的路径重写成/weihu.html,例如www.kgc.com/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 maintaining now!</h1>" > /var/www/html/weihu.html
systemctl restart nginx 
只有IP为192.168.100.10能正常访问,其它地址都是维护页面

如果rewrite (.+) /weihu.html; 改成rewrite (.+) /weihu.html permanent;的话,如果是非192.168.100.10
的主机访问会使浏览器修改请求访问的URL成http: //www.kgc.com/weihu.html再请求访问,这样就会进入一直在rewrite的死循环,访问请求会一直被重写成 http://www.kgc.com/weihu.html再请求访问

3基于旧域名跳转到新域名后面加目录

现在访问的是http://bbs.kgc.com/post/,现在需要将这个域名下面的访问都跳转http://www.kgc.com/bbs/post/

vim /usr/local/nginx/conf/nginx.conf
server {
listen       80;
server_name bbs.kgc.com;           #域名修改
charset utf-8;
access_log/var/log/nginx/www.kgc.com-access.log;

#添加
location /post {
rewrite (.+) http://www.kgc.com/bbs$1 permanent;     #这里的$1为位置变量,代表/post
}

location / {
root     html; 
index    index.html index.htm;
}
}

mkdir -p /usr/local/nginx/html/bbs/post
echo “this is 1.html” >> /usr/ local/nginx/html/bbs/post/1.html
echo “192.168.100.10 bbs.kgc.com” >> /etc/hosts
systemctl restart nginx
使用浏览器访问http://bbs.kgc.com/post/1.html 跳转到http://www.kgc.com/bbs/post/1.html

4基于参数匹配的跳转

现在访问http://www. kgc.com/100- (100|200) -100.html跳转到http://www.kgc.com页面。

vim /usr/local/nginx/conf/nginx.conf
server {
listen   80;
server_name www.kgc.com;      #域名修改
charset utf-8;
access_log/var/log/nginx/www.kgc.com-access.log;
if ($request_uri ~^/100-(100|200)-(\d+).html$) {
rewrite (.+) http://www.kgc.com permanent;
}
location / {
root   html;
index index.html index.htm;
}
}

Srequest_uri:包含请求参数的原始URI,不包含主机名,如: http://www. kgc.com/abc/bbs/index.html?a=1&b=2中的/abc/bbs/index.php?a=1&b=2
$uri:这个变量指当前的请求URI,不包括任何参数,如:/abc/bbs/ index. html
$document_uri : 与$uri相同,这个变量指当前的请求URI,不包括任何传递参数,如: /abc/ bbs/ index . html

systemctl restart nginx
使用浏览器访问http://www.kgc.com/100-200-100.html或http://www.kgc.com//00-100-100.htmal 跳转到http://www.kgc.com页面。

5基于目录下所有php 结尾的文件跳转

要求访问http://www.kgc.com/upload/123.php跳转到首页。

vim /usr/local/nginx/conf/nginx.conf
server{
listen     80;
server name   www.kgc.com;          #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.kgc.com permanent;
}

location / {
root    html;
index   index.html index.htm;
}
}

systemctl restart nginx
浏览器访问http://www.kgc.com/upload/123.php跳转到http://www.kgc.com页面。

6基于最普通一条url请求的跳转

要求访问一个具体的页面如http://www.kgc.com/abc/123.html 跳转到首页

vim /usr/local/nginx/conf/nginx.conf
server {
listen    80;
server_name   www.kgc.com;        #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log;
location ~* ^/abc/123.html {
rewrite (.+) http://www.kgc.com permanent;
}

location / {
root     html ;
index index.html index.htm;
}
}

总结

优先级总结
(location = 整路径) > (location ^~ 路径) > (location ~ 或 ~* 正则顺序) > (location 部分起始路径) > (location /)

习题

1、浏览器访问 http://www.kgc.com/bbs/index.php 跳转到http://www.kgc.com页面。

        location = /bbs/index.php {
         rewrite ^/ http://www.kgc.com permanent;
        }


      location ~* ^/bbs/index.php {
      rewrite (.+) http://www.kgc.com permanent;
      }

       location / {
       root   html;
       index  index.html index.htm;
       }

172.16.10.88 www.kgc.com >> /etc/hosts
2、浏览器访问 http://www.kgc.com/bbs/index.php 或者http://www.kgc.com/post/index.php 跳转到http://www.kgc.com/test.html页面。

        location ~* ^/(bbs|post)/index.php$ {
        rewrite ^/ http://www.kgc.com/text.html permanent;
        }

        location / {
        root   html;
        index  index.html index.htm;
        }

3、浏览器访问 http://mail.kgc.com/post/1.html 跳转到http://www.kgc.com/mail/post/1.html页面。

    server {
        listen       80;
        server_name  mail.kgc.com;

        charset utf-8;

        access_log /var/log/nginx/www.kgc.com-access.log;

        location /post {
        rewrite (.+) http://www.kgc.com/mail$1 permanent;
        }


        location / {
        root   html;
        index  index.html index.htm;
        }

echo “172.16.10.88 mail.kgc.com” >> /etc/hosts

4、浏览器访问 http://www.accp.com 跳转到http://www.kgc.com页面,并且保证域名后面的路径参数不变

server {
        listen       80;
        server_name  www.accp.com;

        charset utf-8;

        access_log /var/log/nginx/www.kgc.com-access.log;

        location / {
        if ($host = 'www.accp.com'){
            rewrite ^/(.*)$ http://www.kgc.com/$1 permanent;    
             }
        root html;
        index  index.html index.htm;
        }

echo “172.16.10.88 www.accp.com” >> /etc/hosts

5、只允许你本机浏览器能访问 http://www.kgc.com 下的所有页面,其它主机访问只能看到维护页面

    server {
        listen       80;
        server_name  www.kgc.com;
        charset utf-8;
        access_log /var/log/nginx/www.kgc.com-access.log;

        set $rewrite true;
        if ( $remote_addr = "172.16.10.88" ) {
                set $rewrite false;
        }

        if ( $rewrite = true ) {
                rewrite (.+) /weihu.html;
        }

        location = /weihu.html {
                root /var/www/html;
        }

        location / {
        root html;
        index  index.html index.htm;
        }

mkdir -p /var/www/html
echo “mmmmmmmmm” > /var/www/html/weihu.html
172.16.10.88 www.kgc.com>> /etc/hosts

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值