防盗链

作者: 一剑仙人跪_ | 来源:发表于2019-07-31 20:49 被阅读0次

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

 ngx_http_referer_module

配置要点:

[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

none : 允许没有http_refer的请求访问资源;
blocked : 允许不是http://开头的,不带协议的请求访问资源---被防火墙过滤掉的; server_names : 只允许指定ip/域名来的请求访问资源(白名单);
准备两台机器,一张图片

配置nginx配置文件,并上传图片

第一台

[root@nginx-server html]# vim /etc/nginx/conf.d/nginx.conf 
server {    
    listen       80;   
    server_name  localhost;

   location ~  .*\.(gif|jpg|png|jpeg)$ {        
         root  /usr/share/nginx/html;
         valid_referers none blocked *.qf.com 192.168.1.10;               
          if ($invalid_referer) {                   
                         return 403;               
            }      
    }
 }

重载nginx服务

[root@nginx-server ~]# nginx -s reload -c /etc/nginx/nginx.conf
配置nginx访问页面

第二台
创建页面

[root@nginx-server nginx]# vim index.html 
<html> <head>    
    <meta charset="utf-8">    
    <title>qf.com</title> 
</head> 
<body style="background-color:red;">   
     <img src="http://192.168.1.10/test.jpg"/> 
</body> 
</html>

相关文章

网友评论

      本文标题:防盗链

      本文链接:https://www.haomeiwen.com/subject/ezdxdctx.html