[root@web01 www]# cat /app/server/nginx/conf/vhosts/default.conf
server {
listen default_server;
server_name 192.168.1.24 web01.espressos.cn;
root /app/www;
index index.php index.html index.htm;
location ~* \.(gif|jpg|png|swf|flv)$ { #对gif,jpg,png,swf,flv后缀的文件实行防盗链
valid_referers none blocked 192.168.1.24 web01.espressos.cn; #对192.168.1.24 web01.espressos.cn这两个来路进行判断(主要是根椐http协议里的referer)
if ($invalid_referer) { #if{}里面内容的意思是,如果来路不是指定来路就跳转到错误页面,当然直接返回403也是可以的。
rewrite ^/ http://192.168.1.24/403.html;
#return ;
}
}
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log /app/log/nginx/access/default.log;
}
第8行;valid_referers none blocked *.espressos.cn *qq.com *baidu.com ;
就是白名单,允许文件链出的域名白名单,自行修改成您的域名! *.espressos.cn这个指的是子域名,域名与域名之间使用空格隔开!baidu.com是搜索引擎,做qq.com的白名单是因为可能有用户用邮箱订阅你的站点,如果不设置为白名单,用户在邮箱就无法看见你的图了,同理,如果还有其他订阅方式、搜索引擎都最好添加一下。

验证代码:

[root@web01 www]# cat q.html
<html>
<body><img alt="http://192.168.1.24/cat.png" src="http://192.168.1.24/cat.png" height="auto" width="auto"></body>
</html>
[root@web01 www]#

另一台WEB服务器盗用WEB01的图片(配置如下):

[root@lmr default]# cat /app/server/nginx/conf/vhosts/default.conf
server {
listen ;
server_name localhost;
index index.html index.htm index.php;
root /app/www/default;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
include /app/server/nginx/conf/rewrite/default.conf;
access_log /app/log/nginx/access/default.log;
}

验证:

[root@lmr default]# pwd
/app/www/default
[root@lmr default]# cat q.html
<html>
<body><img alt="http://192.168.1.24/cat.png" src="http://192.168.1.24/cat.png" height="auto" width="auto"></body>
</html>
[root@lmr default]#

防盗成功!!!

把web01上的防盗代码注释掉:

[root@web01 www]# cat /app/server/nginx/conf/vhosts/default.conf
server {
listen default_server;
server_name 192.168.1.24 web01.espressos.cn;
root /app/www;
index index.php index.html index.htm;
#location ~* \.(gif|jpg|png|swf|flv)$ {
#valid_referers none blocked 192.168.1.24 web01.espressos.cn;
#if ($invalid_referer) {
# rewrite ^/ http://192.168.1.24/403.html;
# return ;
# }
#}
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log /app/log/nginx/access/default.log;
}
[root@web01 www]# /app/server/nginx/sbin/nginx -s reload

重新测式盗连图片的服务器,盗图是否成功:

图片盗连成功!!

============新测试======================

 sh-4.1# cat /etc/nginx/vhosts/test.espressos.cn.conf
server {
listen ;
server_name test.espressos.cn;
index index.html 123.57.37.211 index.htm index.php;
root /usr/html/test.espressos.cn;
location ~ .*\.(php|php5)
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
valid_referers none blocked 123.57.37.211 test.espressos.cn;
if ($invalid_referer) {
#rewrite ^/ http://123.57.36.227/404.html;
return ;
}
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
include /etc/nginx/rewrite/test.espressos.cn.conf;
access_log /var/log/nginx/test.espressos.log;
}

注意上面的第19和20行:(图片服务器的IP:123.57.37.211)

[root@lmr default]# cat q.html
<html>
<body><img alt="cat.png" src="http://123.57.37.211/cat.png" height="auto" width="auto"></body>
</html>
[root@lmr default]# 本机是另外一台WEB服务器IP192.168.1.83

访问测试面q.html(上面的页网代码),测试结果如下:

图片服务器的404.html:

 sh-4.1# cat /usr/html/test.espressos.cn/.html
error
sh-4.1# cat /etc/nginx/vhosts/test.espressos.cn.conf
server {
listen ;
server_name test.espressos.cn;
index index.html 123.57.36.227 index.htm index.php;
root /usr/html/test.espressos.cn;
location ~ .*\.(php|php5)
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
valid_referers none blocked 123.57.36.227 test.espressos.cn;
if ($invalid_referer) {
rewrite ^/ http://123.57.36.227/404.html;
#return ;
}
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
include /etc/nginx/rewrite/test.espressos.cn.conf;
access_log /var/log/nginx/test.espressos.log;
}

注意21和22行(上为图片服务器的web配置文件)

最新文章

  1. JVM初学笔记
  2. java 中文转化为拼音
  3. CSS3新增属性
  4. C#DbHelperOleDb,Access数据库帮助类
  5. 利用TabHost制作QQ客户端标签栏效果(低版本QQ)
  6. Windows中搭建已存在的Octopress环境
  7. 现代程序设计 homework-07
  8. poj 2289 网络流 and 二分查找
  9. 2014年50个程序员最适用的免费JQuery插件
  10. innerXml,outerXml,innerText的不同
  11. 文件系统与linux相关知识点
  12. TagHelper+Layui封装组件之Radio单选框
  13. java获取机器IP地址常用方法
  14. sql server 查询所有表结构
  15. Java开发笔记(七十)Java8新增的几种泛型接口
  16. iOS常用算法
  17. sessionStorage记录返回前端的数据,用于解决登录拦截器刷新页面的问题
  18. springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)
  19. Python全栈-day11-函数3
  20. windows8安装msi或exe软件提示2503错误的解决办法

热门文章

  1. jQuery.extend重载合并
  2. checkbox复选框居中
  3. SQL Server 更新 触发器
  4. Tomcat 访问 Manager App,Host Manager
  5. fc游戏反编译流程
  6. 绿化和卸载 DOS 批处理
  7. 图解aclocal、autoconf、automake、autoheader、configure
  8. 15个CSS3和jQuery的超棒页面过渡效果教程
  9. Discuz常见小问题2-如何清空,删除,清除全部DIY的数据
  10. Android性能优化的方方面面