Nginx Rewrite概述以及常见的用法

一、Nginx Rewrite概述

1.1、Nginx Rewrite概述

概述:

    现在Nginx已经成为很多公司作为前端反向代理服务器的首选,在实际工作中往往会
    遇到很多跳转(重写URL)的需求。比如更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。如果在后端使用的Apache服务器,虽然也能做跳转,规则库也很强大,但是用Nginx跳转效率会更高。

●跳转场景:
1、可以调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求。
2、为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
3、网址换新域名后,让旧的访问跳转到新的域名上。例如,访问京东的360buy.com会跳转到jd.com。
4、根据特殊变量、目录、客户端的信息进行URL调整等。

跳转实现:
Nginx是通过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。另外该模块需要 PCRE支持,应在编译Nginx时指定PCRE 支持,默认已经安装。根据相关变量重定向和选择不同的配置,从一个location跳转到另一个location,不过这样的循环最多可以执行10次,超过后Nginx将返回500错误。同时,重写模块包含set指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。rewrite功能就是,使用Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。

在这里插入图片描述
●Rewrite实际场景
■Nginx跳转需求的实现方式
①使用rewrite进行匹配跳转
②使用if匹配全局变量后跳转
③使用location匹配在跳转

二、Nginx Rewrite基本操作

2.1、Rewrite命令

●Rewrite命令语法
rewrite < regex > < replacement > [flag]
regex:正则表达式
replacement :跳转后的内容
flag:rewrite支持的flag标记

●flag标记说明

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

●last 和 break 比较

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

2.2、location分类和优先级

2.2.1、location分类

  1. location= patt {} [精准匹配]
  2. location patt {} [一般匹配]
  3. location ~ patt {} [一般匹配]
标记说明
~执行一个正则匹配,区分大小写
~*执行一个正则匹配,不区分大小写
!~执行一个正则匹配,区分大小写不匹配
!~*执行一个正则匹配,不区分大小写不匹配
^~普通字符匹配:使用前缀匹配,如果匹配成功,则不再匹配其他location
=普通字符精确匹配,也就是完全匹配
@定义一个命名的location,使用在内部定向时

2.2.2、location优先级

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

2.2.3、location优先级规则

  • 匹配某个具体文件
    (location=完整路径) > (location ~ 完整路径) > (location ~ *完整路径) >(location~完整路径) > (location完整路径) > (location /)
    用目录做匹配访问某个文件
    (location=目录) > (location ^ ~ 目录/) > (location ~ 目录 ) > (location~*目录) > (location目录) > (location /)

三、Rewrite使用场景实验

搭建基础环境

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.51xit.top;   ## 修改域名

        #charset koi8-r;
        ## 指定 www.51xit.top 的日志文件
        access_log  /var/log/nginx/www.51xit.top.access.log;   ## 修改此处

        location / {
            root   html;
            index  index.html index.htm;
        }
== >> wq 保存
[root@localhost ~]# mkdir -p /var/log/nginx  ##创建存放日志文件的目录
[root@localhost ~]# touch /var/log/nginx  /www.51xit.top.access.log    ##存放www.51xit.top日志的文件
[root@localhost nginx]# ll
total 8
-rw-r--r-- 1 root root 5973 Sep  7 07:01    www.51xit.top.access.log    ##查看文件生成成功
[root@localhost ~]# 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 ~]# killall -s HUP nginx   ##刷新nginx配置

测试:打开浏览器输入 20.0.0.25 测试一下,是否正常
在这里插入图片描述

3.1、基于域名测试

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.html index.htm;
              #### 新增部分 ####
              if ($host = 'www.51xit.top') {  ##这边是老域名
                rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;    
                ##使用正则表达式匹配 “/开头和任意字符结尾”,然后跳转到新的www.52xit.top
                }
             }
[root@localhost ~]# 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 ~]# killall -s HUP nginx

测试:打开浏览器输入 www.51xit.top 跳转到 www.52xit.top

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

3.2、基于客户端IP访问跳转

  • 2、基于客户端IP访问跳转。例如今天公司业务新版本上线,所有IP访问任何都显示一个固定维护页面,只有公司IP才能访问正常
server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;  ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉

        access_log  /var/log/nginx/www.51xit.top.access.log;

        set $rewrite true;  ## 新增的

        if ($remote_addr = '20.0.0.10') {    ## 我们这边设置的是管理员IP
        set $rewrite false;  ##管理员IP设为 flase
        }

        if ($rewrite = true) {  ## 如果匹配的不是 20.0.0.10
        rewrite (.+) /wh.html;  ## 跳转到 维护网页
        }

        location = /wh.html {  ## 设置维护网页的结尾域名
        root /usr/local/nginx/html/;  ## 维护网页存放的路径
        }

        location / {    ## 之前在这里做的配置加上 #root   html;
            index  index.html index.htm;
#              if ($host = 'www.51xit.top') {
#                rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;
#                }
[root@localhost ~]# 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 ~]# killall -s HUP nginx
[root@localhost ~]# cd /usr/local/nginx/html/ 
[root@localhost html]# ll
total 8
-rw-r--r-- 1 root root 494 Sep  2 22:47 50x.html
-rw-r--r-- 1 root root 612 Sep  2 22:47 index.html
[root@localhost html]# cp index.html wh.html   ##创建维护页面
[root@localhost html]# vi wh.html 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>网页维护中,暂时无法访问!!!</h1>
</body>
</html>
== >> wq 保存
[root@localhost html]# killall -s HUP nginx

测试:打开浏览器输入 20.0.0.25,会显示网页维护中,暂时无法访问!!!

在这里插入图片描述

这时候我们把配置文件里面的管理员IP设为 20.0.0.1,因为我虚拟机网卡的IP20.0.0.1,所以会进入正常的网页
 if ($remote_addr = '20.0.0.1') {    ## 新增的
        set $rewrite false;  ##新增的
        }
== >> wq 保存
[root@localhost ~]# killall -s HUP nginx

在这里插入图片描述

3.3、基于旧域名跳转到新域名

基于旧域名跳转到新域名后面加目录,录入现在访问的是 http://www.51xit.top/bbs
现在需要将这个域名下面的都转到 http://www.52xit.top/new/bbs/

[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;  ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉

        access_log  /var/log/nginx/www.51xit.top.access.log;

        ####新增代码####
        location /bbs {   输入http://www.51xit.top/bbs 跳转到 http://www.52xit.top/new/bbs/ 
        rewrite (.+) http://www.52xit.top/new$1 permanent;
        }

#######还是一样之前做的配置全部加上###########
#        set $rewrite true;
 
#        if ($remote_addr = '20.0.0.1') {
#        set $rewrite false;
#        }
 
#        if ($rewrite = true) {
#        rewrite (.+) /wh.html;
#        }
 
#        location = /wh.html {
#        root /usr/local/nginx/html/;
#        }
[root@localhost ~]# 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 ~]# killall -s HUP nginx

测试:浏览器输入 www.51xit.top/bbs 会跳转到 www.51xit.top/new/bbs

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

3.4、基于参数匹配的跳转

[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;  ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉

        access_log  /var/log/nginx/www.51xit.top.access.log;

        ##### 新增的 #####
        ## 输入 www.51xit.top/100-(100-200)—纯数字.html访问网页
        if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.*) http://www.51xit.top permanent;
        }
         
          ######## 之前的加上###########
#        location /bbs {
#        rewrite (.+) http://www.52xit.top/new$1 permanent;
#        }
[root@localhost ~]# 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 ~]# killall -s HUP nginx

测试:在浏览器输入 www.51xit.top/100-200-100.html 测试成功!!!

在这里插入图片描述

3.5、基于目录下所有以php结尾的文件跳转

基于目录下所有以php结尾的文件跳转,访问http://www.51xit.top/upload/1.php跳转到首页

[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;  ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉

        access_log  /var/log/nginx/www.51xit.top.access.log;

         #### 新增内容 ####
         ### 输入 /upload/任意字符.php结尾 来访问网页
        location ~* /upload/.*\.php$ {
        rewrite (.+) http://www.51xit.top permanent;
        }

        ######## 之前的加上###########
        #if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        #rewrite (.*) http://www.51xit.top permanent;
        #}
[root@localhost ~]# 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 ~]# killall -s HUP nginx

测试:在浏览器输入http://www.51xit.top/upload/bbs/1.php,测试成功!!!

在这里插入图片描述

3.6、基于普通一条url请求的跳转到首页

  • 基于普通一条url请求的跳转,访问一个具体的页面跳转到首页
[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;  ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉

        access_log  /var/log/nginx/www.51xit.top.access.log;
        
        #### 新增内容 ####
        ### 输入 /1/test.html 结尾访问页面
        location ~* ^/1/test.html {
        rewrite (.+) http://www.51xit.top permanent;
        }

         ######## 之前的加上###########
#        location ~* /upload/.*\.php$ {
#        rewrite (.+) http://www.51xit.top permanent;
#        }
[root@localhost ~]# 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 ~]# killall -s HUP nginx

在这里插入图片描述

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值