欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

NGINX中瀏覽器的盜鏈與防止被盜的實(shí)現(xiàn)

 更新時(shí)間:2025年07月15日 10:18:16   作者:俗_人  
本文介紹在局域網(wǎng)內(nèi)實(shí)現(xiàn)盜鏈與防盜鏈的配置方法,通過(guò)Nginx設(shè)置valid_referers限制資源訪問(wèn)來(lái)源,非法引用返回403禁止訪問(wèn),感興趣的可以了解一下

1、盜鏈的實(shí)現(xiàn)(本項(xiàng)目?jī)H供參考,在局域網(wǎng)內(nèi)實(shí)現(xiàn),請(qǐng)遵守相關(guān)法律)

1.1 準(zhǔn)備操作

我們需要兩臺(tái)主機(jī),一臺(tái)為Ubuntu,作為偷盜機(jī),一臺(tái)centos作為被盜機(jī),具體配置以及IP地址如下表:

名稱IP地址CPU內(nèi)存nginx安裝方式
centos192.168.107.19022編譯安裝
Ubuntu192.168.107.18022apt安裝

1.2 nginx的安裝

nginx的安裝在此就不再贅述

見(jiàn)如下?tīng)顟B(tài)

#centos
[root@localhost ~]# systemctl start nginx.service 
[root@localhost ~]# systemctl status nginx.service 
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2025-05-07 19:40:01 CST; 24s ago
     Docs: http://nginx.org/en/docs/
  Process: 1958 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 1961 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1961 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
           ├─1962 nginx: worker process
           └─1963 nginx: worker process

5月 07 19:40:01 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
5月 07 19:40:01 localhost.localdomain systemd[1]: Started nginx - high performance web server.
[root@localhost ~]# 

#ubnutu
root@ubuntu:~# systemctl start nginx
root@ubuntu:~# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2025-05-07 11:22:53 UTC; 17min ago
       Docs: man:nginx(8)
    Process: 958 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 966 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 967 (nginx)
      Tasks: 3 (limit: 4519)
     Memory: 9.1M
        CPU: 158ms
     CGroup: /system.slice/nginx.service
             ├─967 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─968 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─969 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

May 07 11:22:53 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
May 07 11:22:53 ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
root@ubuntu:~# 

1.3 實(shí)現(xiàn)盜鏈

ubuntu操作

root@ubuntu:~# cd /var/www/html/
root@ubuntu:/var/www/html# ls
index.html  index.html.bak
root@ubuntu:/var/www/html# vim index.html
root@ubuntu:/var/www/html# 
#index.html文件內(nèi)容如下
<html>
<body>
<h1>this is ailun-yegeyu  </h1>
<img src="http://192.168.107.190/a.jpg"/>
</body>
</html>
~        

centos操作

[root@localhost data]# cd /apps/nginx/conf.d/
[root@localhost conf.d]# ls
ailun.conf
[root@localhost conf.d]# vim ailun.conf

主站點(diǎn)目錄為根目錄下的data文件夾

我們?cè)谡军c(diǎn)目錄下拖入一張a.jpg

[root@localhost ~]# cd /data/
[root@localhost data]# ls
a.jpg  index.html  main  test

網(wǎng)站測(cè)試
http://192.168.107.180/index.html

2、防盜鏈的實(shí)現(xiàn)

配置文件如下

server{
listen 80;
server_name www.ailun.com;
root /data/;

location ~* \.(jpg|gif|swf|png)$ {
         valid_referers none 192.168.107.190;
         if ( $invalid_referer ) {
           return  403;
           #rewrite ^/ http://192.168.107.190/error.png;
           }
        }


}

這段代碼是 Nginx 的配置文件片段,主要作用是配置一個(gè)虛擬主機(jī),并且對(duì)特定類型的文件進(jìn)行防盜鏈處理。下面是對(duì)代碼各部分的詳細(xì)解釋:

1. 服務(wù)器監(jiān)聽(tīng)配置

listen 80;
server_name www.ailun.com;
root /data/;
  • listen 80;:指定 Nginx 監(jiān)聽(tīng) 80 端口,這是 HTTP 協(xié)議的默認(rèn)端口。也就是說(shuō),當(dāng)有客戶端通過(guò) 80 端口發(fā)起 HTTP 請(qǐng)求時(shí),Nginx 會(huì)對(duì)其進(jìn)行處理。
  • server_name www.ailun.com;:定義了該虛擬主機(jī)的域名。當(dāng)客戶端訪問(wèn) www.ailun.com 時(shí),Nginx 會(huì)使用這個(gè)配置塊來(lái)處理請(qǐng)求。
  • root /data/;:設(shè)置該虛擬主機(jī)的根目錄為 /data/。當(dāng)客戶端請(qǐng)求一個(gè)文件時(shí),Nginx 會(huì)在 /data/ 目錄下查找對(duì)應(yīng)的文件。

2. 特定文件類型的位置塊配置

location ~* \.(jpg|gif|swf|png)$ {
  • location 是 Nginx 中用于匹配請(qǐng)求 URI 的指令。
  • ~* 表示使用不區(qū)分大小寫(xiě)的正則表達(dá)式進(jìn)行匹配。
  • \.(jpg|gif|swf|png)$ 是一個(gè)正則表達(dá)式,用于匹配以 .jpg、.gif、.swf.png 結(jié)尾的請(qǐng)求 URI。也就是說(shuō),當(dāng)客戶端請(qǐng)求這些類型的文件時(shí),會(huì)進(jìn)入這個(gè) location 塊進(jìn)行處理。

3. 防盜鏈配置

valid_referers none 192.168.107.190;
  • valid_referers 指令用于指定合法的 Referer 頭部值。Referer 頭部會(huì)在瀏覽器請(qǐng)求資源時(shí)攜帶,用于表示請(qǐng)求是從哪個(gè)頁(yè)面發(fā)起的。
  • none 表示允許沒(méi)有 Referer 頭部的請(qǐng)求,也就是直接在瀏覽器地址欄輸入資源鏈接的請(qǐng)求。
  • 192.168.107.190 表示允許從 IP 地址為 192.168.107.190 的頁(yè)面發(fā)起的請(qǐng)求。

4. 非法引用處理

if ( $invalid_referer ) {
    return  403;
    #rewrite ^/ http://192.168.107.190/error.png;
}
  • $invalid_referer 是 Nginx 的一個(gè)內(nèi)置變量,如果請(qǐng)求的 Referer 頭部值不在 valid_referers 指定的列表中,這個(gè)變量的值為 1,否則為 0。
  • if ( $invalid_referer ) 表示如果 Referer 頭部值不合法,則執(zhí)行 if 塊內(nèi)的代碼。
  • return 403; 表示返回 403 狀態(tài)碼,即禁止訪問(wèn)??蛻舳藭?huì)收到一個(gè)“禁止訪問(wèn)”的錯(cuò)誤頁(yè)面。
  • #rewrite ^/ http://192.168.107.190/error.png; 這行代碼被注釋掉了。如果取消注釋,當(dāng) Referer 頭部值不合法時(shí),會(huì)將請(qǐng)求重定向到 http://192.168.107.190/error.png

3、驗(yàn)證

經(jīng)過(guò)筆者的多次驗(yàn)證,我們?cè)谠鹊臑g覽器上仍然可以訪問(wèn)到這張圖片

我們只能將這種原因歸結(jié)于瀏覽器的緩存問(wèn)題

我們選擇換幾個(gè)瀏覽器測(cè)試

在另一臺(tái)centos的火狐瀏覽器上

在windows10瀏覽器中

我們可以觀察到頁(yè)面已經(jīng)變成了403,防盜鏈實(shí)驗(yàn)成功

到此這篇關(guān)于NGINX中瀏覽器的盜鏈與防止被盜的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)NGINX 瀏覽器盜鏈與防止被盜內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論