教你怎么防止别人盗用你的图片,nginx防盗链

两个网站 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
3.图片网站服务器配置(192.168.13.133)

在这里插入图片描述
我们就以这张图片为例

[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
在这里插入图片描述
显然图片上传成功

4.盗链机器配置(192.168.13.129)
[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
在这里插入图片描述
你会发现盗链机器可以盗取你的图片
怎么样防止呢?

5.防盗链(192.168.13.133)
[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
在这里插入图片描述
你会发现,盗链机器看不到图片了。
而且你查看图片服务器机器的访问日志

[root@localhost ~]# tail -f /var/log/nginx/access.log 

在这里插入图片描述
你会发现,访问你机器的ip被显示出来了。

发布了13 篇原创文章 · 获赞 27 · 访问量 3638

猜你喜欢

转载自blog.csdn.net/baidu_38803985/article/details/104802695