小阿轩yx-Nginx Rewrite

小阿轩yx-Nginx Rewrite

Nginx Rewrite 概述

  • 现在 Nginx 已经成为很多公司作为前端反向代理服务器的首选

实际工作中会遇到很多跳转(重写 URL)的需求

  • 更换域名后需要保持旧的域名能跳转到新的域名上
  • 某网页发生改变需要跳转到新的页面
  • 网站防盗链等等需求

后端使用的Nginx 服务器与Apache 服务器

区别

  • Apache 服务器虽然也能做跳转,规则库也很强大
  • 用 Nginx 跳转效率会更高(注:更有优势)

Rewrite 跳转场景包括

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

Rewrite 跳转实现

Nginx 是通过 ngx_http_rewrite_module 模块

  • 支持 url 重写
  • 支持if条件判断
  • 另外该模块需要 PCRE 支持
  • 应在编译 Nginx 时指定 PCRE 支持,默认已经安装

缺点

  • 不支持 else

根据相关变量重定向和选择不同的配置

从一个location 跳转到另一个location

  • 不过这样的循环最多可以执行 10 次
  • 超过后 Nginx 将返回 500 错误

同时,重写模块包含 set 指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。

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

Rewrite 实际应用场景

实际工作的应用中,Nginx跳转需求有三种方式可实现

  • 可以直接用rewrite 进行匹配跳转
  • 也可以使用if匹配全局变量后跳转
  • 另外,还可以使用location 匹配再跳转

所以rewrite 只能放在 server{}、f、location{}配置段中

Nginx 正则表达式

字符描述
^匹配输入字符串的起始位置
$匹配输入字符串的结束位置
 
*匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”
+“ol!”匹配前面的字符一次或多次。如“ol+”能匹配“o!"及“ol!”、“ol!”,但不能匹配“o”
?匹配前面的字符零次或一次,例如“do(es)?"能匹配“do"或者“does”,"?”等效于”{0,1}”
.匹配除"\n”之外的任何单个字符,若要匹配包括“n"在内的任意字符,请使用诸如“[.\n]”之类的模式
\将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n"匹配一个换行符,而“\$”则匹配“$”
\d匹配纯数字
{n}重复n次
{n,}重复n次或更多次
[c]匹配单个字符c
[a-z]匹配 a-z 小写字母的任意一个
[a-zA-Z]匹配 a-z 小写字母或 A-Z 大写字母的任意一个

Nginx Rewrite 基本操作

Rewrite 语法

  • regex:表示正则匹配规则
  • replacement:表示跳转后的内容
  • flag:表示 rewrite 支持的 flag 标记
rewrite<regex><replacement>[flag];

flag 标记说明

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

(注:如果后面不跟 fag 标记,那么默认是 302 临时重定向

实际工作场景中,还有另一种 return 指定。因为 301 和 302不能简单的只返回状态码,还必须有重定向的 URL,这就是 return 指令无法返回 301 和 302 的原因)

last 和 break 区别

  • last 一般写在 server 和if中,而 break 一般使用在 location 中。
  • last 不终止重写后的 ur 匹配,即新的 ur 会再从 server 走一遍匹配流程,而 break 终止重写后的匹配

Location 分类

location 大致可以分为三类

  • location = patt {} [精准匹配]

  • location patt {} [一般匹配]

  • location ~ patt {} [正则匹配]

(注:精准匹配和一般匹配不需要做详细的说明,主要是正则匹配)

[root@localhost ~]# mkdir -p /var/www/html/aaa
[root@localhost ~]# mkdir -p /var/www/html/bbb
[root@localhost ~]# mkdir -p /var/www/html/ccc

[root@localhost ~]# echo "aaa">/var/www/html/aaa/aaa.html
[root@localhost ~]# echo "bbb">/var/www/html/bbb/index.html
[root@localhost ~]# echo "ccc">/var/www/html/ccc/INDEX.html
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

        location / {
            root   /var/www/html/aaa;
            index  index.html aaa.html;
        }

        location = /index.html {
            root   /var/www/html/bbb;
            index  index.html index.htm;
        }

        location ~ /INDEX.html {
            root   /var/www/html/ccc;
            index  index.html index.htm INDEX.html;
        }

(注:

http://192.168.10.101

访问到的是aaa

http://192.168.10.101/index.html

访问到的是bbb

http://192.168.10.101/INDEX.html

访问到的是ccc)

正则匹配的一些表达式

  • ~:表示执行一个正则匹配,区分大小写

  • ~*:表示执行一个正则匹配,不区分大小写

  • > !~:表示执行一个正则匹配,区分大小写不匹配

  • !~":表示执行一个正则匹配,不区分大小写不匹配

  • ^~:表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他 location

  • =:进行普通字符精确匹配,也就是完全匹配

  • @:它定义一个命名的 location,使用在内部定向时,例如 error_page, try _files

Location 优先级 

Nginx location 配置中 location 的顺序没有太大关系

匹配优先级和 location 表达式的类型有关

  • 相同类型的表达式

  • 字符串长的会优先匹配

按优先级排列说明

  • 等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项

  • ^~类型表达式。一旦匹配成功,则不再查找其他匹配项

  • 正则表达式类型(~~*)的优先级次之

  • 常规字符串匹配类型。按前缀匹配

  • 通用匹配(/),如果没有其它匹配,任何请求都会匹配到

(注:= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求)

从功能看 rewrite 和 location 似乎有点像,都能实现跳转

主要区别

  • rewrite 是在同一域名内更改获取资源的路径
  • 而location 是对一类路径做控制访问或反向代理
  • 还可以proxy_pass 到其他机器
  • 很多情况下rewrite 也会写在 location 里

执行顺序

  • 执行server块里面的 rewrite 指令
  • 执行location 匹配
  • 执行选定的 location 中的 rewrite 指令
#精确匹配 / ,主机名后面不能带任何字符串
location = / { 
    [ configuration A ] 
}

# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求,但是正则和最长字符串会优先匹配
location / { 
    [ configuration B ] 
}

# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续向下搜索,只有后面的正则表达式没有匹配到时,这一条才会起作用 
location /documents/ { 
    [ configuration C ] 
}

# 匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续向下搜索,只有后面的正则表达式没有匹配到时,这一条才会起作用 
location ~ /documents/abc { 
    [ configuration D ] 
}

# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止向下匹配 
location ^~ /images/ { 
    [ configuration E ] 
}

# 匹配所有以 gif,jpg 或 jpeg 结尾的请求,然而所有请求/images/下的图片会被 [ configuration E] 处理,因为^~的优先级更高 
location ~* \.(gif|jpg|jpeg)$ { 
    [ configuration F ] 
}

# 最长字符匹配到 /images/abc,优先级最低 
location /images/abc { 
    [ configuration G ] 
}


# 匹配以/images/abc 开头的,优先级次之 
location ~ /images/abc { 
    [ configuration H ] 
}

# 匹配/images/abc/1.html 文件,如果和正则~ /images/abc/1.html 相比,正则优先级更高
location /images/abc/1.html { 
    [ configuration I ] 
}

(总结:

如果是匹配某个具体文件

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

如果是用目录做匹配访问某个文件

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

Rewrite 常见示例

案例环境

  • 192.168.10.101 www.benet.com 

  • 192.168.10.102 www.accp.com

设置好客户端和服务器端的hosts文件

浏览器访问www.benet.com,跳转到www.accp.com

安装 Nginx 服务

[root@localhost ~]# rpm -Uvh http:/nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]# yum -y install nginx

修改 Nginx 默认站点配置文件

[root@localhost ~l# vim /etc/nginx/conf.d/default.conf
server {
    listen    80:
    server name ww.kgc.com;
    #charset koi8-r;
access log /var/log/nginx/www.kgc.com-access.logmain:
location /{
root    /usr/share/nginx/html;
index index.html index.htm;
#error_page 404    /404.html;
…./省略部分
[root@localhost ~]# systemctl start nginx

Rewrite 示例

(注:在做每一步场景测试之前请确定 www.kgc.com 域名可以正常解析,每做下一个场景之前请删除上一个场景的配置。另外,要清除浏览器缓存)

flag 标记说明

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

基于域名的跳转

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

server {
        listen       80;
        server_name  www.benet.com;
        charset utf-8;
        #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
        if ($host = 'www.benet.com') {
          rewrite ^/(.*)$ http://www.accp.com/$1 permanent;
        }
    }
}
[root@localhost ~]# systemctl restart nginx

 域名直接跳转

  • F12 可以清楚的看到从旧域名 www.benet.com 跳转到了新域名 www.accp.com 上,状态码是 301 永久重定向

  • b:域名后面加参数跳转

基于客户端 IP 访问跳转

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

server {
    listen       80;
    server_name  www.benet.com;
    #access_log  /var/log/nginx/host.access.log  main;

    set $rewrite true;
    if ($remote_addr = "192.168.10.51") {
        set $rewrite false;
    }
    if ($rewrite = true) {
    rewrite (.+) /maintenance.html;
    }

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

#	if ($host = 'www.benet.com') {
#	  rewrite ^/(.*)$ http://www.accp.com/$1 permanent; 
#	}
  }
}
  • (.+)  匹配整个URL,匹配上以后,就在URL后面加上/maintenance.html

  • .+  匹配任意字符1次或多次

  • .*  匹配任意字符0次或多次

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# echo "Website is Maintaining,Please visit later."> /usr/local/nginx/html/maintenance.html

(注:

$host = 'www.benet.com'  判断获取到得请求URL是否为www.benet.com     另外,=号两边有空格

permanent:返回 301 永久重定向,浏览器地址栏会显示跳转后的 URL 地址,爬虫更新 url,没有则返回302)

^/(.*)$

  • ^  指的是以任何字符或字符串开始的域名都会匹配上

  • /  指得是url后跟的/,比如www.benet.com/index.html

  • .*  匹配任意多个字符,注意这里的(),括号里的内容会用后面的$1调用

  • $   结尾

(注:Rewrite 语法rewrite<regex><replacement>[flag];

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

[root@localhost html]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir -p bbs/post
[root@localhost html]# cd bbs/post
[root@localhost post]# echo "i am bbs">index.html
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  bbs.benet.com;
    #access_log  /var/log/nginx/host.access.log  main;

    location /post {
        rewrite (.+) http://www.benet.com/bbs$1 permanent;
    }

    location / {
        root   html;
        index  index.html index.htm;
    }
}
[root@localhost ~]# systemctl restart nginx

客户端添加域名解析

  • 192.168.10.101 bbs.benet.com

基于参数匹配的跳转

root@localhost post]# vim /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  bbs.benet.com;
    #access_log  /var/log/nginx/host.access.log  main;

    if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.*) http://www.benet.com permanent;
    }

    location / {
        root   html;
        index  index.html index.htm;
    }
}
  • \d+  匹配任意个数字

[root@localhost ~]# systemctl restart nginx

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

[root@localhost post]# vim /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  bbs.benet.com;
    #access_log  /var/log/nginx/host.access.log  main;
    
    location ~* /upload/.*\.php$ {
        rewrite (.+) http://www.benet.com permanent;
    }

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

注:

  • ~*  不区分大小写

  • ~   区分大小写

  • .*\.php  一个文件的名字   名字.后缀   任意名字.php

[root@localhost ~]# systemctl restart nginx

浏览器访问 http://www.benet.com/upload/1.php会跳转到http://www.benet.com

基于最普通一条 url 请求的跳转

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

server {
    listen       80;
    server_name  bbs.benet.com;
    #access_log  /var/log/nginx/host.access.log  main;

    location ~* ^/1/test.html {
        rewrite (.+) http://www.benet.com permanent; 
    }

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

浏览器访问http://www.benet.com/1/test.html 跳转到http://www.benet.com

小阿轩yx-Nginx Rewrite

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值