How to configure anti-leech in nginx website service

1. The principle of hotlinking

1.1 Web page preparation

Web source host (192.168.153.20) configuration

cd /usr/local/nginx/html
将preview.jpg图片文件传到/usr/local/nginx/html目录下
vim index.html
......
<img src="preview.jpg"/>
</body>
</html>

echo "192.168.153.20 www.wt.com" >> /etc/hosts
echo "192.168.153.10 www.abc.com" >> /etc/hosts

Insert picture description here
Insert picture description here

Hotlink website host (192.168.153.20) configuration

为了区别于源主机的 nginx 服务,盗链主机使用一般的wed服务即可
yum install -y httpd

vim /var/www/html/index.html
<html><body><h1>Hello!</h1>
<img src="http://www.wt.com/preview.jpg"/>          
</body></html>

echo "192.168.153.20 www.wt.com" >> /etc/hosts
echo "192.168.153.10 www.abc.com" >> /etc/hosts

systemctl restart httpd

Insert picture description here

1.2 Browser access verification

Insert picture description here
Insert picture description here

Two, configure anti-theft chain

2.1 Modify the configuration file

vim /usr/local/nginx/conf/nginx.conf
http {
......
	server {
	......
		location ~* \.(jip|gif|swf)$ {
			valid_referers *.wt.com wt.com;
			if ( $invalid_referer ) {
				rewrite ^/ http://www.wt.com/error.png;
				#return 403;
			}
		}
	......
	}
}

. ~ * (Jpg | gif | swf) $: This regular expression matching is case-insensitive representation, in .jpg or .gif or .swf files ending;
valid_referers: Set a trusted site, you can use the normal picture;
later URL or domain name: the URL containing the relevant string in the referer;
if statement: if the source domain of the link is not in the list listed in valid_referers, and $invalid_referer is 1, then perform the following operation, that is, rewrite or return to the 403 page .


Insert picture description here
Transfer the error.png image file to the /usr/local/nginx/html directory
Insert picture description here

2.2 Browser access verification

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/112757756