当前位置: 首页 > news >正文

nginx-rewrite、if、浏览器分离、防盗链

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  <regex>  <replacement>  [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属性获取小括号中匹配的内容。如^(hello | chenyu)$   //字符串为“hello chenyu”,可以捕获的结果为:

$1=hello$2=chenyu   这些被捕获的数据,在后面就可以当作变量一样进行使用了

rewrite功能

reweite在企业里应用非常的广泛

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

rewrite配置

//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]# ls
mao.jpg[root@nginx imgs]# vim /usr/local/nginx/conf/nginx.conflocation /imgs {}

访问

 

flag标记--break

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

再次访问,发现404找不到网页

 

重写rewrite 

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

 此时再次访问

 还可以使用break,让我们访问得站点跳转到百度得首页

 location /imgs {rewrite ^/imgs/(.*\.jpg)$ http://www.baidu.com break;}[root@nginx html]# nginx -s reload

//浏览器访问http://192.168.100.10/imgs/mao.jpg

 

flag标记-last

 

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

访问网页 

           

 可以实现跳转到百度

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

访问

flag标记--redirect

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

访问

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

 flag标记--permanent

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

访问

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

 if判断

可以使用在server段和location段

语法

if  (condition)  {...}

常见condition 

  1. 变量名
  2. 以变量名为操作数构成的比较表达式(可使用=,!=类似的比较符进行测试)
  3. 正则表达式的模式匹配操作

~  区分大小写的模式匹配检查

~*  不区分大小写的模式检查

  1. 测试指定路径为文件的可能性(-f  !-f)
  2. 测试指定路径为目录的可能性(-d  !-d)
  3. 测试文件的存在性(-e  !-e)
  4. 检查文件是否有执行权限(-x  !-x)

配置基于域名的跳转 

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

修改本地机的hosts文件

将这两条加入进去

192.168.100.10 www.hb.com
192.168.100.10 www.hubei.com

//修改nginx服务器主机名为www.cy.com
[root@nginx html]# hostnamectl set-hostname www.hb.com
[root@nginx html]# bash//将两个域名写入到/etc/hosts中,并传给客户端
[root@www html]# vim /etc/hosts
192.168.100.10 www.hb.com
192.168.100.10 www.hubei.com
[root@www html]# scp /etc/hosts root@192.168.100.20:/etc/hosts//修改配置文件,写入rewrite和if结合使用[root@www html]# vim /usr/local/nginx/conf/nginx.conf
server {listen       80;server_name  www.hb.com;   //设置域名location / {if ($host = 'www.hb.com') {    //变量host为rewrite的全局变量rewrite ^/(.*)$ http://www.hubei.com/$1 permanent; }                 //$1是匹配http://www.hb.com/后面的字段root html;index index.html index.htm;}
[root@www html]# echo "test" > /usr/local/nginx/html/index.html 
[root@www html]# nginx -s reload

访问

客户端使用浏览器访问--http://www.hb.com---我们会发现自动跳转到新的域名www.hubei.com中

基于客户端ip访问跳转

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

[root@www html]# vim /usr/local/nginx/conf/nginx.confserver {listen       80;server_name  www.cy.com;set $rewrite true;if ($remote_addr = "192.168.100.1") {        ///写入本地主机vmnet的ipv主机的ipset $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 html]# mkdir /var/www/html -p
[root@www html]# echo "weihu" > /var/www/html/weihu.html
[root@www html]# nginx -s reload
//将nginx服务器的/etc/hosts文件发送给客户端2(192.168.100.30)
[root@www html]# scp /etc/hosts root@192.168.100.30:/etc/hosts

 访问

192.168.100.1的主机来访问

正常访问

 

 使用192.168.100.30的主机来访问

方位的就是weihu界面

基于浏览器实现分离

//在/usr/local/nginx/html目录中创建如下目录和文件
[root@www html]# mkdir firefox chrome
[root@www html]# echo "firefox test" > firefox/index.html
[root@www html]# echo "chrome test" > chrome/index.html修改配置文件[root@www html]# vim /usr/local/nginx/conf/nginx.confserver {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {if ($http_user_agent ~ Firefox) {rewrite ^(.*)$ /firefox/$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;}//使用nginx -t 测试配置文件是否正确
[root@www 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//重载nginx
[root@www html]# nginx -s reload

使用谷歌浏览器访问

使用火狐来访问 

防盗链案例

了解防盗链的原理之前,我们得先学习一个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: 可以支持正则表达式和*的字符串。如果是正则表达式,需要以~开头表示

案例 

//在/usr/local/nginx/html下,上传照片
[root@www html]# ls
mao.jpg
//配置nginx配置文件location ~* \.(jpg|png) 

使用浏览器访问

此时可以正常访问

使用命令查看referer信息,此时还未配置防盗链 

[root@www html]# curl --referer http://baidu.com -I http://192.168.100.10/mao.jpg
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Tue, 27 Aug 2024 11:20:21 GMT
Content-Type: image/jpeg
Content-Length: 366251
Last-Modified: Tue, 27 Aug 2024 11:14:22 GMT
Connection: keep-alive
ETag: "66cdb50e-596ab"
Accept-Ranges: bytes

配置防盗链

 location ~* \.(jpg|png) {valid_referers none blocked www.hb.com;   //有效的来源if ($invalid_referer) {               //无效的来源return 403;break;}}

再次使用浏览器访问,我们发现是可以访问的

 但是使用命令查看referer信息的时候,会发现返回了403了

[root@www html]# curl --referer http://www.hb.com -I http://192.168.100.10/mao.jpg
HTTP/1.1 403 Forbidden
Server: nginx/1.24.0
Date: Tue, 27 Aug 2024 11:30:40 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • PHP 全攻略:从环境搭建到实战项目的深度探索
  • 国产游戏技术能否引领全球?
  • 《算法竞赛进阶指南》0x26广搜变形
  • ROS实现简单避障
  • 如何利用「搭贝」进销存系统锁住库存
  • Code Llama: Open Foundation Models for Code论文阅读
  • STM32外部中断事件控制器-EXTI
  • 【AI学习】在魔塔社区玩Ollama:部署GLM4和CodeGeeX4
  • 切换JDK版本
  • CSS3页面布局-三栏-固定宽度布局
  • TCP协议(1)
  • Ubuntu上搭建Nginx环境
  • Golang | Leetcode Golang题解之第368题最大整除子集
  • 面试被面试官问:3D目标检测预处理优化策略有哪些?
  • 计算机网络模型
  • python3.6+scrapy+mysql 爬虫实战
  • 分享一款快速APP功能测试工具
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • 2017前端实习生面试总结
  • 77. Combinations
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • extract-text-webpack-plugin用法
  • java概述
  • Mysql5.6主从复制
  • php面试题 汇集2
  • ReactNative开发常用的三方模块
  • webpack入门学习手记(二)
  • 构建工具 - 收藏集 - 掘金
  • 聊聊flink的BlobWriter
  • 前端路由实现-history
  • 前嗅ForeSpider采集配置界面介绍
  • 日剧·日综资源集合(建议收藏)
  • 新版博客前端前瞻
  • 用element的upload组件实现多图片上传和压缩
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • ### RabbitMQ五种工作模式:
  • #include
  • #大学#套接字
  • (9)STL算法之逆转旋转
  • (Java入门)抽象类,接口,内部类
  • (八)Flask之app.route装饰器函数的参数
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)jdk与jre的区别
  • *++p:p先自+,然后*p,最终为3 ++*p:先*p,即arr[0]=1,然后再++,最终为2 *p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET Core 通过 Ef Core 操作 Mysql
  • .net core使用ef 6
  • .net 后台导出excel ,word
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景