ngx_http_referer_module模块说明

该模块可以进行防盗链设置。 盗链的含义是网站内容本身不在自己公司的服务器上,而通过技术手段,直接在调用其他公司的服务器 网站数据,而向最终用户提供此内容。

Syntax:    valid_referers none | blocked | server_names | string ...;
Default:    —
Context:    server, location


#none:请求报文首部没有referer首部
#blocked:请求报文有referer首部,但无有效值
#server_names:referer首部中包含本主机名
#arbitrary_string:任意字符串,但可使用*作通配符
#regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头,

示例:

location ~* \.(gif|jpg|png|swf|flv)$ {
  valid_referers none blocked lutixia.net *.jfedu.net https://10.0.0.101/* ; #定义访问原始站点不过滤 
  root /usr/share/nginx/html;
  if ($invalid_referer) {
    return 403;
  }
}

#valid_referers 表示合法的referers设置
#none: 表示没有referers,直接通过浏览器或者其他工具访问。
#blocked: 表示有referers,但是被代理服务器或者防火墙隐藏;
#lutixia.net: 表示通过lutixia.net访问的referers;
#*.jfedu.net: 表示通过*.jfedu.net访问的referers,*表示任意host主机。

测试实验

实验环境

192.168.32.211  原始nginx服务器  
192.168.32.212  盗链服务器,盗链原始服务器下的192.168.32.211/day.jpg图片
192.168.32.213  客户机

192.168.32.211和192.168.32.212的nginx配置一样

[root@node1 ~]# uname -r
3.10.0-957.el7.x86_64   
[root@node1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@node1 ~]# nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

实验要求

1、配置nginx 192.168.32.212可以盗链192.168.32.211上的day.jgp
2、通过在192.168.32.211上配置防盗链,让客户只能访问192.168.32.211/day.jpg才能获取图片信息

实验步骤:

1、安装nginx请参考nginx的安装部署

2、在原始服务器192.168.32.211上把day.jpg图片拷贝到发布目录下,客户机可以访问

cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html/

客户机192.168.32.213可以正常访问192.168.32.211/day.jgp

[root@node3 ~]# curl -I 192.168.32.211/day.jpg
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Sat, 12 Sep 2020 13:19:39 GMT
Content-Type: image/jpeg
Content-Length: 961243
Last-Modified: Sat, 12 Sep 2020 13:11:08 GMT
Connection: keep-alive
ETag: "5f5cc8ec-eaadb"
Accept-Ranges: bytes

客户机192.168.32.213无法访问192.168.32.212/day.jgp

[root@node3 ~]# curl -I 192.168.32.212/day.jpg
HTTP/1.1 404 Not Found
Server: nginx/1.16.0
Date: Sat, 12 Sep 2020 13:22:22 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

3、在盗链机192.168.32.212上盗取192.168.32.211上的day.jpg

配置盗链主页

cat >> /usr/local/nginx/html/index.html <<EOF
node2-32.212
<html>
<h1>welcome to 192.168.32.212</h1>
<img src="http://192.168.32.211/day.jpg">
</html>
EOF

4、客户机192.168.32.213可以通过浏览器访问192.168.32.212/day.jgp,来得到192.168.32.211/day.jgp

5、在原始服务器192.168.32.211上配置防盗链

server的配置

server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~* \.(gif|jpg|png|swf|flv)$ {
            valid_referers none blocked  https://10.0.0.101/* ;
            if ($invalid_referer) {
                  return 403 "Forbidden Access";
            }
        }
}

重新加载配置文件

nginx -t
nginx -s reload

访问测试

已经无法在192.168.32.212上获取原始服务器的图片了

原始服务器仍可以正常访问

posted @ 2020-09-12 13:58  yaowx  阅读(692)  评论(0编辑  收藏  举报