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中 |
redirect | 302临时重定向 | 显示跳转后的URL |
permanent | 301永久重定向 | 显示跳转后的URL |
Rewrite 应用场景
- URL规范化:调整用户浏览的URL,使其更规范
- 伪静态化:将动态URL伪装成静态地址
- 域名切换:旧域名跳转到新域名
- 条件调整:根据变量、目录、客户端信息进行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 Found | 304 Moved Permanently |
Nginx标记 | redirect | permanent |
浏览器行为 | 每次都会询问服务器 | 缓存重定向结果 |
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,来告诉浏览器该网页是从哪个页面链接过来的。