Nginx 防盗链

两个网站 A 和 B, B网站引用了A网站上的图片,这种行为就叫做盗链。 防盗链,就是要防止B引用A的图片。

一、nginx 防止网站资源被盗用模块

ngx_http_referer_module

如何区分哪些是不正常的用户?

​ HTTP Referer是Header的一部分,当浏览器向Web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理,例如防止未经允许的网站盗链图片、文件等。因此HTTP Referer头信息是可以通过程序来伪装生成的,所以通过Referer信息防盗链并非100%可靠,但是,它能够限制大部分的盗链情况。

二、防盗链配置

配置要点:

[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"';

# valid_referers 使用方式                         
Syntax:     valid_referers none | blocked | server_names | string ...;
Default:    —
Context: server, location
  • none : 允许没有http_referer的请求访问资源;
  • blocked : 允许不是http://开头的,不带协议的请求访问资源---被防火墙过滤掉的;
  • server_names : 只允许指定ip/域名来的请求访问资源(白名单);

准备两台机器,一个图片网站服务器,一个盗链机服务器

示例1

1. 图片服务器配置 192.168.181.128

[root@localhost conf.d]# cat default.conf 
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

[root@localhost html]# ls
50x.html  index.html  test.jpg
测试访问:
image.png
图片服务器查看日志:
image.png

Referer:这个匹配的连接为空 “-”

2. 盗链机配置 192.168.181.130

[root@localhost conf.d]# cat default.conf 
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

[root@localhost html]# cat index.html
<html>
<head>
    <meta charset="utf-8">
</head>
<body style="background-color:red;">
    <img src="http://192.168.181.128/test.jpg"/>
</body>
</html>
测试访问:
image.png
图片服务器查看日志:
image.png

Referer记录了:连接是192.168.181.130这台机器。


在图片服务器操作

[root@localhost conf.d]# cat 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;
                }
    }
}
测试访问:
image.png
图片服务器查看日志:
image.png

上面配置并没有允许192.168.181.130这台机器访问。

示例2

1. 图片服务器配置 192.168.181.128

[root@localhost conf.d]# cat 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 192.168.181.130;
                if ($invalid_referer) {
                   return 403;
                }
    }
}

因为none允许为空值访问,所以加不加ip都可以访问,如果把none擦除,就不可以了
重载nginx服务

2. 在盗链机测试:

# 测试不带http_refer:
[root@localhost ~]# curl -I "http://192.168.181.128/test.jpg"     # -I 显示头信息
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 11 Mar 2020 04:09:31 GMT
Content-Type: image/jpeg
Content-Length: 1309243
Last-Modified: Thu, 05 Mar 2020 11:11:00 GMT
Connection: keep-alive
ETag: "5e60de44-13fa3b"
Accept-Ranges: bytes

# 测试带非法http_refer:
[root@localhost ~]# curl -e "http://www.baiud.com" -I "http://192.168.181.128/test.jpg"    
 # -e指定访问链接
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Wed, 11 Mar 2020 04:11:33 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

# 测试带合法的http_refer:
[root@localhost ~]# curl -e "http://www.jd.com" -I "http://192.168.181.128/test.jpg"
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 11 Mar 2020 04:13:48 GMT
Content-Type: image/jpeg
Content-Length: 1309243
Last-Modified: Thu, 05 Mar 2020 11:11:00 GMT
Connection: keep-alive
ETag: "5e60de44-13fa3b"
Accept-Ranges: bytes

# 盗链机访问
[root@localhost ~]# curl -e "http://192.168.181.130" -I "http://192.168.181.128/test.jpg"
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 11 Mar 2020 04:14:39 GMT
Content-Type: image/jpeg
Content-Length: 1309243
Last-Modified: Thu, 05 Mar 2020 11:11:00 GMT
Connection: keep-alive
ETag: "5e60de44-13fa3b"
Accept-Ranges: bytes

如果用户直接在浏览器输入你的图片地址,那么图片显示正常,因为它符合规则。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 获取全套nginx教程,请访问瓦力博客 防盗链是一种机制,也可以说是一种技术,目的是防止自己网站的东西(如图片,文...
    滔滔逐浪阅读 546评论 0 13
  • 为了防止资源给盗用,从而给我们的所在服务器造成额外的负担,需要采取防盗链的措施。 使用 nginx 模块ngx_h...
    JJNile阅读 2,283评论 0 0
  • nginx防盗链的方法 一般,我们做好防盗链之后其他网站盗链的本站图片就会全部失效无法显示,但是您如果通过浏览器直...
    很少更新了阅读 8,573评论 1 3
  • 修改Nginx网站的配置 首先找到nginx.conf文件,找到文件中的server{} 虚拟机配置 在serve...
    岑吾阅读 167评论 0 1
  • 做博客的朋友经常会有一些烦恼,比如网站总是被人抄袭,比如网站总是被人引用上面的图片,虽然一张图两张图,并不耗费很多...
    沧海一粟谦阅读 507评论 0 3