微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx rewrite模块

1、rewrite的概述

rewrite功能就是,使用Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向

rewrite只能放在server{},location{},if{}中,并且认只能对域名后边的除去传递的参数外的字符串起作用,

 

2、rewrite 过程:

(1) 执行 server 块里面的 rewrite 指令

(2) 执行 location 匹配

(3) 执行选定的 location 中的 rewrite 指令

语法: rewrite [flag];

regex :表示正则匹配规则

replacement :表示跳转后的内容

flag :表示 rewrite 支持的 flag 标记

flag标记说明:

last :本条规则匹配完成后,继续向下匹配新的location URI规则,一般用在 server 和 if 中

break :本条规则匹配完成即终止,不再匹配后面的任何规则,一般使用在 location 中

redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址

permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

 

3、基于域名跳转

操作步骤

vim /usr/local/Nginx/conf/Nginx.conf

server {

listen 80;

server_name www.xhx.com; #域名修改

charset utf-8;

access_log /var/log/Nginx/www.xhx.com-access.log; #日志修改

location / { #添加域名重定向

if ($host = 'www.xhx.com'){ #$host为rewrite全局变量,代表请求主机头字段或主机名

rewrite ^/(.*)$ http://www.hello.com/$1 permanent; #$1为正则匹配的内容,即域名后边的字符串

}

root html;

index index.html index.htm;

}

}

echo "172.16.10.101 www.xhx.com www.hello.com" >> /etc/hosts

cd /usr/local/Nginx/html

mkdir test

cd test

echo "<h1>xhx<h1>" > index.html

systemctl restart Nginx #重启服务

浏览器输入模拟访问 http://www.xhx.com/test/index.html跳转www.hello.com/test/index.html,查看元素可以看到返回301,实现了永久重定向跳转,而且域名后的参数也正常跳转

 

 

 

4.基于客户端 IP 访问跳转

操作步骤

vim /usr/local/Nginx/conf/Nginx.conf

server {

            listen 80;

            server_name www.xhx.com; #域名修改

              charset utf-8;

             access_log /var/log/Nginx/www.xhx.com.access.log; #日志修改

            set $rewrite true;

            if ($remote_addr = "172.16.10.101") {      #当客户端IP为172.16.10.101时,将变量值设为false,不进行重写

            set $rewrite false; }                                  #除了合法IP,其它都是非法IP,进行重写跳转维护页面

              if ($rewrite = true){                               #当变量值为true时,进行重写

               rewrite (.+) /test.html;                     #重写在访问IP后边插入/index.html,例如172.16.10.101/index.html

               }

              location = /test.html {

               root /var/www/html;                             #网页返回/var/www/html/index.html的内容

               }

             location / {

               root html;

               index index.html index.htm;

}

}

 

mkdir -p /var/www/html/

echo "<h1>正在维护</h1>" > /var/www/html/index.html

systemctl restart Nginx

 本机访问

其他主机访问

 

 

 

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

操作步骤

vim /usr/local/Nginx/conf/Nginx.conf

server {

listen 80;

server_name bbs.xhx.com; #域名修改

charset utf-8;

access_log /var/log/Nginx/bbs.xhx.com.access.log;

#添加

location /test {

rewrite (.+) http://www.xhx.com/bbs$1 permanent; #这里的$1为位置变量,代表/test

}

location / {

root html;

index index.html index.htm;

}

}

 

 

 

 

 

mkdir -p /usr/local/Nginx/html/bbs/test

echo "<h1>this is web<h1>" >> /usr/local/Nginx/html/bbs/test/1.html

echo "172.16.10.101 www.xhx.com bbs.xhx.com" >> /etc/hosts systemctl restart Nginx

使用浏览器访问 http://bbs.xkq.com/ test/1.html 跳转http://www.xkq.com/bbs/test/1.html

 

 

 

 

6.基于参数匹配的跳转

步骤

 

vim /usr/local/Nginx/conf/Nginx.conf

server {

listen 80;

server_name www.xhx.com; #域名修改

charset utf-8;

access_log /var/log/Nginx/xhx.kgc.com-access.log;

if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {

rewrite (.*) http://www.xhx.com permanent;

}

location / {

root html;

index index.html index.htm;

}

systemctl restart Nginx

 

 

 用浏览器访问 http://www.xhx.com/100-200-100.html 或 http://www.xhx.com/100-100-100.html 跳转到http://www.xhx.com页面

 

 

 

 

 

 

 

7.基于目录下所有 PHP 结尾的文件跳转

操作步骤

vim /usr/local/Nginx/conf/Nginx.conf

server {

listen 80;

server_name www.xhx.com; #域名修改

charset utf-8;

access_log /var/log/Nginx/www.xhx.com.access.log ;

location ~* /upload/.*.PHP$ {

rewrite (.+) http://www.xhx.com permanent;

}

location / {

root html;

index index.html index.htm;

}

}

 

 

 

 

 

systemctl restart Nginx

浏览器访问 http://www.xhx.com/upload/888.php 跳转http://www.xhx.com页面

 

 

 

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐