在 Ubuntu 上配置防盗链
一、Apache2 配置
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|webp|svg)$ - [F,L]
二、Nginx 配置
location ~* \.(jpg|jpeg|png|gif|bmp|webp|svg)$ {
valid_referers none blocked server_names yourdomain.com *.yourdomain.com;
if ($invalid_referer) {
return 403;
# 或重定向到提示页:rewrite ^/ https://yourdomain.com/forbidden.html redirect;
}
expires 30d;
add_header Cache-Control "public, immutable";
}
三、测试与注意事项