Nginx Rewrite

  • Nginx Rewrite概述
    • Rewrite跳转场景
    • Rewrite实际场景
    • Nginx正则表达式
    • Rewrite命令
      • last和break比较
    • location分类
  • 基于域名的跳转
  • 基于客户端IP访问跳转
  • 基于旧、新域名跳转并加目录
  • 基于参数匹配的跳转
  • 基于目录下所有php文件跳转
  • 基于最普通url请求的跳转

Nginx Rewrite概述

Rewrite跳转场景

1、URL看起来更规范,合理
2、企业会将动态URL地址伪装成静态地址提供服务
3、网址换新域名后,让旧的访问跳转到新的域名上
4、服务端某些业务调整

Rewrite实际场景

1.Nginx跳转需求的实现方式
使用rewrite进行匹配跳转
使用if匹配全局变量后跳转
使用location匹配再跳转

2.rewirte放在server{},if{},location{}段中
location只对域名后边的除去传递参数外的字符串起作用

3.对域名或参数字符串
使用if全局变量匹配 (重定向)
使用proxy_pass反向代理 (动静分离)

Nginx正则表达式

常用的正则表达式元字符

  字符                         说明
   ^                   匹配输入字符串的起始位置
   $                   匹配输入字符串的结束位置
   *                   匹配前面的字符零次或多次
   +                   匹配前面的字符一次或多次
   ?                  匹配前面的字符零次或一次
   .                   匹配除“\n”(换行)之外的任何单个字符
   \      转义字符      将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用(使用诸如“[.\n]”之类的模式,可匹配包括“\n”在内的任意字符)
  \d                   匹配纯数字  [0-9] 代替单个数字
  {n}                  重复n次
 {n,}                  重复n次或更多次
  [c]                  匹配单个字符c
 [a-z]                 匹配a-z小写字母的任意一个
[a-zA-Z]               匹配a-z小写字母或A-Z大写字母的任意一个

Rewrite命令

Rewrite命令语法

flag标记说明

last和break比较

(1)last:url重写后,马上发起一个新请求。再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏不变。
(2)break:url重写后,直接使用当前资源,不再使用location余下的语句,完成本次请求,地址栏不变。
总结:last和break在重定向后,地址栏都不会发生变化,这是它们的相同点,不同点在于last会写在server和if中,break是写在location中,last不会终止重写后的url匹配,break会终止重写后的url匹配。

location分类

分类

location = patt {} [精确匹配]
location patt {} [一般匹配]
location ~ patt {} [正则匹配]

正则匹配的常用表达式
loation优先级
1.相同类型的表达式,字符串长的会优先匹配

2.按优先级排列

= 类型                                       精确匹配
^~ 类型表达式                                 前缀匹配
正则表达式 (~和~*)类型  
常规字符串匹配类型,按前缀匹配                   一般匹配
通用匹配(/),如果没有其他匹配,任何请求都会匹配到  通用匹配

location优先级规则
(location = 完整路径)精确 > (location ^~ 完整路径) 前缀> (location ~* 完整路径 )正则 = (location ~ 完整路径) 正则> (location 完整路径)一般 > (location /)通用

用目录做匹配访问某个文件
(location = 目录)> (location ^~ 目录)>(location ~ 目录)=(location ~* 目录)>(location 目录)>(location /)

比较rewrite和location
相同点 :都可以实现跳转

不同点:

rewrite是在同一域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理,还可以proxy_pass到其它机器
rewrite会写在location里,执行顺序
执行server块里面的rewrite指令
执行location匹配
执行选定的location中的rewrite指令

基于域名的跳转

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

配置环境:Nginx架构上

1、给windows配置映射

在C/Windows/system32/drivers/etc/hosts文件
文本形式打开文件
添加
20.0.0.13 www.domain.com www.newdomain.com

2、编辑配置文件
vi /etc/nginx.conf 

####修改默认站点配置文件
server { 
	server_name www.domain.com; 
	if  ($host = 'www.domain.com')
          { 
	rewrite ^/(.*)$ http://www.newdomain.com/$1 permanent;
          }
}

####添加新域名www.newdomain.com的站点位置

server { 
	listen   80;
	server_name www.newdomain.com;
	#charset utf-8;
	access_log /var/log/nginx/www.newdomain.com-access.log main; 
	location / { 
       	root /usr/share/nginx/html; 
	index index.html index.htm;
	}
}

[root@server ~]# nginx -t              //检查语法,报错,没有目录不存在
[root@server ~]# mkdir /var/log/nginx   //创建目录
[root@server ~]# nginx -t
[root@server ~]# mkdir -p /usr/share/nginx/html  //创建主页 
[root@server ~]# echo "this is a test web." > /usr/share/nginx/html/index.html
[root@server ~]# cat /usr/share/nginx/html/index.html  //查看主页
this is a test web.
[root@server ~]# systemctl restart nginx

验证效果:
在浏览器上http://www.domain.com进行验证
实现了跳转:

抓包:

基于客户端IP访问跳转

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

方法一:
1、做映射
vi /etc/hosts
20.0.0.13  www.domain.com

2、修改默认站点配置文件

[root@server ~]# vi /etc/nginx.conf 
[root@server ~]#nginx -t
[root@server ~]#systemctl restart nginx

server { 
        listen       80;
        server_name  www.domain.com;
 set $rewrite true;    //变量自定义
        if ($remote_addr = "192.168.40.13") {    //全局变量 (重定向)
        set $rewrite false;
    }
        if ($rewrite = true) { 
        rewrite (.+) /maintenance.html;    //整个域名
    }
        location = /maintenance.html {      //精确定位
        root   /usr/share/nginx/html;        //根路径
    }

3、创建重定向网页
vi /usr/share/nginx/html/maintenance.html

Website is Maintaining,Please visit later.

检查效果:
1、本机访问:

2、其他客户机访问:

方法二:
1.修改服务器地址进行访问跳转
ifconfig ens33 20.0.0.7

2、修改域名映射
vi /etc/hosts

20.0.0.7   www.domain.com

检查效果:
本机访问:

基于旧、新域名跳转并加目录

场景:将域名http;//bbs.domain.com下面的发帖都跳转到http://www.domain.com/bbs.且域名跳转后保持参数不变。

vi /etc/nginx.conf
####修改默认站点配置
server { 
        listen       80;
        server_name  bbs.domain.com;
        charset utf-8;

   access_log  logs/bbs.domain.access.log main;
       location /post { 
            rewrite (.+) http://www.domain.com/bbs$1 permanent;
        }

        location / { 
            root  /usr/share/nginx/html;
            index  index.html index.htm;
        }

##### 末行添加
server { 
             listen 80;
             server_name www.domain.com;
             charset utf-8;
             access_log /var/log/nginx/www.domain.com-access.log main;
       location / { 
             root html;
             index index.html index.htm;
    }
}

cd /usr/local/nginx/html/
[root@client3 html]# ls -lh
[root@client3 html]# mkdir -p bbs/post
[root@client3 html]# cd bbs/post/
[root@client3 post]# ls -lh
总用量 0
[root@client3 post]# vi a.html
1234567
[root@client3 post]# systemctl restart nginx
[root@client3 post]# vi /etc/hosts
20.0.0.13  www.domain.com bbs.domain.com

在浏览器上访问
bbs.domain.com/post/a.html
效果展示:

基于参数匹配的跳转

配置需求:
现在访问http://www.domain.com/100-(100|200)-100.html
跳转到http://www.test.com页面。

vi /etc/nginx.conf 
####修改默认站点配置
server { 
        listen       80;
        server_name  www.domain.com;
        charset utf-8;         if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { 
        rewrite (.*) http://www.domain.com permanent;
        } 
        access_log logs/www.domain.acess.log main;

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


#####后面添加的部分删除

nginx -t
systemctl restart nginx

验证效果:
范围内:

范围外:

基于目录下所有php文件跳转

配置环境:
访问http://www.domain.com/upload/1.php跳转到首页

vi /etc/nginx.conf 
####修改默认站点配置
server { 
        listen       80;
        server_name  www.domain.com;

        charset utf-8;
        location ~* /upload/.*\.php$ { 
             rewrite (.+) http://www.domain.com permanent;
        }
        access_log logs/www.domain.acess.log main;
        
        location / { 
            root  html;
            index  index.html index.htm;
        }

在浏览器上访问
效果展示:

不区分大小写

基于最普通url请求的跳转

**环境配置:**访问一个具体的页面跳转到首页,如浏览器访问http://www.domain.com/1/test.html跳转到首页。

vi /etc/nginx.conf 
####修改默认站点配置
server { 
        listen       80;
        server_name  www.domain.com;

        charset utf-8;
        location ~* /1/test.html { 
             rewrite (.+) http://www.domain.com permanent;
        }
        access_log logs/www.domain.acess.log main;

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

在浏览器上访问
效果展示:

本文地址:https://blog.csdn.net/Nipengliang/article/details/110494099