实验环境:
接nginx(2),server2、3的http服务打开
一、重定向
这里实验结果有问题。
(1)域名保护
防止域名恶意解析到服务器IP,为了安全不想让客户端直接以ip形式访问
以下是未加入域名保护时,客户curl服务器的结果

配置文件? 加入return 500;
?nginx -s reload
此时curl的结果为

?(2)重定向:80重定向443
? 配置文件加入 ??? rewrite ^/(.*)$ https://www.westos.org permanent;

?客户端curl www.westos.org
这里客户端一定要有解析!
?
?
?若配置文件改为rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
表示在网页中用户鼠标点击后的页面进行重定向

(3)网站的整体迁移
?比如某个网站A需要维护但是不能停止服务,那么用临时网站C代替,用户访问A时自动跳到C
配置文件底部新加一个虚拟主机,并新建相应文件
?? server { ????????????? listen 80; ???????????? server_name bbs.westos.org;
???????????? location / { ????????????????? root /bbs; ????????????????? index index.html; ??????????????? } ?????? }

?此时客户端 curl bbs.westos.org
可以看到新页面,但要注意我们更想要另一种效果

配置文件新增rewrite ^/bbs$ http://bbs.westos.org permanent;

客户端测试:?? 效果如下
curl www.westos.org/bbs curl www.westos.org curl www.westos.org/bbs -I?

?但是这里出现一个问题,curl www.westos.org/bbs/index.html -I
出现404的情况,也就是说不能继续往下重定向

解决办法:配置文件取消注释
? rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;

?再次测试 ? ? ? curl www.westos.org/bbs/index.html -I,成功

?(4)基于if语句的网站迁移
通过一条if语句,用一个服务器实现重定向
配置文件需要把上边定义的服务器注销

?原有server上,注销旧策略,加入if判断
???????????? if ($host = "bbs.westos.org"){ ?????????????????? rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent; ?????????????? }
???????????? location / { ???????????????? # proxy_pass http://westos; ?????????????????? root html; ?????????????????? index index.html;

将bbs目录拷贝到
nginx -s reload
?
?主机测试

?二、防盗链
(1)简单的盗链方式
server2:新建haha.html,内容如下
<html> <body>
<img src='http://172.25.73.1/download/haha.jpg'/>
</body> </html>
浏览器 http://172.25.73.2/haha.html出现下图?
要注意的是,文件中仅添加了一个地址,也就是说这个页面并不耗费server2的流量,而是耗费的 http://172.25.73.1/download/haha.jpg的流量,所以这种行为非常不道德,要防止盗链。

(2)防盗链
?1、遇到盗链情况,让他返回值是403
配置文件修改:
??????? location ~ \.(jpg|png)$ { ??????????????? valid_referers none blocked www.westos.org; ??????????????? if ($invalid_referer) { ??????????????????????? return 403;? 返回403 ??????????????????????? #rewrite ^/ http://www2.westos.org/daolian.jpg;?? 连接到另一个图 ?????? } ????? }

?这里和重定向有关系,没有出来效果
|