Nginx图片的防盗链配置
[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配置文件)

Nginx图片的防盗链配置的更多相关文章
- nginx实现防盗链配置方法介绍
有些朋友觉得防盗链就是防止图片,其实有很多东西要进行防盗链了,下面我来介绍在nginx中实现防盗链配置方法有对图片防盗链与下载资源等. 防盗链配置 假设网站域名是 www.php100.com. 编辑 ...
- Nginx防盗链配置案例配置,Nginx的知识分享
防盗链的含义:网站内容不在自己服务器上,而通过技术手段,绕过别人放广告有利益的最终页,直接在自己的有广告有利益的页面上向最终用户提供此内容. 常常是一些名不见经传的小网站来盗取一些有实力的大网站的地址 ...
- nginx rewrite重写与防盗链配置
nginx rewrite重写规则与防盗链配置方法 时间:2016-02-04 15:16:58来源:网络 导读:nginx rewrite重写规则与防盗链配置方法,rewrite规则格式中flag标 ...
- nginx secure_link下载防盗链
下载服务器上有众多的软件资源, 可是很多来源不是本站,是迅雷.flashget, 源源不断的带宽,防盗链绝对是当务之急. 使用来源判断根本不靠谱,只能防止一些小白站点的盗链,迅雷之类的下载工具完全无效 ...
- 16.Nginx优化与防盗链
Nginx优化与防盗链 目录 Nginx优化与防盗链 隐藏版本号 修改用户与组 缓存时间 日志切割 小知识 连接超时 更改进程数 配置网页压缩 配置防盗链 配置防盗链 隐藏版本号 可以使用 Fiddl ...
- Apache 防盗链配置
盗链 网站内有许多的图片地址,或一些我们可以用到的资源,在这种情况下,我可以通过赋值其他图片链接地址,到我自己的平台上,这样相当于盗取了一张图片的链接,那么盗链会有什么危害呢? 当我们盗取一张图片链接 ...
- nginx如何设置防盗链
关于nginx防盗链的方法网上有很多教程,都可以用,但是我发现很多教程并不完整,所做的防盗链并不是真正的彻底的防盗链! 一般,我们做好防盗链之后其他网站盗链的本站图片就会全部失效无法显示,但是您如果通 ...
- 【转】nginx如何设置防盗链
转自博客园作者:howhy,文章地址:nginx如何设置防盗链.大佬写的甚好,在此备份一下 关于nginx防盗链的方法网上有很多教程,都可以用,但是我发现很多教程并不完整,所做的防盗链并不是真正的彻底 ...
- Nginx图片防盗链配置
如果我们自己网站内的图片资源被其它网站所盗用,这会增加自己网站的带宽资源,增加很多额外的消耗,而且会对我们系统的稳定性有影响,为了防止自己网站上的图片资源被其它网站所盗用,我们需要给自己的服务器配置防 ...
随机推荐
- QJSON封装好的序列和还原方法
QJSON封装好的序列和还原方法 {*******************************************************}{ }{ QJSON与数据集互转 }{ }{ 版权所 ...
- PostgreSQL配置文件--AUTOVACUUM参数
8 AUTOVACUUM参数 AUTOVACUUM PARAMETERS 8.1 autovacuum 字符型 默认: autovacuum = on Enable autovacuum subpro ...
- 前端对比插件JS
https://github.com/kpdecker/jsdiff demo http://kpdecker.github.io/jsdiff/ 缺点:文件大于10M的就比较不了了 用于比对两段HT ...
- ylbtech-LanguageSamples-Delegates(委托)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Delegates(委托) 1.A,示例(Sample) 返回顶部 “委托”示例 本示例 ...
- centos查看哪些包提供指定头文件
[问题]:项目迁移时,原来在suse上正常的代码在centos上报错: g++ -g -Wall -fPIC -I../include -I./ -I../src -I/share/comm_ext ...
- centos/7/isos/x86_64 下载
为了节约有限的可用带宽. 不从mirror.centos.org下载iso映像 以下镜子应该可用的ISO映像: http://mirrors.aliyun.com/centos/7/isos/x86_ ...
- [Angular] AfterContentChecked && AfterViewChecked
AfterContentChecked & AfterViewChecked are called after 'OnChanges' lifecycle. And each time 'ng ...
- 如何在 MyEclipse 下用 Gradle 构建 Web Application
一.安装 Gradle 插件,参考我的另一篇文章:MyEclipse 集成 Gradle开发环境 二.新建一个 Web Project, 在项目根目录下新建 build.gradle 文件, 并向其中 ...
- AFNetworking 文件上传Data,File图片,文件等上传
一:AFNetworking的文件上传: 主要几个以下类似 - (BOOL)appendPartWithFileURL:(NSURL *)fileURL name:(NSString *)name e ...
- Activity生命周期以及启动模式对生命周期的影响
前天用户体验反馈的一个需求,要求每次进入应用都定位到首页;这个操作很明显不适合放在首页Activity(启动模式为SingleTask)的onResume中,如果对Activity的启动模式和生命周期 ...