Nginx rewrite模块

Rewrite

跳转场景

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

实际场景

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

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

对域名或参数字符串:
使用if全局变量匹配。使用proxy_pass反向代理。

正则表达式

常用的正则表达式

字符解释
^匹配输入字符串的起始位置
$匹配输入字符串的结束位置
*匹配前面的字符零次或多次
+匹配前面的字符一次或多次
匹配前面的字符零次或一次
.匹配除了“\n”之外的任何单个字符
\将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用
\d匹配纯数字
{n}重复n次
{n,}重复n次或多次
[c]匹配单个字符c
[a-z]匹配a-z小写字母的任意一个
[a-zA-Z]匹配a-z小写字母或A-Z大写字母的任意一个

Rewrite命令

语法

ewrite    < 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匹配

location

分类

location = patt {}    #精准匹配
location  patt {}     #一般匹配
location ~ patt {}    #正则匹配

正则匹配的常用表达式

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

优先级

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

按优先级排列(把最先执行的写在配置文件的最上面)
= 类型
^~ 类型表达式
正则表达式(和*)类型
常规字符串匹配类型,按前缀匹配
通用匹配(/),如果没有其它匹配,任何请求都会匹配到

比较rewrite和location

相同点不同点
都能实现跳转rewrite是在同一域名内更改获取资源的路径。location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器

rewrite会写在location里,执行顺序:
执行server块里面的rewrite指令。执行location匹配。执行选定的location中的rewrite指令。

优先级示例

location = / {	#精确匹配 /,主机名后面不能带任何字符串
    [configuraion A ]	
}

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

location /documents/ {		#匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用
    [configuraion C ]
}

location ~ /documents/abc {		#匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用
    [configuraion D ]
}

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

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

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

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

location /images/abc/1.html {	#如果和正则 ~ images/abc/1.htm相比,正则优先级更高
    [configuraion I ]
}

项目

基本环境

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm   #安装nginx源
yum -y install nginx

基于域名跳转

要求:现在公司旧域名www.old.com有业务需求有变更,需要使用新域名www.new.com代替。
不能废除旧域名,从旧域名跳转到新域名,且保持其参数不变。

vi /etc/nginx.conf 
添加修改
 server_name  www.old.com;
        if ($host = 'www.old.com')
    {
        rewrite ^/(.*)$ http://www.new.com/$1 permanent;
    }

ulimit -n 65535 >> /etc/rc.local 
vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.old.com;
    location / {
    if ($host = 'www.old.com') {
    rewrite ^/(.*)$ http://www.new.com/$1 permanent;
    }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
systemctl stop nginx
systemctl start nginx

访问www.old.com
在这里插入图片描述

成功。

基于客户端IP访问跳转

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

vi /etc/nginx/conf.d/default.conf
server {
    set $rewrite true;  #判断标志$rewrite
    if ($remote_addr = "20.0.0.10") {
       set $rewrite false;
     }     #允许公司内部访问,更改标志位false(这样不会执行下面if判断的rewrite了)
    if ($rewrite = true) {
        rewrite (.+) /error.html;
     }     #如果不是公司IP,加上后缀地址作为标识
    location = /error.html {
      root   /usr/share/nginx/html;
    }      #精确匹配有/error.html后缀的网页

vi /usr/share/nginx/html/error.html
<h1>you cannot visit</h1>

客户端地址为20.0.0.10
在这里插入图片描述

客户端为20.0.0.12
在这里插入图片描述

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

要求:
将域名http://bbc.old.com下面的发帖都跳转到http://www.old.com/bbs,且域名跳转后保持参数不变。

vi /etc/nginx/conf.d/default.conf
server {
    server_name  bbc.old.com;

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

mkdir -p bbs/post
cd bbs/post/
vi abc.html
systemctl stop nginx
systemctl start nginx

在这里插入图片描述

基于参数匹配的跳转

要求:
http://www.test.com/100-(100|200)-100.html跳转到http://www.test.com页面。

vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.test.com;
    access_log  /var/log/nginx/host.access.log  main;
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
       rewrite (.*) http://www.test.com permanent;
    }
    
systemctl stop nginx
systemctl start nginx

访问www.test.com/100-100-10.html
在这里插入图片描述
成功。

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

要求:
访问http://www.test.com/upload/1.php跳转到首页

vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.test.com;
    location ~* /upload/.*\.php$ {
    rewrite (.+) http://www.test.com permanent;
    }

systemctl stop nginx
systemctl start nginx

在这里插入图片描述
成功。

基于最普通url请求的跳转

要求:
访问一个具体的页面跳转到首页,如浏览器访问http://www.test.com/1/test.html跳转到首页。

vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.test.com;
    location ~* ^/1/test.html {
     rewrite (.+) http://www.test.com permanent;
    }

systemctl stop nginx
systemctl start nginx

访问http://www.test.com/1/test.html
在这里插入图片描述
成功。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值