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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值