NGINX中瀏覽器的盜鏈與防止被盜的實現(xiàn)
1、盜鏈的實現(xiàn)(本項目僅供參考,在局域網內實現(xiàn),請遵守相關法律)
1.1 準備操作
我們需要兩臺主機,一臺為Ubuntu,作為偷盜機,一臺centos作為被盜機,具體配置以及IP地址如下表:
名稱 | IP地址 | CPU | 內存 | nginx安裝方式 |
---|---|---|---|---|
centos | 192.168.107.190 | 2 | 2 | 編譯安裝 |
Ubuntu | 192.168.107.180 | 2 | 2 | apt安裝 |
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 實現(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文件內容如下 <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
主站點目錄為根目錄下的data文件夾
我們在站點目錄下拖入一張a.jpg
[root@localhost ~]# cd /data/ [root@localhost data]# ls a.jpg index.html main test
網站測試http://192.168.107.180/index.html
2、防盜鏈的實現(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 的配置文件片段,主要作用是配置一個虛擬主機,并且對特定類型的文件進行防盜鏈處理。下面是對代碼各部分的詳細解釋:
1. 服務器監(jiān)聽配置
listen 80; server_name www.ailun.com; root /data/;
listen 80;
:指定 Nginx 監(jiān)聽 80 端口,這是 HTTP 協(xié)議的默認端口。也就是說,當有客戶端通過 80 端口發(fā)起 HTTP 請求時,Nginx 會對其進行處理。server_name www.ailun.com;
:定義了該虛擬主機的域名。當客戶端訪問www.ailun.com
時,Nginx 會使用這個配置塊來處理請求。root /data/;
:設置該虛擬主機的根目錄為/data/
。當客戶端請求一個文件時,Nginx 會在/data/
目錄下查找對應的文件。
2. 特定文件類型的位置塊配置
location ~* \.(jpg|gif|swf|png)$ {
location
是 Nginx 中用于匹配請求 URI 的指令。~*
表示使用不區(qū)分大小寫的正則表達式進行匹配。\.(jpg|gif|swf|png)$
是一個正則表達式,用于匹配以.jpg
、.gif
、.swf
或.png
結尾的請求 URI。也就是說,當客戶端請求這些類型的文件時,會進入這個location
塊進行處理。
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 的一個內置變量,如果請求的Referer
頭部值不在valid_referers
指定的列表中,這個變量的值為1
,否則為0
。if ( $invalid_referer )
表示如果Referer
頭部值不合法,則執(zhí)行if
塊內的代碼。return 403;
表示返回 403 狀態(tài)碼,即禁止訪問??蛻舳藭盏揭粋€“禁止訪問”的錯誤頁面。#rewrite ^/ http://192.168.107.190/error.png;
這行代碼被注釋掉了。如果取消注釋,當Referer
頭部值不合法時,會將請求重定向到http://192.168.107.190/error.png
。
3、驗證
經過筆者的多次驗證,我們在原先的瀏覽器上仍然可以訪問到這張圖片
我們只能將這種原因歸結于瀏覽器的緩存問題
我們選擇換幾個瀏覽器測試
在另一臺centos的火狐瀏覽器上
在windows10瀏覽器中
我們可以觀察到頁面已經變成了403,防盜鏈實驗成功
到此這篇關于NGINX中瀏覽器的盜鏈與防止被盜的實現(xiàn)的文章就介紹到這了,更多相關NGINX 瀏覽器盜鏈與防止被盜內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
nginx+apache+mysql+php+memcached+squid搭建集群web環(huán)境
當前,LAMP開發(fā)模式是WEB開發(fā)的首選,如何搭建一個高效、可靠、穩(wěn)定的WEB服務器一直是個熱門主題,本文就是這個主題的一次嘗試。2011-03-03使用Nginx反向代理與proxy_cache緩存搭建CDN服務器的配置方法
linux下通過Nginx反向代理和proxy_cache緩存搭建CDN服務器加快Web訪問速度的配置方法2013-06-06Nginx worker_connections配置太低導致500錯誤案例
這篇文章主要介紹了Nginx worker_connections配置太低導致500錯誤案例,需要的朋友可以參考下2015-04-04nginx提示:500 Internal Server Error錯誤的解決方法
本文章來給大家總結了大量關于導致nginx中提示500 Internal Server Error錯誤的原因總結與解決方法分析有需要了解的朋友可參考參考2013-04-04