Nginx Rewrite

一,常用正则表达式:

字符	描述
\	将后面接着的字符标记为一个特殊字符或者一个原义字符或一个向后引用
^	匹配输入字符串的起始位置
$	匹配输入字符串的结束位置
*	匹配前面的字符零次或者多次
+	匹配前面字符串一次或者多次
?	匹配前面字符串的零次或者一次
.	匹配除“\n”之外的所有单个字符
(pattern)	匹配括号内的pattern

二,rewrite模块(ngx_http_rewrite_module)
nginx通过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。另外该模块需要PCRE支持,应在编译nginx时指定PCRE支持。根据相关变量重定向和选择不同的配置,从一个location跳转到另一个location,不过这样的循环最多可以执行10次,超过后nginx将返回500错误。同时,重写模块包含set指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。学习rewrite之前要对正则表达式要很熟悉,下面先给出一些常用的正则表达式元字符。

三,Rewrite执行顺序如下
执行 server 块里面的 rewrite 指令。
执行 location 匹配。

执行选定的 location 中的 rewrite 指令

http {
server {
rewrite                                             #优先级1
location ~* \.(jpgl gif | swf)$ {
rewrite                                             #优先级2
valid referers none blocked *.dog.com.dog.com;
if($invalid_referer ) {
rewrite^/ http://www.dog.com/error.png;             #优先级3
}
}

三,Rewrite命令语法

1 rewrite [flag];
2 :正则表达式
3 :跳转后的内容
4 [flag]rewrite支持的flag标记
flag标记说明

last

相当于Apache的[L]标记,表示完成rewrite

break

本条规则匹配完成即终止,不再匹配后面的任何规则

redirect

返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url

permanent

返回301永久重定向,浏览器地址会显示跳转后的URL地址,爬虫更新url

1)last:url重写后,马上发起一个新请求,再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏不变

2)break:url重写后,直接使用当前资源,不再使用location余下的语句,完成本次请求,地址栏不变

两者差别:last和break再重定向后,地址栏都不会发生变化,这是他们的相同点,不同点在于last会写在server和if中,break是写在location中,last不会终止重写后的url匹配,break会终止重写后的url匹配

四,location分类
location = patt{} [精准匹配]
2 location patt {} [一般匹配]
3 location ~ patt {} [正则匹配]

在这里插入图片描述
location优先级

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

2、按优先级排列

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

实验 基于域名的跳转

systemctl stop firewalld 
setenforce 0
ping www.baidu.com 

systemctl status firwalld

vim /etc/resolv.conf 
 
 nameserver 114.114.114.114   #添加地址
 
 vim /etc/hosts 
 
192.168.189.14 www.123.com www.456.com 

mkdir -p /var/log/nginx/

vim /usr/local/nginx/conf/nginx.conf 

 server {
        listen       80;
        server_name  www.123.com; 
        #域名修改
       # charset utf-8;
        access_log  /var/log/nginx/www.123.com-access.log;
         #取消注释,开启并对日志保存路径进行修改

        location / {
        #在原有location位置添加内容
            if ($host = 'www.123.com') {
            rewrite^/(.*)$ http:// www.456.com/$1 permanent;
        }
            root   html;
            index  index.html index.htm;
        }
       
nginx -t   #检查一下

systemctl restart nginx

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
基于IP地址访问跳转

 vim /usr/local/nginx/conf/nginx.conf

    #gzip  on;

    server {
        listen       80;
        server_name  www.123.com;
        charset utf-8;
        access_log  /var/log/nginx/www.1.com-access.log;
        set $rewrite true;
        if ($remote_addr = "192.168.189.14") {
        set $rewrite false;
        }
        if ($rewrite = true){
        rewrite (.+) /weihu.html;
        }
        location = /weihu.html {
        root /var/www/html;
}
        
        location / {
            root   html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

[root@server ~]# mkdir -p /var/www/html
[root@server ~]# echo '<h1> this is weihu web! </h1>' > /var/www/html/weihu.html
[root@server ~]# 
[root@server ~]# 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@server ~]# systemctl restart nginx

在这里插入图片描述
基于IP地址访问192.168.189.13
在这里插入图片描述

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

 mkdir -p /usr/local/nginx/html/bbs/post
 echo "<h1> this is 1.html </h1>" >> /usr/local/nginx/html/bbs/post/1.html

 echo "192.168.189.14 bbs.123.com" >> /etc/hosts
 
 vim /etc/hosts

127.0.0.1   localhost server localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.152.130 bbs.123.com www.123.com www.456.com

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

     #gzip  on;

    server {
        listen       80;
        server_name  bbs.1.com;
        charset utf-8;
        access_log  /var/log/nginx/www.123.com-access.log;
        location /post {
        rewrite (.+) http://www.123.com/bbs$1 permanent;
}

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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值