通过 Nginx 实现防盗链
在这个网络有宽带有上限,流量收费的环境下,图片防盗链接就有实际意义了。
# 防盗链,valid_referers none blocked 可允许无引用访问。
#location ~* /wp-content/.*(gif|png|jpeg|jpg|svg|7z|zip|rar|exe|pdf)$ {
location ~* ^/wp-content/uploads/20.*$ {
root D:/WordPress;
valid_referers y.z *.y.z *.google.com *.bing.com *.baidu.com;
if ($invalid_referer) {
return 301 https://y.z/anti-theft.png;
}
}
y.z/anti-theft.png 的图片

防盗链中开放特定ip:word博客场景
# 防盗链,【valid_referers none blocked】可允许无引用访问,PDF.js View插件是有引用访问。
#location ~* /wp-content/.*(gif|png|jpeg|jpg|svg|7z|zip|rar|exe|pdf)$ {
location ~* ^/wp-content/uploads/20.*$ {
# IP检查通过,赋值为1;win版nginx没有geo或map模块。
set $bypass_ref 0;
# ip段的条件:($remote_addr ~ ^10\.0\.0\.)
if ($remote_addr = 1.2.3.4) {
set $bypass_ref 1;
}
valid_referers y.z *.y.z *.google.com *.bing.com *.baidu.com;
# 无效引用访问,赋值为${bypass_ref}1
if ($invalid_referer) {
set $bypass_ref "${bypass_ref}1";
}
# ip检查失败=0,无效引用访问=1
if ($bypass_ref = 01) {
return 301 https://y.z/anti-theft.png;
}
root D:/WordPress;
}
发表回复