理论+实操:nginx rewrite 重写功能和nginx的正则表达式

前言

  • rewrite
  • Nginx的正则表达式

一:Rewrite 跳转场景

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

二: Rewrite 跳转实现

服务__ 协议 __功能模块

url 资源定位路径

  • nginx————支持url重写、支持if条件判断,但不支持else
  • 跳转————循环最多可以执行10次,超过后nginx将返回500代码错误
  • rewrite————使用nginx’全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向

在这里插入图片描述

三: Rewrite 使用场景

3.1 nginx跳转需求的实现方式

  • 使用rewrite进行匹配跳转
  • 使用if匹配全局变量后跳转
  • 使用location匹配再跳转

3.2 rewrite放在 server{},if{},location{}段中

3.3 对域名或参数字符串

  • 使用if全局变量匹配
  • 使用proxy_pass反向代理

四: nginx正则表达式

4.1 常用的正则表达式元字符

在这里插入图片描述

五:Rewrite 命令

5.1 语法

在这里插入图片描述

5.2 flag标记说明

在这里插入图片描述

5.3 last 和 break 比较

在这里插入图片描述

小结:

  • 简述Rewrite跳转实现
  • 什么是正则表达式?列举至少三个常用的flag
  • 比较Rewrite命令flag参数值:last和break,说出二者的一同

正则表达式:

处理字符串的工具,一某种规则去处理,正砸表达式就是定义其中的规则

六:location分类

6.1 分类

在这里插入图片描述

6.2 正则匹配的常用表达式

在这里插入图片描述

七:location优先级

7.1 相同类型的表达式,字符串长的会优先匹配

7.2 按优先级排列

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

八: 比较rewrite 和location

8.1 相同点

  • 都能实现跳转

8.2 不同点

  • rewrite是在同一域名内更改获取资源的路径
  • location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器

8.3 rewrite会写在location里,执行顺序

  • 执行server块里面的rewrite指令
  • 执行location匹配
  • 执行选定的location中的rewrite指令

九 : Location优先级的示例

在这里插入图片描述

在这里插入图片描述

十 : location优先级规则(从高到低排列)

10.1 匹配某个具体的文件

  • location = 完整路径
  • location ^~ 完整路径
  • location ~* 完整路径
  • location ~ 完整路径
  • location 完整路径
  • location /

10.2 用目录做匹配访问某个文件

  • location = 目录
  • location ^~ 目录
  • location ~ 目录
  • location ~* 目录
  • location 目录
  • location /

十一:应用场景测试

11.1 基于域名的跳转

  • 公司旧域名www.domain.com,因业务需求有变更,需要使用新域名www.newdomain.com代替
    • 不能废除旧域名
    • 从旧域名跳转到新域名,且保持其参数不变

在这里插入图片描述

  • 浏览器测试输入某个具体的URL——http://www.domain.com/test/1/index.php
  • 跳转到http://www.newdomain.com/test/1/index.php
  • 从headers里面可以看到301实现了永久重定向跳转,且域名后的参数也正常跳转
[root@localhost bin]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
获取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.IHyTHc: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost bin]# yum install nginx -y 

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@localhost ~]# cd /abc/LNMP-C7/LNMP-C7/
[root@localhost LNMP-C7]# ls
Discuz_X3.4_SC_UTF8.zip
mysql-boost-5.7.20.tar.gz
ncurses-5.6.tar.gz
nginx-1.12.2.tar.gz
php-5.6.11.tar.bz2
php-7.1.10.tar.bz2
php-7.1.20.tar.bz2
php-7.1.20.tar.gz
zend-loader-php5.6-linux-x86_64_update1.tar.gz
[root@localhost LNMP-C7]# tar -zxvf nginx-1.12.2.tar.gz -C /opt
[root@localhost LNMP-C7]# useradd -M -s /sbin/nologin nginx
[root@localhost LNMP-C7]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ pcre pcre-devel make zlib-devel -y
[root@localhost nginx-1.12.2]#  ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost conf]# vim nginx.conf
 37         server_name  www.accp.com;
 41         access_log  logs/www.accp.com/access.log  main;
[root@localhost conf]# yum install bind -y
[root@localhost conf]# vim /etc/named.conf 
 13         listen-on port 53 { any; };
 21         allow-query     { any; };
[root@localhost conf]# vim /etc/named.rfc1912.zones 
 25 zone "accp.com" IN {
 26         type master;
 27         file "accp.com.zone";
 28         allow-update { none; };
 29 };
[root@localhost conf]# cd /var/named/
[root@localhost named]# ls
data     named.ca     named.localhost  slaves
dynamic  named.empty  named.loopback
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone 
www     IN      A       192.168.247.202
[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service 
[root@localhost named]# setenforce 0
[root@localhost named]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost named]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# cd logs/
[root@localhost logs]# mkdir www.accp.com
[root@localhost logs]# ls
error.log  www.accp.com
[root@localhost logs]# nginx 
[root@localhost logs]# netstat -natp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6087/nginx: master  

打开客户机,配置dns

在这里插入图片描述

在这里插入图片描述

[root@localhost logs]# vim /usr/local/nginx/conf/nginx.conf

		location / {
           #域名重定向
‘                 if ($host = 'www.accp.com') {
 ’                       rewrite ^/(.*)$ http://www.kgc.com/$1 permanent;
  ‘               }
                 root /html;
                 index index.html index.htm;
        }

[root@localhost logs]# vim /etc/named.rfc1912.zones 
zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};
[root@localhost logs]# cd /var/named/
[root@localhost named]# ls
accp.com.zone  dynamic   named.empty      named.loopback
data           named.ca  named.localhost  slaves
[root@localhost named]# cp -p accp.com.zone kgc.com.zone

[root@localhost named]# systemctl restart named
[root@localhost named]# killall -1 nginx

在这里插入图片描述
在这里插入图片描述

在网址后面追加网页,也会变

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
 37         server_name  bbs.accp.com;
 42         location /post {
 43                 rewrite (.+) http://www.accp.com/bbs$1 permanent;
 44         }
[root@localhost html]# cd -
/var/named
[root@localhost named]# vim accp.com.zone 
[root@localhost named]# cat accp.com.zone 
$TTL 1D
@	IN SOA	@ rname.invalid. (
					0	; serial
					1D	; refresh
					1H	; retry
					1W	; expire
					3H )	; minimum
	NS	@
	A	127.0.0.1
bbs	IN	A	192.168.247.202
[root@localhost named]# killall -3 nginx
[root@localhost named]# nginx

[root@localhost named]# systemctl restart named

在这里插入图片描述

在这里插入图片描述

11.2 基于客户端IP访问跳转

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

在这里插入图片描述

布尔类型的数据可以直接进行if判断,判断访问者是否合法

如果不合法,在原有地址的基础上追加一条/maintenance.html页面

对追加的页面内写入想要输出的内容

在这里插入图片描述

[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf

 42 #设置是否合法的IP标志
 43         set $rewrite true;
 44 #判断是否为合法IP,是否是允许的IP
 45         if ($remote_addr = "192.168.247.139"){
 46                 set $rewrite false;
 47         }
 48 #不被允许的IP进行判断,打上标记
 49         if ($rewrite = true){
 50                 rewrite (.+) /maintenance.html;
 51         }
 52 #匹配标记进行跳转站点
 53         location = /maintenance.html {
 54                 root html;
 55         }
[root@localhost named]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# vim maintenance.html
[root@localhost html]# killall -3 nginx 
[root@localhost html]# nginx 

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

11.3 基于参数匹配的跳转——————跳转到首页

在这里插入图片描述

[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
 37         server_name  www.accp.com;
 42         if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
 43                 rewrite (.*) http://www.accp.com permanent;
 44         }
[root@localhost named]# vim accp.com.zone 
[root@localhost named]# cat accp.com.zone 
$TTL 1D
@	IN SOA	@ rname.invalid. (
					0	; serial
					1D	; refresh
					1H	; retry
					1W	; expire
					3H )	; minimum
	NS	@
	A	127.0.0.1
www	IN	A	192.168.247.202
[root@localhost named]# systemctl restart named
[root@localhost named]# killall -3 nginx
[root@localhost named]# nginx

在这里插入图片描述

在这里插入图片描述

11.4 基于目录下所有PHP文件跳转

  • 访问PHP文件都会跳转到首页
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
 42         location ~* /upload/.*\.php$ {
 43                 rewrite (.+) http://www.accp.com permanent;
 44         }
[root@localhost named]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost named]# killall -3 nginx
[root@localhost named]# nginx

在这里插入图片描述

在这里插入图片描述

11.5 基于最普通URL请求的跳转————跳转到首页

在这里插入图片描述

[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
 42         location ~* ^/abc/123.html {
 43                 rewrite (.+) http://www.accp.com permanent;
 44         }
[root@localhost named]# killall -3 nginx
[root@localhost named]# nginx

在这里插入图片描述

在这里插入图片描述

11.5 基于最普通URL请求的跳转————跳转到首页

[外链图片转存中…(img-82cagtIv-1577351501751)]

[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
 42         location ~* ^/abc/123.html {
 43                 rewrite (.+) http://www.accp.com permanent;
 44         }
[root@localhost named]# killall -3 nginx
[root@localhost named]# nginx
发布了87 篇原创文章 · 获赞 26 · 访问量 4527

猜你喜欢

转载自blog.csdn.net/Lfwthotpt/article/details/103719160