🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

How to Set Up Nginx Hotlink Protection in Baota Panel and LNMP Environments

Naiba has an Alibaba Cloud Hong Kong server. Today, I received an email saying there was an $18 prepaid charge. Logging into the backend, I found the traffic package was used up, so now it's pay-as-you-go for traffic. No need to guess, it's because hotlink protection was never set up, and image traffic ran too high. So, I need to set a hotlink protection rule for nginx.

Method to Set Up Nginx Hotlink Protection

Insert the corresponding code into your virtual host's nginx configuration file. The complete code is as follows
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
valid_referers none blocke *.域名.com *.baidu.com *.google.com;
if ($invalid_referer) {
rewrite ^/ https://另外一个网站/2019/07/18/ZjgUET.gif;
}
}

Code Explanation

The code above is added directly to the virtual host configuration file in LNMP. If you are also an LNMP user, you should be able to see
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
the content above. Add the rules based on this. The value after `valid_referers none blocked` is the domain allowed to access images, which will not be blocked. The path after `rewrite ^/` is the image displayed when another site hotlinks; this image cannot be placed directly on your server, or it will also be blocked by hotlink protection and won't display. If you want to add other file types for hotlink protection, just add them to `gif|jpg|jpeg|png|bmp|swf`.

Baota Panel Hotlink Protection

There are two methods for hotlink protection in Baota Panel.One methodis to add nginx hotlink protection code in the website configuration file yourself in the Baota Panel backend, under Websites, Edit Website Configuration, just like the nginx hotlink protection mentioned earlier.Another methodis to directly find the Hotlink Protection option below when editing the website and turn it on.宝塔防盗链

Restart Nginx Service

After setting up, you need to restart the nginx service once to load the hotlink protection rules. For LNMP, you can use /etc/init.d/nginx restart or simply lnmp restart. For Baota Panel, find nginx in Software Store, under Runtime Environment, click on it, then click Restart in the service section.References:http://nginx.org/en/docs/http/ngx_http_referer_module.html http://www.ttlsa.com/nginx/nginx-referer/

Image Watermarking for Hotlink Protection

Of course, another method for hotlink protection is to not set it up initially, but instead add watermarks to images, letting others copy them to advertise your website. Later, when traffic is insufficient or you feel it's time to reel in, enable hotlink protection and set the blocked images to ones containing your website information, achieving the goal of having other websites advertise and promote your site for free. If you want to add watermarks to WordPress images, you can refer to the method below for automatic operation.

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step.

「Naibabiji WordPress Website Building Coaching Service」—From choosing a domain and buying hosting, to installing a Theme and publishing content, I「ll coach you through every step, helping you avoid detours and reach your goal directly.

👉 Learn about the Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan QR Code to Follow

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: