搜尋
首頁運維Nginx如何設定Nginx的防盜鏈

實驗環境

•一台最小化安裝的centos 7.3虛擬機器
•設定:1核心/512mb
•nginx版本1.12.2

一、設定盜鏈網站

1.啟動一台nginx虛擬機,設定兩個網站

vim /etc/nginx/conf .d/vhosts.conf

新增以下內容

server {
 listen 80;
 server_name site1.test.com;
 root /var/wwwroot/site1;
 index index.html;

 location / {
 }
}

server {
 listen 80;
 server_name site2.test.com;
 root /var/wwwroot/site2;
 index index.html;

 location / {
 }
}

如何設定Nginx的防盜鏈

2.在宿主機編輯c:\windows\system32\ drivers\etc\hosts檔案

192.168.204.11      site1.test.com
192.168.204.11   # .建立網站根目錄

mkdir /var/wwwroot
cd /var/wwwroot
mkdir site1
mkdir site2
echo -e "<h1 id="site">site1</h1><img  src="/static/imghwm/default1.png"  data-src="https://img.php.cn/upload/article/000/887/227/168390384638564.jpg?x-oss-process=image/resize,p_40"  class="lazy"  src=&#39;1.jpg&#39; alt="如何設定Nginx的防盜鏈" >" >> site1/index.html
echo -e "<h1 id="site">site2</h1><img  src="/static/imghwm/default1.png"  data-src="https://img.php.cn/upload/article/000/887/227/168390384638564.jpg?x-oss-process=image/resize,p_40"  class="lazy"  src=&#39;http://site1.test.com/1.jpg&#39; alt="如何設定Nginx的防盜鏈" >" >> site2/index.html

4.將1.jpg上傳到/var/wwwroot/site1目錄

##5.啟動nginx服務

systemctl restart nginx
netstat -anpt | grep nginx

6.防火牆放通80埠

如何設定Nginx的防盜鏈

setenforce 0
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

7.在宿主機存取

如何設定Nginx的防盜鏈

二、設定site1.test.com防盜鏈

如何設定Nginx的防盜鏈

1.編輯nginx設定檔

server {
 listen 80;
 server_name site1.test.com;
 root /var/wwwroot/site1;
 index index.html;

 location / {
 }

 location ~ \.(jpg|png|gif|jpeg)$ {
  valid_referers site1.test.com;
  if ($invalid_referer) {
   return 403;
  }
 }
}
server {
 listen 80;
 server_name site2.test.com;
 root /var/wwwroot/site2;
 index index.html;

 location / {
 }
}

#2.重啟nginx服務

如何設定Nginx的防盜鏈

systemctl restart nginx

3.在宿主機訪問

清除瀏覽器緩存,存取

##清除瀏覽器緩存,存取

如何設定Nginx的防盜鏈

可見,防盜鏈配置起到了作用

三、配置防盜鏈返回其他資源如何設定Nginx的防盜鏈

1.編輯nginx設定檔

增加一個虛擬主機,對防盜鏈保護的資源進行重寫

server {
 listen 80;
 server_name site1.test.com;
 root /var/wwwroot/site1;
 index index.html;
 location / {
 }
 location ~ \.(jpg|png|gif|jpeg)$ {
  valid_referers site1.test.com;
  if ($invalid_referer) {
   rewrite ^/ http://site3.test.com/notfound.jpg;
   #return 403;
  }
 }
}
server {
 listen 80;
 server_name site2.test.com;
 root /var/wwwroot/site2;
 index index.html;
 location / {
 }
}
server {
 listen 80;
 server_name site3.test.com;
 root /var/wwwroot/site3;
 index index.html;
 location / {
 }
}

解釋location ~ \ .(jpg|png|gif|jpeg)$ {}為設定防盜鏈的檔案類型,使用垂直線|分隔。

valid_referers site1.test.com *.nginx.org;為白名單,使用空格分隔,可以使用*進行泛域名設定。

if ($invalid_referer) {}為判斷是否符合白名單,不符合白名單會執行{}內的內容。

rewrite ^/ ;為重寫資源,如果不合符白名單,則重寫為該位址。

return 403;代表傳回的狀態碼為403。


2.建立site3根目錄

cd /var/wwwroot
mkdir site3
echo -e "<h1 id="site">site3</h1><img  src=&#39;notfound.jpg&#39; alt="如何設定Nginx的防盜鏈" >" >> site3/index.html

#3.上傳notfound.jpg檔案至/var/wwwroot/site3目錄

#4.重啟nginx服務

systemctl restart nginx

5.在宿主機編輯c:\windows\system32\ drivers\etc\hosts檔案

增加對site3.test.com的映射

192.168.204.11      site1.test.com192.168.204.11 .com

192.168.204.11      site3.test.com

##6.在宿主機訪問



可以看到,在site2中被偷走的site1的1.jpg文件,被重定向到了site3上的notfound.jpg文件

#

以上是如何設定Nginx的防盜鏈的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
nginx行動:示例和現實應用程序nginx行動:示例和現實應用程序Apr 17, 2025 am 12:18 AM

NGINX可用於提升網站性能、安全性和可擴展性。 1)作為反向代理和負載均衡器,NGINX可優化後端服務和分擔流量。 2)通過事件驅動和異步架構,NGINX高效處理高並發連接。 3)配置文件允許靈活定義規則,如靜態文件服務和負載均衡。 4)優化建議包括啟用Gzip壓縮、使用緩存和調整worker進程。

NGINX單元:支持不同的編程語言NGINX單元:支持不同的編程語言Apr 16, 2025 am 12:15 AM

NGINXUnit支持多種編程語言,通過模塊化設計實現。 1.加載語言模塊:根據配置文件加載相應模塊。 2.應用啟動:調用語言運行時執行應用代碼。 3.請求處理:將請求轉發給應用實例。 4.響應返回:將處理後的響應返回給客戶端。

在Nginx和Apache之間進行選擇:適合您的需求在Nginx和Apache之間進行選擇:適合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有優劣,適合不同場景。 1.NGINX適合高並發和低資源消耗場景。 2.Apache適合需要復雜配置和豐富模塊的場景。通過比較它們的核心特性、性能差異和最佳實踐,可以幫助你選擇最適合需求的服務器軟件。

nginx怎麼啟動nginx怎麼啟動Apr 14, 2025 pm 01:06 PM

問題:如何啟動 Nginx?答案:安裝 Nginx啟動 Nginx驗證 Nginx 是否已啟動探索其他啟動選項自動啟動 Nginx

怎麼查看nginx是否啟動怎麼查看nginx是否啟動Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎麼關閉nginx怎麼關閉Apr 14, 2025 pm 01:00 PM

要關閉 Nginx 服務,請按以下步驟操作:確定安裝類型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服務:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自動啟動(可選):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

nginx在windows中怎麼配置nginx在windows中怎麼配置Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

nginx403錯誤怎麼解決nginx403錯誤怎麼解決Apr 14, 2025 pm 12:54 PM

服務器無權訪問所請求的資源,導致 nginx 403 錯誤。解決方法包括:檢查文件權限。檢查 .htaccess 配置。檢查 nginx 配置。配置 SELinux 權限。檢查防火牆規則。排除其他原因,如瀏覽器問題、服務器故障或其他可能的錯誤。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器