Yesterday in the QQ group, I saw a friend asking how to prevent hotlinking in the environment of LNMP. Regarding this question, Brother Jun has actually answered it in the forum. I will borrow it to share it here, and it will also be considered as a forum. notes.

Since nginx does not support .htaccess, it is not feasible to prevent it directly from this aspect. We have to solve it by modifying the configuration file.
First, we find the conf file of the domain name that needs to be protected against hotlinking. The path is: /usr/local/nginx/conf/vhost/, such as vpsok.net.conf
It is better to back it up first to avoid making a mistake. Cover it directly back, this is a good habit! ! !
Find the following section:

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

Modify it to:

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
valid_referers none blocked www.vpsok.net vpsok.net;
if ($invalid_referer) {
rewrite ^/ http://vpsaa.com/404.jpg;
#return 404;
}
expires 30d;
}

Please modify the above content according to your personal situation. I will give a basic explanation here.
The first line is gif|jpg|jpeg|png... These are the file types you need to prevent hotlinking. You can add some suffix types;
The third line is the domain name of your website, which means it is allowed. Domain name, if there are multiple, please add, pay attention to the spaces;
The fifth line is the picture seen by the hotlink, and returns a 404.jpg. The source address of this picture must be externally linked, otherwise, others will see it. There is only one XX.
After completion, save it, upload it to the original location and overwrite it, then restart lnmp to make it take effect.

/root/lnmp restart

In addition, a friend asked how to prevent collection. There seems to be no particularly effective method for this problem at present. It is recommended that you study it from the system level, such as the restrictions of iptables rules, etc.


Hong Kong/United States/Domestic High Speed ​​VPS

postid
9550

Leave a Reply