防盜鏈

nginx 防止網(wǎng)站資源被盜用模塊

 ngx_http_referer_module

配置要點(diǎn):

[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 : 允許沒(méi)有http_refer的請(qǐng)求訪問(wèn)資源;
blocked : 允許不是http://開(kāi)頭的,不帶協(xié)議的請(qǐng)求訪問(wèn)資源---被防火墻過(guò)濾掉的; server_names : 只允許指定ip/域名來(lái)的請(qǐng)求訪問(wèn)資源(白名單);
準(zhǔn)備兩臺(tái)機(jī)器,一張圖片

配置nginx配置文件,并上傳圖片

第一臺(tái)

[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服務(wù)

[root@nginx-server ~]# nginx -s reload -c /etc/nginx/nginx.conf
配置nginx訪問(wèn)頁(yè)面

第二臺(tái)
創(chuàng)建頁(yè)面

[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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容