LNMP配置——Nginx配置 ——Nginx防盗链
一、配置
#vi /usr/local/nginx/conf/vhost/test.com.conf
写入:
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
if ($host != 'test.com' ){
rewrite ^(.*)$ http://test.com/$1 permanent;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
valid_referers none blocked server_names *.test.com ;
if ($invalid_referer) {
return 403;
//如果来源网址的不是test.com的域名,则返回403
}
access_log /tmp/1.log combined_realip;
}
}
#/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

#/usr/local/nginx/sbin/nginx -s reload
# service nginx stop
# service nginx start

二、检验:
#curl -x127.0.0.1:80 -e "http://test.com/1.txt" test.com/2.jpg -I

状态码200
#curl -x127.0.0.1:80 -e "http://aaa.com/1.txt" test.com/2.jpg -I

状态码403
检验成功!

浙公网安备 33010602011771号