在 Apache2 的 Ubuntu 版本中,配置防盗链可以通过修改 Apache 配置文件或 .htaccess 文件来实现。以下是详细的步骤:
打开 Apache 配置文件:
通常,主配置文件位于 /etc/apache2/apache2.conf 或 /etc/apache2/httpd.conf。你可以使用以下命令打开它:
sudo nano /etc/apache2/apache2.conf
添加防盗链规则:
在配置文件中找到 <Directory> 或 <Location> 块,添加以下内容:
<Directory "/var/www/html">
# 允许的域名列表
SetEnvIf Referer "^http(s)?://(www\.)?example\.com$" allow_referer
SetEnvIf Referer "^$" deny_referer
# 拒绝所有其他引用
Order allow,deny
Allow from env=allow_referer
Deny from all
</Directory>
将 example.com 替换为你允许的域名。
重启 Apache 服务: 保存并关闭文件后,重启 Apache 服务以应用更改:
sudo systemctl restart apache2
.htaccess 文件创建或编辑 .htaccess 文件:
在你的网站根目录(例如 /var/www/html)下创建或编辑 .htaccess 文件:
sudo nano /var/www/html/.htaccess
添加防盗链规则:
在 .htaccess 文件中添加以下内容:
RewriteEngine On
# 允许的域名列表
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example\.com$ [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F,L]
将 example.com 替换为你允许的域名。
保存并关闭文件:
保存并关闭 .htaccess 文件。
.htaccess 文件可能会对性能产生一定影响,因为每个请求都需要检查 .htaccess 文件。通过以上步骤,你可以在 Apache2 的 Ubuntu 版本中配置防盗链,保护你的网站资源不被未经授权的网站引用。