centos 7.6——Nginx中rewrite模块应用(location)——基于域名的跳转等

centos 7.6——Nginx中rewrite模块应用(location)——基于域名的跳转等

一、Nginx Rewrite概述

什么是rewrite?

rewrite重写是由ngx_http_rewrite_module模块提供,主要是实现URI改写功能,并且此模块是Nginx默认安装的模块。通过
rewrite,用户可以实现url重定向,根据regex规则来匹配内容跳转到replacement,结尾以flag标记。

1.1 Rewrite跳转场景

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

1.2 Rewrite跳转实现

ngx_http_rewrite_module

  • Nginx : 支持URL重写、支持if条件判断,但不支持else
  • 跳转 :循环最多可以执行10次,超过后Nginx将返回500错误
  • PCRE支持
  • 重写模块set指令

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

1.3 Nginx跳转需求的实现方式

  • 使用rewrite进行匹配跳转
  • 使用if匹配全局变量后跳转
  • 使用location匹配再跳转
  • rewrite放在 server{},if{},location{} 段中
  • location只对域名后边的除去传递参数外的字符串起作用
  • 对域名或参数字符串
  • 使用if全局变量匹配
  • 使用proxy_pass反向代理

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

在这里插入图片描述

1.5 Rewrite命令语法

1.5.1 rewrite 命令语法


rewrite      <regex>       <repalcement>         [flag];
             (正则)         (跳转的内容)         rewrite 支持的flag标记
             

1.5.2 last和break比较

last break
使用场景 一般写在server和if中 一般使用在location中
URL匹配 不终止重写后的url匹配 终止重写后的url匹配

1.3.3flag标记说明

标记 说明
last 相当于Apache的[L]标记,表示完成rewrite
break 本条规则匹配完成即终止,不再匹配后面的任何规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url
permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url

1.6 location分类

1.6.1分类

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

1.6.2 正则匹配的常用表达式

在这里插入图片描述

1.6.3 location 优先级

  • 相同类型的表达式,字符串长的会优先匹配
  • 按优先级排列
  • = 类型
  • ^~ 类型表达式
  • 正则表达式(~和 ~*)类型
  • 常规字符串匹配类型,按前缀匹配
  • 通用匹配(/),如果没有其它匹配,任何请求都会匹配到

1.6.4 比较 rewrite 和 location

相同点

  • 都能实现跳转

不同点

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

1.6.5 Location优先级的示例 2-1

在这里插入图片描述

扫描二维码关注公众号,回复: 11629695 查看本文章

1.6.6 Location优先级的示例 2-2

在这里插入图片描述

1.6.7 location优先级规则

  • 匹配某个具体文件

(location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径) > (location ~ 完整路径) > (location 完整路径) > (location /)

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

(location = 目录) > (location ^~ 目录/) > (location ~ 目录) > (location ~* 目录) > (location 目录) > (location /)

二、Nginx Rewrite基本操作

安装Nginx服务

安装nginx源

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm


yum install -y nginx

修改默认站点配置文件:etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.domain.com;
    #charset koi8-r;
    access_log  /var/log/nginx/www.domain.com-access.log  main;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    ......//省略部分
}

systemctl start nginx

==== 注 意=====

  • 确定域名可以正常解析
  • 做下一个场景前,要删除上一个场景的配置
  • 及时清除浏览器缓存

2.1 基于域名的跳转

公司旧域名www.kgc.com,因业务需求有变更,需要使用新域名
www.ttyy778899.com代替

  • 不能废除旧域名
  • 从旧域名跳转到新域名,且保持其参数不变


先做一个DNS域名, www.kgc.com


[root@promote html]# vim /etc/named.conf

 listen-on port 53 { any; };
  allow-query     { any; };
  
[root@promote html]# vim /etc/named.rfc1912.zones 

  zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};
zone "ttyy778899.com" IN {        type master;
        file "ttyy778899.com.zone";
        allow-update { none; };
};   
[root@promote html]# cd /var/named/
[root@promote named]# ll
总用量 20
drwxrwx---. 2 named named   23 812 14:26 data
drwxrwx---. 2 named named   60 812 19:59 dynamic
-rw-r-----. 1 root  named  167 812 14:26 kgc.com.zone
-rw-r-----. 1 root  named 2253 45 2018 named.ca
-rw-r-----. 1 root  named  152 1215 2009 named.empty
-rw-r-----. 1 root  named  152 621 2007 named.localhost
-rw-r-----. 1 root  named  168 1215 2009 named.loopback
drwxrwx---. 2 named named    6 61 23:26 slaves
[root@promote named]# cp -p named.localhost kgc.com.zone

[root@promote named]# vim kgc.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.75.131

[root@promote named]# cp -p kgc.com.zone ttyy778899.com.zone
[root@promote named]# vim ttyy778899.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.75.131
~                                  
~                                 
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

[root@localhost ~]#yum install -y nginx
[root@promote named]# rpm -q nginx
nginx-1.18.0-1.el7.ngx.x86_64
[root@promote named]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
[root@promote named]# vim /etc/nginx/conf.d/default.conf 
server {
    listen       192.168.75.131:80;
    server_name  www.kgc.com;

    
     location / {
            if ($host = 'www.kgc.com') {
           rewrite ^/(.*)$ http://www.ttyy778899.com/$1 permanent;          }
        root   /usr/share/nginx/html;
       }
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote named]# systemctl restart nginx
[root@promote named]# 


虚拟机win10 浏览器输入旧的域名www.kgc.com 之后跳转到www.ttyy778899.com

在这里插入图片描述

2.2基于客户端ip访问跳转

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


[root@promote named]# vim /etc/nginx/conf.d/default.conf 

 set $rewrite true;      #判断标志$rewrite
    if ($remote_addr = "192.168.75.30") {
     set $rewrite false;     # 允许公司内部访问,更改标识位false
    }
    if ($rewrite = true) {     #如果不是公司ip,加上后缀地址作为标识
    rewrite (.+) /error.html;  
   }
   location = /error.hmtl {
   root /usr/share/nginx/nginx/html;
   }
   location / {
   root /usr/share/nginx/html;
   index  index.html index.htm;
   }
[root@promote named]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote named]# systemctl restart nginx

使用虚拟机win10 IP地址 192.168.75.88不属于公司内部网络访问www.kgc.com失败

在这里插入图片描述

使用虚拟机win10 IP地址 192.168.75.30属于公司内部IP地址访问www.kgc.com成功

在这里插入图片描述

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

例如现在访问的是 http://bbs.kgc.com,
现在需要将这个域名下面的发帖都跳转到http://www.kgc.com/bbs,注意保持域名跳转后的参数不变。


[root@promote named]# vim /etc/nginx/conf.d/default.conf 

 location /post {
   rewrite (.+) http://www.kgc.com/bbs$1 permanent;
}
[root@promote named]# cd /var/named/
[root@promote named]# vim kgc.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.75.131
~  
[root@promote named]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote named]# systemctl restart nginx
[root@promote named]# systemctl restart named

                                 

虚拟机win10 输入 http://bbs.kgc.com/post/abc.html 之后还是继续跳转到www.kgc.com/bbs/post/abc.html验证成功
现在访问的是 http://bbs.kgc.com,现在需要将这个域名下面的发帖都跳转到http://www.kgc.com/bbs,注意保持域名跳转后的参数不变。**
浏览器需要清空缓存
在这里插入图片描述

2.4 基于参数匹配的跳转,例如现在访问

http://www.kgc.com/100-100-100.html

http://www.kgc.com/100-200-任意数字.html

跳转到http://www.kgc.com 页面


先做一个DNS域名, www.kgc.com

[root@promote html]# vim /etc/named.conf

 listen-on port 53 { any; };
  allow-query     { any; };
[root@promote html]# vim /etc/named.rfc1912.zones 
  zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};
[root@promote named]# vim kgc.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.75.131
~                                 
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

[root@localhost ~]#yum install -y nginx
[root@promote named]# rpm -q nginx
nginx-1.18.0-1.el7.ngx.x86_64
[root@promote named]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
[root@promote named]# vim /etc/nginx/conf.d/default.conf 

server {
    listen       192.168.75.131:80;
    server_name  www.kgc.com;

   if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {   //直接在server中写,不要在location中写
       rewrite (.*) http://www.kgc.com permanent;
}

[root@promote nginx]# systemctl restart nginx
[root@promote nginx]# iptables -F
[root@promote nginx]# setenforce 0
[root@promote nginx]# systemctl restart named

虚拟机win0 验证

http://www.kgc.com/100-100-100.html
在这里插入图片描述

在这里插入图片描述

2.5 基于目录下所有结尾的文件跳转,访问http://www.kgc.com/upload/1.php跳转到首页。


[root@promote nginx]# vim /etc/nginx/conf.d/default.conf 

 location ~* /upload/.*\.php$ {
  rewrite (.+) htpp://www.kgc.com permanent;
}

[root@promote nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote nginx]# systemctl restart nginx
[root@promote nginx]# 

虚拟机win10 验证访问http://www.kgc.com/upload/1.php
在这里插入图片描述

在这里插入图片描述

2.6 基于最普通一条url请求的跳转,访问一个具体的页面跳转的首页


[root@promote nginx]# vim /etc/nginx/conf.d/default.conf
   listen       192.168.75.131:80;
    server_name  www.kgc.com;

  
   location ~* ^/1/test.html {
    rewrite (.+) http://www.kgc.com permanent;   //+号,代表一次或者多次, “ . ”点号是匹配除了“\n” 的任意字符
}

[root@promote nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote nginx]# systemctl restart nginx
[root@promote nginx]# 

win10 虚拟机验证访问 http://www.kgc.com/1/test.html

在这里插入图片描述

注:该实验思路,当你想通过链接直接访问淘宝购物车里的商品,会弹出登录用户。
这里就是对某个网页的页面进行权限设置,无法直接通过网页的链接进行直接的访问。只能经过首页验证等方式才能访问。

拓展小结

匹配内容是URI
站点目录:/d/www
URL:http://127.0.0.1/admin/index.php
对应本地文件:/d/www/admin/index.php,如果是windows文件系统,由于不区分大小写,如果站点下的目录是Admin,那么这里的文件地址会变成:/d/www/Admin/index.php,下面的URI也会跟着变。
URI:admin/index.php,这里的URI是从文件系统去掉站点目录的部分与“%{REQUEST_URI}”有区别的。如果站点下的目录是Admin,URI=Admin/index.php

%{REQUEST_URI}:/admin/index.php,是URL去掉域名的部分

%{REQUEST_FILENAME}:URL对应本地文件,这里有几点要注意的地方:
1.windows文件系统下,会自动根据本地的文件夹大小写进行转换
2.FILENAME会自动去掉多余的部分,如/d/www/admin/setting/index.php,如果admin是个空文件夹,里面没有任何东西,这里的%{REQUEST_FILENAME}=,估计apache的文件系统是先从顶级目录往下遍历,当某个目录不存时,则从这个不存在的目录地址中断

下面是可以用作if判断的全局变量

$args : #这个变量等于请求行中的参数,同$query_string

$content_length : 请求头中的Content-length字段。

$content_type : 请求头中的Content-Type字段。

$document_root : 当前请求在root指令中指定的值。

$host : 请求主机头字段,否则为服务器名称。

$http_user_agent : 客户端agent信息

$http_cookie : 客户端cookie信息

$limit_rate : 这个变量可以限制连接速率。

$request_method : 客户端请求的动作,通常为GET或POST。

$remote_addr : 客户端的IP地址。

$remote_port : 客户端的端口。

$remote_user : 已经经过Auth Basic Module验证的用户名。

$request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。

$scheme : HTTP方法(如http,https)。

$server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

$server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。

$server_name : 服务器名称。

$server_port : 请求到达服务器的端口号。

$request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。

$uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。

$document_uri : 与$uri相同。

rewrite模块

    URI跟URL介绍
    什么是uri?统一标识符,拿www.abc.com/aw/wd/举例,那么rui就是/aw/wd/这部分数据(也有可能是图片,html网页,如果是伪静态的话,那就得看配置是啥玩意了
    什么是url?  统一定位符,还是拿www.abc.com/aw/wd/举例,那么整个www.abc.com/aw/wd/就是url        

    什么是rewrite?
    rewrite重写是由ngx_http_rewrite_module模块提供,主要是实现URI改写功能,并且此模块是Nginx默认安装的模块。通过rewrite,用户可以实现url重定向,根据regex规则来匹配内容跳转到replacement,结尾以flag标记。

    工作方式
    rewrite匹配默认是按照从上至下的顺序进行匹配,如果没有添加flag参数,直到把最后一个rewrite匹配完才终止,如果最后一个都没有匹配到内容则返回500,如果想要提前终止就得flag参数来帮忙实现。

rewrite模块常见指令

if (条件) {} #设定条件,再进行重写
set #后面+值,可以给变量赋值
return #返回状态码信息,并且支持自定义信息
rewrite #重写 (这个是重点)
rewrite_log # rewrite日志

猜你喜欢

转载自blog.csdn.net/weixin_42099301/article/details/107941987