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

57.Nginx重写,if,基于浏览器分离,防盗链

  • Nginx重写,if,基于浏览器分离,防盗链

  • Rewrite 功能概述

  • 基本概念
  • 作用:实现URL地址重定向

  • 依赖:需要PCRE(Perl兼容正则表达式)支持

  • 位置:只能放在 server{}location{}if{}

  • 作用范围:默认只对域名后的字符串起作用(不包括参数)

语法:

rewrite <regex> <replacement> [flag];

regex:正则匹配规则

replacement:跳转后的内容

flag:标记位

Flag标记说明

标记说明使用场景
last继续向下匹配新的location规则server和if中
break终止匹配,不再执行后续规则location中
redirect302临时重定向显示跳转后的URL
permanent301永久重定向显示跳转后的URL

Rewrite 应用场景

  1. URL规范化:调整用户浏览的URL,使其更规范
  2. 伪静态化:将动态URL伪装成静态地址
  3. 域名切换:旧域名跳转到新域名
  4. 条件调整:根据变量、目录、客户端信息进行URL调整

先自定义一个网页

[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# mkdir test
[root@nginx html]# cd test/
[root@nginx test]# echo test01 > test.html
[root@nginx test]# vim /usr/local/nginx/conf/nginx.conflocation /test {}
[root@nginx test]# nginx -s reload

查看网页

在这里插入图片描述

break 标记示例

[root@nginx test]# cd ..
[root@nginx html]# ls
50x.html  index.html  test
[root@nginx html]# mv test ceshi
[root@nginx html]# ls
50x.html  index.html  ceshi

再次查看会发现找不到网页

在这里插入图片描述

我们使用rewrite

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conflocation /test {rewrite ^/test(/test.html)(.*)$ /ceshi/$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让我们跳转到百度首页

 location /test {rewrite ^/test(/test.html)(.*)$ http://www.baidu.com break;}

在这里插入图片描述

last 标记示例

        location /test {rewrite ^/test(/test.html)(.*)$ /ceshi/$1 last;}location /ceshi {rewrite ^/ceshi(/)(.*)$ http://www.baidu.com break;}

last可以让我们连续跳转

redirect 标记示例

临时重定向

        location /test {        rewrite ^/test(/test.html)(.*)$ /ceshi/$1 redirect;}

在这里插入图片描述

可以显示新的url

permanent 标记示例

location /test {        rewrite ^/test(/test.html)(.*)$ /ceshi/$1 permanent;}

在这里插入图片描述

特性临时重定向(302)永久重定向(301)
HTTP状态码302 Found304 Moved Permanently
Nginx标记redirectpermanent
浏览器行为每次都会询问服务器缓存重定向结果

302:

浏览器不会缓存重定向结果

每次访问原URL都会向服务器发起请求

服务器返回302,浏览器再跳转到新URL

301:

浏览器会缓存重定向结果

第一次访问后,浏览器记住"原URL → 新URL"的映射

后续访问直接跳转,不再请求原URL服务器

if判断

可以使用在server段和location段

语法:

if (condition) {…}

常见的condition:

(1)变量名

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

(3)正则表达式的模式匹配操作
区分大小写的模式匹配检查

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

(4)测试指定路径为文件的可能性(-f !-f)

(5)测试指定路径为目录的可能性(-d !-d)

(6)测试文件的存在性(-e !-e)

(7)检查文件是否有执行权限(-x !-x)

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

修改nginx服务器主机名为

[root@nginx html]# hostnamectl set-hostname www.xie.com
[root@nginx html]# bash

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

[root@www html]# vim /etc/hosts
192.168.100.10 www.xie.com
192.168.100.10 www.xiexie.com[root@www html]# scp /etc/hosts root@192.168.100.20:/etc/hosts

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

[root@www html]# echo xie > index.html
[root@www html]# vim /usr/local/nginx/conf/nginx.confserver {listen       80;server_name  www.xie.com;location / {if ($host = 'www.xie.com'){rewrite ^/(.*)$ http://www.xiexie.com/$1 permanent; }root html;index index.html index.htm;}
[root@www html]# nginx -s reload

客户端访问www.xie.com会自动跳转到www.xiexie.com中

在这里插入图片描述

基于ip访问跳转

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

[root@www html]# vim /usr/local/nginx/conf/nginx.confserver {listen       80;server_name  www.xie.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;}
[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

分别测试

在这里插入图片描述

在这里插入图片描述

基于浏览器实现分离

在/usr/local/nginx/html目录中创建如下目录和文件

[root@www ~]# cd /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]# ls
50x.html  ceshi  chrome  firefox  index.html

修改配置文件

[root@www html]# vim /usr/local/nginx/conf/nginx.confserver {listen       80;server_name  localhost;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;}
[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
[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

向web服务器发送请求的时候,一般都会带上Referer,来告诉浏览器该网页是从哪个页面链接过来的。

http://www.dtcms.com/a/457984.html

相关文章:

  • 【多线程】死锁
  • 自学阿里云认证,能救一个是一个!
  • 买域名做网站跳转网新科技集团有限公司
  • 关于网站维护的书籍商务网站规划建设与管理答案
  • 【C语言基础详细版】03. 函数详解:从概念到高级应用
  • 涿州做网站公司阿里云网站怎么备案域名
  • 网站制作设计机构贵阳企业网站建设
  • wordpress快速仿站教程建立一个网站需要哪些
  • Linux 进程分身术:fork() 函数的深度解析
  • 小程序建站公司app开发需要多少费用
  • 贪心算法详解:从入门到精通(C++实现)
  • 颜群JVM【04】助记符
  • 网站优化 推广重庆教育建设集团有限公司网站
  • Manjaro 系统下 PCManFM 挂载 NTFS 分区报错:从踩坑到彻底解决
  • 单片机使用串口(usart) , 封装( print )函数 .
  • 外贸网站建设和优化做网站要不要学ps
  • 福建省建设厅网站 企业三网一体网站建设
  • 湖南做网站大连凯杰建设有限公司网站
  • 吴恩达机器学习课程(PyTorch适配)学习笔记:2.4 激活函数与多类别处理
  • 【PAG】PAG简介
  • hutool交并集
  • 赣州建设公司网站权威网站有哪些
  • Python制作12306查票工具:从零构建铁路购票信息查询系统
  • 《道德经》第十三章
  • 东莞做网站网络公司官网建设的重要性
  • Docker 容器操作
  • 小说网站建设源码潜江网络
  • 做网页游戏网站html网页设计大赛作品
  • 日语学习-日语知识点小记-进阶-JLPT-N1阶段应用练习(8):语法 +考え方21+2022年7月N1
  • 维基框架 (Wiki Framework) v1.1.2 | 企业级微服务开发框架