nginx 防盗链

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

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

ngx_http_referer_module

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

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

比如在www.google.com 里有一个www.baidu.com 链接,那么点击这个www.baidu.com ,它的header 信息里就有:Referer=http://www.google.com

2、防盗链配置

准备2台服务器

[root@nginx-server ~]# 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

第一台服务器做资源库

yum安装nginx

给网站目录/usr/share/nginx/html/下上传图片

启动nginx服务访问网站

第二台服务器做盗链

yum安装nginx

进入网站目录/usr/share/nginx/html下

cd /usr/share/nginx/html

rm -rf * 删除目录下所有文件

重定向配置nginx访问页面

cat >index.html <<EOF

<html>
<head>
    <meta charset="utf-8">
    <title>qf.com</title>
</head>
<body style="background-color:red;">
    <img src="http://10.36.192.90/1.png"/>
</body>
</html>

EOF

启动nginx服务访问网站

可以看到能盗链成功

进入第一台服务器

查看访问日志

可以看到请求都是来自http://10.36.192.136/同一ip请求

为该服务器做防盗链

进入nginx配置文件子文件

server {
listen 80;
server_name localhost;

location / {
     root   /usr/share/nginx/html;
     index  index.html index.htm;

     valid_referers none  *.baidu.com;
            if ($invalid_referer) {
               return 502;
            }
    }

重启nginx服务

systemctl restart nginx

none : 允许没有http_refer的请求访问资源;

blocked : 允许不是http://开头的,不带协议的请求访问资源;

server_names : 只允许指定ip/域名来的请求访问资源(白名单);

none

测试不带http_refer:

[root@mycat html]# curl -I “http://10.36.192.90/1.png”
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 23 Oct 2023 12:16:18 GMT
Content-Type: image/png
Content-Length: 270739
Last-Modified: Sat, 21 Oct 2023 08:27:13 GMT
Connection: close
ETag: “65338b61-42193”
Accept-Ranges: bytes

测试带http_refer:

[root@mycat html]# curl -e http://10.36.192.136 -I “http://10.36.192.90/1.png”
HTTP/1.1 502 Bad Gateway
Server: nginx/1.24.0
Date: Mon, 23 Oct 2023 12:22:24 GMT
Content-Type: text/html
Content-Length: 497
Connection: close
ETag: “6435975a-1f1”

blocked

测试带协议的请求访问资源:

root@mycat html]# curl -e http://www.aliyun.com -I “http://10.36.192.90/1.png”
HTTP/1.1 502 Bad Gateway
Server: nginx/1.24.0
Date: Mon, 23 Oct 2023 12:19:51 GMT
Content-Type: text/html
Content-Length: 497
Connection: close
ETag: “6435975a-1f1”

测试不带协议的请求访问资源

[root@mycat html]# curl -e www.aliyun.com -I “http://10.36.192.90/1.png”
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 23 Oct 2023 12:20:31 GMT
Content-Type: image/png
Content-Length: 270739
Last-Modified: Sat, 21 Oct 2023 08:27:13 GMT
Connection: close
ETag: “65338b61-42193”
Accept-Ranges: bytes

server_names

测试允许的ip访问

[root@mycat html]# curl -e http://10.36.192.136 -I “http://10.36.192.90/1.png”
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 23 Oct 2023 12:27:45 GMT
Content-Type: image/png
Content-Length: 270739
Last-Modified: Sat, 21 Oct 2023 08:27:13 GMT
Connection: close
ETag: “65338b61-42193”
Accept-Ranges: bytes

测试不允许的ip访问

[root@mycat html]# curl -e http://10.36.192.2 -I “http://10.36.192.90/1.png”
HTTP/1.1 502 Bad Gateway
Server: nginx/1.24.0
Date: Mon, 23 Oct 2023 12:29:13 GMT
Content-Type: text/html
Content-Length: 497
Connection: close
ETag: “6435975a-1f1”