NGINX中瀏覽器的盜鏈與防止被盜的實(shí)現(xià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安裝方式 |
|---|---|---|---|---|
| centos | 192.168.107.190 | 2 | 2 | 編譯安裝 |
| Ubuntu | 192.168.107.180 | 2 | 2 | apt安裝 |
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)文章
Nginx配置ssl支持https全過(guò)程(docker版)
在Docker環(huán)境中配置Nginx使用SSL涉及準(zhǔn)備SSL證書(shū)和密鑰、編寫(xiě)Nginx配置文件以及使用Docker運(yùn)行Nginx,首先,需獲取SSL證書(shū)和密鑰,可以是自簽名或由CA頒發(fā),編寫(xiě)Nginx配置文件時(shí),確保正確引用證書(shū)和密鑰路徑,通過(guò)Docker命令或Docker Compose運(yùn)行Nginx容器2024-10-10
Nginx Lua 根據(jù)參數(shù)請(qǐng)求轉(zhuǎn)發(fā)的實(shí)現(xiàn)
本文介紹了如何使用Nginx和Lua腳本實(shí)現(xiàn)基于參數(shù)的請(qǐng)求轉(zhuǎn)發(fā),文章詳細(xì)說(shuō)明了配置方法,并提供了示例代碼,幫助讀者理解如何通過(guò)NginxLua模塊根據(jù)請(qǐng)求參數(shù)將流量轉(zhuǎn)發(fā)到不同后端服務(wù),這種方法有助于實(shí)現(xiàn)靈活的負(fù)載均衡和動(dòng)態(tài)內(nèi)容處理2022-05-05
Nginx配置移動(dòng)端和PC端自動(dòng)跳轉(zhuǎn)方式
文章介紹了如何通過(guò)Nginx配置PC端和移動(dòng)端自動(dòng)跳轉(zhuǎn),PC端和移動(dòng)端各有獨(dú)立的域名,PC端訪問(wèn)任何域名時(shí)會(huì)跳轉(zhuǎn)到www.yxf.com,移動(dòng)端訪問(wèn)任何域名時(shí)會(huì)跳轉(zhuǎn)到m.yxf.com,配置時(shí)需要修改Nginx的conf文件,使用302或301重定向2025-11-11
lnmp環(huán)境中如何為nginx開(kāi)啟pathinfo
這篇文章主要介紹了lnmp環(huán)境中如何為nginx開(kāi)啟pathinfo的方法,操作很簡(jiǎn)單,需要的朋友可以參考下2015-01-01

