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图片防盗链配置
如果我们自己网站内的图片资源被其它网站所盗用,这会增加自己网站的带宽资源,增加很多额外的消耗,而且会对我们系统的稳定性有影响,为了防止自己网站上的图片资源被其它网站所盗用,我们需要给自己的服务器配置防 ...
随机推荐
- 轻量级的REST中间件
轻量级的REST中间件 纯净的REST中间件,绝对的轻量级,不需要安装任何三方控件 基于HTTPS.SYS和WEBSOCKET通信,支持海量并发 支持跨越DELPHI6~DELPHI10.2.2的开发 ...
- 二十四种设计模式:访问者模式(Visitor Pattern)
访问者模式(Visitor Pattern) 介绍表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 示例有一个Message实体类,某些对象对 ...
- Javascript中的对象(二)
Javascript是一种基于原型的对象语言,而不是我们比较熟悉的,像C#语言基于类的面向对象的语言.在前一篇文章中,我们已经介绍了Javascript中对象定义的创建.接下来我们来介绍一下Javas ...
- ylbtech-LanguageSamples-Delegates(委托)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Delegates(委托) 1.A,示例(Sample) 返回顶部 “委托”示例 本示例 ...
- Less的安装与配置
Less的安装与配置 Less与Sass 先说一段题外话,很多初学者在选择CSS 预处理语言不免会感到犹豫,作为CSS 的两大预处理语言Less与Sass,各自都有着很广泛的使用群体,我究竟该选哪个好 ...
- 《深入理解Java虚拟机》笔记7
组内有人问我,她写的程序总是在短时间内就jvm异常. 另外,debug时候又可以正常通过,写的逻辑并不复杂, 只是用poi检索Excel.第一反应还是程序可能写的有问题, 也许写了一个jvm未预测的错 ...
- linux 的计划任务 cron
https://serverfault.com/questions/587696/how-to-restart-php-fpm-from-cron 我也遇到了这个问题,想用cron 来启动php-fp ...
- html中css三种常见的样式选择器
1:标签选择器 标签选择器,是所有带有某种标签的都生效.这里以p为例,也就是所有的带有p标记的都会这样的样式 复制代码 代码如下: <html><head><stylet ...
- PHP的抽象类、接口的区别和选择[转载]
区别: 1.对接口的使用是通过关键字implements.对抽象类的使用是通过关键字extends.当然接口也可以通过关键字extends继承. 2.接口中不可以声明成员变量(包括类静态变量),但是可 ...
- iOS多线程之NSOperation和NSOperationQueue的使用
一:NSOperation 两个子类+重写main方法 NSInvocationOperation NSBlockOperation 有个类方法 BlockOprationWith: 还有就是自己个子 ...