两个网站 A 和 B, B网站引用了A网站上的图片,这种行为就叫做盗链。 防盗链,就是要防止B引用A的图片。
1.如何区分哪些是不正常的用户? HTTP Referer是Header的一部分,当浏览器向Web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理,例如防止未经允许 的网站盗链图片、文件等。因此HTTP Referer头信息是可以通过程序来伪装生成的,所以通过Referer信息防盗链并非100%可靠,但是,它能够限制大部分的盗链情况。
2. 防盗链配置配置注意事项:
1.如果你是编译安装的nginx,你需要去配置文件中将日志格式的注释去掉
[root@localhost ~]# vim /etc/nginx/nginx.conf #配置文件
# 日志格式添加"$http_referer"
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#将上面的注释去掉,编译安装默认是注释掉的
2.如果你是yum安装的话,默认这些日志格式是没有被注释的,可以不用做任何操作
3.准备两台装有nginx的虚拟机
图片网站服务器:上传图片 192.168.13.133
盗链机器:用于盗链测试192.168.13.129
![6317a36e4573d52bf9309bd7bafa079e.png](https://i-blog.csdnimg.cn/blog_migrate/1b01758b92e38711fcebf795f5f12967.png)
[root@localhost ~]# mv test.jpg /usr/share/nginx/html/
#上传图片并改名为test.jpg,并将其移动到/usr/share/nginx/html/目录下,yum安装的默认有这个目录(这个目录编译安装的nginx可能没有,没有的话自己创建)
[root@localhost ~]# chmod 644 /usr/share/nginx/html/test.jpg
#有的虚拟机上传图片后可能权限不够,需要给其添加权限。如果权限不够的话,盗链机器访问时会返回403状态码
开始配置(这里是yum安装的nginx)
[root@localhost ~]# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf,bak #备份配置文件以防万一
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下代码
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@localhost ~]# nginx -t #看看配置又没有出错
[root@localhost ~]# nginx -s reload #重新加载配置文件
然后打开网站,输入:http://192.168.13.133/test.jpg
![3c217b1f013e543987c2bc23999e8d72.png](https://i-blog.csdnimg.cn/blog_migrate/76d85023378f9c213e414b6292500da9.jpeg)
[root@localhost ~]# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf,bak #备份配置文件以防万一
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
同样清空并添加以下代码
server{
listen 80;
server_name locahost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost ~]# cp index.html index.html.bak #备份
[root@localhost ~]# vim index.html
清空并添加以下代码
<html>
<head>
<meta charset="utf-8">
<title>test.com</title>
</head>
<body style="background-color:red;">
<img src="http://192.168.13.133/test.jpg"/>
</body>
</html>
#这里面只需要将ip地址改为你自己的图片服务器的地址,其它的都是一些网页的格式
[root@localhost ~]# nginx -t #看看配置又没有出错
[root@localhost ~]# nginx -s reload #重新加载配置文件
打开网页输入盗链机器的ip地址:192.168.13.129
![f87c84275d4925d17f3822747a67830d.png](https://i-blog.csdnimg.cn/blog_migrate/8709b1411cb89e8b3581b6606fcd2fd3.jpeg)
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下代码
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
valid_referers none blocked www.jd.com; #允许这些访问
if ($invalid_referer) {
return 403;
}
}
}
- none : 允许没有http_refer的请求访问资源
- blocked : 允许不是http://开头的,不带协议的请求访问资源---被防火墙过滤掉的;
[root@localhost ~]# nginx -t #看看配置又没有出错
[root@localhost ~]# nginx -s reload #重新加载配置文件
然后打开网页输入盗链机器的ip地址:192.168.13.129
![010fb151551767b99f88b6aaafce3c41.png](https://i-blog.csdnimg.cn/blog_migrate/33109b201b878c6e21254800497d8376.png)
[root@localhost ~]# tail -f /var/log/nginx/access.log
![undefined_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=cc518916-9130-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/undefined_b.jpg)
你们的评论和点赞是我写文章的最大动力,蟹蟹。