Nginx防盗链
location ~* \.(gif|jpg|png|swf|flv)$ { root html valid_referers none blocked *.nginx.com; if ($invalid_referer) { rewrite ^/ www.nginx.com
- return 404;
} } 前面的root可以不要如果你在server{}中有设置可以不需要设定
[root@host ~]# vim /usr/local/nginx/conf/vhost/test.com.conf …… location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
expires 7d;
valid_referers none blocked server_names *.test.com ;
#定义referer白名单
if ($invalid_referer) {
return 403;
#if函数的意思是:如果不是白名单内的域名,返回值:403
}
access_log off;
} …… [root@host ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@host ~]# /usr/local/nginx/sbin/nginx -s reload 检查 [root@host ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/baidu.png HTTP/1.1 403 Forbidden Server: nginx/1.12.1 Date: Mon, 11 Sep 2017 11:25:47 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive 访问被拒绝,防盗链生效