在Ubuntu下配置Apache防盜鏈,可以通過修改Apache的配置文件來實現。以下是詳細的步驟:
如果你還沒有安裝Apache,可以使用以下命令進行安裝:
sudo apt update
sudo apt install apache2
你可以在Apache的配置文件中添加防盜鏈規則。通常,這些規則會放在/etc/apache2/conf-available/
目錄下的一個文件中,例如security.conf
。
sudo nano /etc/apache2/conf-available/security.conf
在文件中添加以下內容:
<IfModule mod_rewrite.c>
RewriteEngine On
# 允許的來源域名列表
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?anotherdomain\.com [NC]
# 防盜鏈的圖片路徑
RewriteRule \.(jpg|jpeg|png|gif)$ - [F,L]
</IfModule>
在這個例子中:
RewriteEngine On
啟用重寫引擎。RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
檢查HTTP_REFERER是否不是來自example.com
或其子域名。RewriteCond %{HTTP_REFERER} !^http://(www\.)?anotherdomain\.com [NC]
檢查HTTP_REFERER是否不是來自anotherdomain.com
或其子域名。RewriteRule \.(jpg|jpeg|png|gif)$ - [F,L]
如果條件滿足,則返回403 Forbidden狀態碼,并停止進一步處理。創建或編輯完防盜鏈配置文件后,需要啟用該配置:
sudo a2enconf security
使配置生效,需要重啟Apache服務:
sudo systemctl restart apache2
你可以通過訪問你的網站并嘗試從不允許的來源加載圖片來測試防盜鏈規則是否生效。如果一切配置正確,你應該會看到403 Forbidden錯誤。
mod_rewrite
模塊。如果沒有啟用,可以使用以下命令啟用:sudo a2enmod rewrite
通過以上步驟,你就可以在Ubuntu下成功配置Apache防盜鏈了。