Nginx - Rewirte

一、Rewrite

和apache等web服务软件一样,rewrite的主要功能是实现URL地址的重定向。Nginx的rewrite功能需要PCRE软件的支持,即通过perl兼容正则表达式语句进行规则匹配的。默认参数编译nginx就会支持rewrite的模块,但是也必须要PCRE的支持

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

Rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递参数外的字符串起作用。例如http://www.cy.com/abc/aa/index.php?a=1&b=2 只对/abc/aa/index.php重写

URL:就是具体路径/位置

URI:指的是一个拥有相同类型/特性的对象集合

在这里插入图片描述
Nginx:通过ngx_http_rewrite_module模块支持URL重写,支持if条件判断,但不支持else。

跳转:从一个location跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误。

PCRE支持:perl兼容正则表达式的语法规则匹配

重写模块set指令:创建新的变量并设其值

语法格式

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

flag标记说明

last:本条规则匹配完成后,继续向下匹配新的location URL规则,一般用server和if中。

break:本条规则匹配完成即终止,不在匹配后面的任何规则,一般使用在location中。

redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址

permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。

匹配正则的标识符和意义

^   必须以^后的实体开头

$	 必须以$前的实体结尾

.	 匹配任意单个字符

[]	 匹配指定字符集的任意字符

[^]	匹配任何不包括在指定字符集内的任意字符串

|	匹配|之前或之后的实体

()	分组,组成一组用于匹配的实体,通常会有|来协助

\	转义

*	匹配前面的字符出现零次或者多次,如“ab*”能匹配a、ab、abb

+	匹配前面的字符出现一次或者多次,如“ab+”能匹配ab、abb,但是不能匹配a

?	匹配前面的字符出现零次或者一次,如“ab(cd)?”能匹配ab、abcd

(pattern) 匹配括号内pattern并可以在后面获取对应的匹配,常用$0- 9 属性获取小括号中匹配的内容。 如 ( h e l l o ∣ c h e n y u ) 9属性获取小括号中匹配的内容。如^(hello | chenyu) 9属性获取小括号中匹配的内容。(hellochenyu) //字符串为“hello chenyu”,可以捕获的结果为:
$1=hello$2=chenyu 这些被捕获的数据,在后面就可以当作变量一样进行使用了

二、nginx的rewrite功能在企业里应用非常广泛

1、可以调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求

2、为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态的URL地址伪装成静态地址提供服务

3、网址更新域名后,让旧的访问跳转到新的域名上,例如访问京东的360buy.com会跳转到jd.com

4、根据特殊变量、目录、客户端的信息进行URL调整等。

三、rewrite配置

nginx访问自定义网页

[root@nginx ~]# nginx
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# ls
50x.html  index.html
[root@nginx html]# mkdir imgs
[root@nginx html]# cd imgs/
[root@nginx imgs]# rz -E
rz waiting to receive.
[root@nginx imgs]# ls
1.jpg

[root@nginx imgs]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /imgs {
        }

[root@nginx ~]# nginx -t
[root@nginx imgs]# nginx -s reload

浏览器访问
在这里插入图片描述

1、flag标记–break

[root@nginx html]# pwd
/usr/local/nginx/html

[root@nginx html]# ls
50x.html  imgs  index.html
[root@nginx html]# mv imgs/ images
[root@nginx html]# ls
50x.html  images  index.html

再次访问会发现404找不到网页
在这里插入图片描述
需要写rewrite

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 break;
        }

[root@nginx html]# 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@nginx html]# nginx -s reload

刷新网页
在这里插入图片描述
还可以使用break,让我们访问得站点跳转到百度得首页

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ http://www.baidu.com break;
        }

[root@nginx html]# 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@nginx html]# nginx -s reload

浏览器访问http://192.168.100.10/imgs/1.jpg
在这里插入图片描述

2、flag标记-last

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

        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 last;
        }
[root@nginx html]# 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@nginx html]# nginx -s reload

在这里插入图片描述
也可以实现跳转到百度

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

        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 last;
        }

         location /images {
                rewrite ^/imgs/(.*\.jpg)$ http://www.baidu.com last;
        }
        
[root@nginx html]# 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@nginx html]# nginx -s reload

在这里插入图片描述

3、flag标记–redirect

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 redirect;
        }
[root@nginx html]# 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@nginx html]# nginx -s reload

访问http://192.168.100.111/imgs/1.jpg
在这里插入图片描述

4、flag标记–permanent

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 permanent;
        }
[root@nginx html]# nginx -s reload

在这里插入图片描述

四、if判断

可以使用在server段和location段
语法:
if (condition) {…}

常见的condition

(1)变量名
(2)以变量名为操作数构成的比较表达式(可使用=,!=类似的比较符进行测试)
(3)正则表达式的模式匹配操作
~ 区分大小写的模式匹配检查
~* 不区分大小写的模式检查
(4)测试指定路径为文件的可能性(-f !-f)
(5)测试指定路径为目录的可能性(-d !-d)
(6)测试文件的存在性(-e !-e)
(7)检查文件是否有执行权限(-x !-x)

1、配置基于域名跳转

假如现在公司旧的域名www.sq.com有业务需求,需要使用新的域名www.shiqian.com代替,但是旧域名不能废除,需要跳转到新的域名上,而且后面的参数保持不变

修改nginx服务器主机名为www.sq.com

[root@nginx ~]# hostnamectl hostname www.sq.com
[root@nginx ~]# bash

将两个域名写入到/etc/hosts中,并传给客户端

[root@www ~]# vim /etc/hosts 
192.168.100.111 www.sq.com
192.168.100.111 www.shiqian.com
~      
[root@www ~]# scp /etc/hosts root@192.168.100.112:/etc/hosts
                           

修改配置文件,写入rewrite和if结合使用

[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.sq.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                if ($host = 'www.sq.com') {
                        rewrite ^/(.*)$ http://www.shiqian.com/$1 permanent;
                }
                root html;
                index index.html index.htm;
        }
[root@www ~]# nginx -s reload

在这里插入图片描述

2、基于客户端ip访问跳转

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

//修改nginx配置文件
[root@www html]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.cy.com;
        set $rewrite true;
        if ($remote_addr = "192.168.100.20") {
                set $rewrite false;
        }

        if ($rewrite = true) {
                rewrite (.+) /weihu.html;
        }

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

//新建/var/www/html目录,并往该目录下写入文件weihu.html,内容为weihu
[root@www ~]# mkdir /var/www/html -p
[root@www ~]# echo "weihu" > /var/www/html/weihu.html
[root@www ~]# nginx -s reload


//rs1访问
[root@rs1 ~]# curl http://www.sq.com
shiqian test

//rs2访问
[root@rs2 ~]# curl http://www.sq.com
[root@rs2 ~]# 
//因为时命令行的原因所以没有显示维护

五、基于浏览器实现分离

///usr/local/nginx/html目录中创建如下目录和文件
[root@www html]# mkdir edg chrome
[root@www html]# echo "edg test" > edg/index.html
[root@www html]# echo "chrome test" > chrome/index.html

//修改nginx配置文件
[root@www html]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;

        #access_log  logs/host.access.log  main;


	location / {
		if ($http_user_agent ~ Edg) {
			rewrite ^(.*)$ /edg/$1 break;
		}

		if ($http_user_agent ~ Chrome) {
                        rewrite ^(.*)$ /chrome/$1 break;
        }
		root html;
		index index.html index.htm; 
	}
	location /firefox {
		root html;
		index index.html;
	}
	location /chrome {
                root html;
                index index.html;
    }

[root@www html]# nginx -s reload

使用谷歌浏览器访问
在这里插入图片描述
使用edge浏览器访问
在这里插入图片描述

六、防盗链案例

了解防盗链的原理之前,我们得先学习一个HTTP的头信息Referer,当浏览器向web服务器发送请求的时候,一般都会带上Referer,来告诉浏览器该网页是从哪个页面链接过来的。

后台服务器可以根据获取到的这个Referer信息来判断是否为自己信任的网站地址,如果是则放行继续访问,如果不是则可以返回403(服务端拒绝访问)的状态信息。
在这里插入图片描述
语法:
valid_referers none blocked server_names string

none: 如果Header中的Referer为空,允许访问
blocked:在Header中的Referer不为空,但是该值被防火墙或代理进行伪装过,如不带"http://" 、"https://"等协议头的资源允许访问。
server_names:指定具体的域名或者IP
string: 可以支持正则表达式和*的字符串。如果是正则表达式,需要以~开头表示

案例:

[root@www html]# vim /usr/local/nginx/conf/nginx.conf

//开始配置防盗链
        location ~* \.(jpg|png) {
                valid_referers none blocked www.cy.com;   //有效的来源
                if ($invalid_referer) {               //无效的来源
                        return 403;
                        break;
                }
        }
[root@www html]# nginx -s reload


//使用命令查看referer信息的时候,会发现返回了403
[root@www html]# curl --referer http://baidu.com -I http://192.168.100.111/1.jpg
HTTP/1.1 403 Forbidden
Server: nginx/1.24.0
Date: Tue, 03 Sep 2024 10:08:23 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

那么此时防盗链已经做好了
  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值