Linux—Nginx优化—防盗链
一.防盗链
1.在企业网站服务中一般要配置防盗链功能,以避免网站被非法盗用,造成经济损失,也避免不必要的带宽浪费
2.Nginx的防盗链功能非常强大,在默认情况之下,只需要进行简单的配置,即可实现防盗链功能
3.防盗链具体操作
准备两台主机模拟盗链与防盗链
二.具体操作
源主机上:
地址映射:
[root@localhost ~]# vim /etc/hosts
192.168.88.158 www.kgc.com
192.168.88.137 www.accp.com
添加图片:
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
123.jpg 50x.html index.html
[root@localhost html]# vim index.html
<img src="123.jpg"/>
查看页面:
设置盗链主机进行盗链:
地址映射:
[root@localhost ~]# vim /etc/hosts
192.168.88.137 www.accp.com
192.168.88.158 www.kgc.com
添加图片:
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# vim index.html
<img src="http://www.kgc.com/123.jpg"/>
查看:
再在源主机上设置防盗链:
配置文件添加防盗链模块:
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location ~* \.(jpg|gif|swf)$ {
valid_referers none blocked *.kgc.com kgc.com;
if ($invalid_referer) {
rewrite ^/ http://www.kgc.com/error.png;
}
}
添加错误图片并重启服务:
[root@localhost html]# ls
123.jpg 50x.html index.html
[root@localhost html]# rz
[root@localhost html]# ls
123.jpg 50x.html error.png index.html
[root@localhost html]# service nginx restart