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

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

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

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

1.1 準(zhǔn)備操作

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

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

1.2 nginx的安裝

nginx的安裝在此就不再贅述

見如下狀態(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文件夾

我們在站點(diǎn)目錄下拖入一張a.jpg

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

網(wǎng)站測試
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 的配置文件片段,主要作用是配置一個虛擬主機(jī),并且對特定類型的文件進(jìn)行防盜鏈處理。下面是對代碼各部分的詳細(xì)解釋:

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

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

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

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

3. 防盜鏈配置

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

4. 非法引用處理

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

3、驗(yàn)證

經(jīng)過筆者的多次驗(yàn)證,我們在原先的瀏覽器上仍然可以訪問到這張圖片

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

我們選擇換幾個瀏覽器測試

在另一臺centos的火狐瀏覽器上

在windows10瀏覽器中

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

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

相關(guān)文章

最新評論