配置nginx实现防盗链
在企业网站服务中,通常都要配置防盗链功能,以免网站内容被非法盗用,形成经济损失
nginx防盗链功能也很是强大。默认状况下,只须要进行简单的配置,便可实现放到链处理
实验环境
一台nginx服务器,一台测试客户机,一台盗链机
实验过程
1.nginx服务器上部署nginx服务
详细部署过程请查看:http://www.javashuo.com/article/p-qnoedoqg-eb.html
nginx部署成功!
2.配置盗链机
建立盗链机的网页
除非用本地的域名去访问
若是匹配的是无效的,不然就跳转一个error.png页面
配置说明php
[root@localhost ~]# cd /usr/local/nginx/ [root@localhost nginx]# ls client_body_temp fastcgi_temp logs sbin uwsgi_temp conf html proxy_temp scgi_temp [root@localhost nginx]# cd html/ [root@localhost html]# ls 50x.html index.html qq.jpg [root@localhost html]# mount.cifs //192.168.254.10/linuxs /abc Password for root@//192.168.254.10/linuxs: [root@localhost html]# cd /abc [root@localhost abc]# ls error.png [root@localhost abc]# cp error.png /usr/local/nginx/html/ [root@localhost abc]# cd - /usr/local/nginx/html [root@localhost html]# ls 50x.html error.png index.html qq.jpg [root@localhost html]# grep "qq.jpg" index.html <img src="qq.jpg"> [root@localhost html]# service nginx start [root@localhost html]# netstat -natp | grep 80 tcp 0 0 192.168.247.193:80 0.0.0.0:* LISTEN 3038/nginx: master
使用域名,须要使用dns解析服务
搭建dns详见我前面的博客
而后指定盗链主机和客户机的dns服务器
开始设置盗机
在win10内打开控制面板,控制面板内打开程序和功能,里面有开启或者关闭功能,点击
而后点击internet informatoion servers
关闭盗机防火墙
而后客户端去访问盗机网站
盗链成功,接下来作防盗链操做:html
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf location ~*\.(jpg|gif|swf)$ { valid_referers none blocked *.shl.com shl.com; if ( $invalid_referer ) { rewrite ^/ http://www.shl.com/error.png; } }
重启nginx服务linux
[root@localhost named]# service nginx stop [root@localhost named]# service nginx start
防盗链配置成功
FPM优化参数调整示例nginx
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pid pm = dynamic pm.max_children=20 pm.static_servers = 5 pm.min_spare_servers = 2 pm.max_spare_servers = 8