nginx系统真正有效的图片防盗链完整设置详解

我在本地配置了虚拟域名用的是phpstudy   只需要在nginx中找到    ginxconf 找到nginx.conf或者是vhosts.conf  这个得看你的虚拟域名在那个文件 然后添加以下代码就行了

location ~* .(gif|jpg|png|bmp)$ {

  expires     30d;
  valid_referers none blocked *.tp.com server_names ~.tp. ~.baidu.;
  if ($invalid_referer) {
  #return 403;
  rewrite ^/ http://ww4.sinaimg.cn/bmiddle/051bbed1gw1egjc4xl7srj20cm08aaa6.jpg;
  }
}

其中“gif|jpg|jpeg|png|bmp|swf”设置防盗链文件类型,自行修改,每个后缀用“|”符号分开!

valid_referers none blocked *.it300.com it300.com;   这个就是白名单,允许文件链出的域名白名单,自行修改成您的域名!*.it300.com这个指的是子域名,域名与域名之间使用空格隔开!

rewrite ^/ http://www.it300.com/static/images/404.jpg; 这个图片是盗链返回的图片,也就是替换盗链网站所有盗链的图片。这个图片要放在没有设置防盗链的网站上,因为防盗链的作用,这个图片如果也放在防盗链网站上就会被当作防盗链显示不出来了,盗链者的网站所盗链图片会显示X符号。

以下是我整个配置信息

server {
listen 80;
server_name www.tp.com;
root "E:/phpStudy/WWW/tp/public/";
location / {
index index.html index.htm index.php l.php;
autoindex on;
try_files $uri $uri/ /index.php?s=$uri&$args;
}


location ~* .(gif|jpg|png|bmp)$ {
valid_referers *.tp.com server_names ~.tp. ~.baidu.;

expires     30d;
if ($invalid_referer) {
#return 403;
rewrite ^/ http://ww4.sinaimg.cn/bmiddle/051bbed1gw1egjc4xl7srj20cm08aaa6.jpg;
}
}


error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ .php { #去掉$
root E:/phpStudy/WWW/tp/public/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+.php)(.*)$; #增加这一句
fastcgi_param PATH_INFO $fastcgi_path_info; #增加这一句
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

}

}

原文地址:https://www.cnblogs.com/kzfbk/p/7302634.html