• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

nginx rewrite

武飞扬头像
小微光
帮助1

一、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代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。

[root@ux ~]# cd /usr/local/nginx/conf/
[root@ux conf]# vim 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;
       }
}

[root@ux conf]# echo "192.168.80.10  www.kgc.com www.benet.com" >> /etc/hosts
[root@ux conf]# systemctl  restart  nginx.service
学新通

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

(2)基于客户端IP访问跳转

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

[root@ux conf]# vim 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(Semote addr-“192.168.80.10”) {
    #当客户端I队192.168.80.10时,将变量值设为false,不进行重写
        set $rewrite 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的内容
            }
}

[root@ux conf]# mkdir -p /var/www/html/
[root@ux conf]# echo "<h1>zhengzaiweihu</h1>" > /var/www/html/weihu.html
[root@ux conf]# systemctl  restart  nginx.service
学新通

只有IP 为192.168.80.10能正常访问,其它地址都是维护页面

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

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

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

[root@ux conf]# vim nginx.conf
server {
        listen  80;
        server_name   www.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
        }
}

[root@ux conf]# mkdir -p /usr/local/nginx/html/bbs/post
[root@ux conf]# echo "this is 1.html " >> /usr/local/nginx/html/bbs/post/1.html
[root@ux conf]# echo "192.168.80.10  bbs.kgc.com" >> /etc/hosts
[root@ux conf]# systemctl  restart  nginx.service 
学新通

使用浏览器访问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://ww.kgc.com页面。

[root@ux conf]# vim 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;
        }
}

[root@ux conf]# systemctl  restart  nginx.service 

$request 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
d o c u m e n t u r i : 与 document_uri:与 documenturi:uri相同,这个变量指当前的请求URI,不包括任何传递参数,如:/ abc/bbs/index.html

使用浏览器访问http://www.kgc.com/100-200-100.html或http://www.kgc.com/100-100-100.html跳转到http://www.kgc.com页面。
学新通

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

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

[root@ux conf]# vim 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;
        }
        }
        
[root@ux conf]# systemctl  restart  nginx.service

浏览器访问http://www.kgc.com/upload/123.php 跳转到http://www.kgc.com页面。
学新通

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

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

[root@ux conf]# vim 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;
        }
        }
        
[root@ux conf]# systemctl  restart  nginx.service

浏览器访问 http://www.kgc.com/abc/123.html跳转到http://www.kgc.com页面。
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaejab
系列文章
更多 icon
同类精品
更多 icon
继续加载