🚀 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

Naibabiji has an Alibaba Cloud Hong Kong server. Today, I received an email saying there was an $18 prepaid fee. Logging into the backend, I found out that the traffic package was used up, so now traffic is charged on a pay-as-you-go basis. No need to guess; it's because hotlink protection was never set up, causing too much image traffic. So, I need to set up a hotlink protection rule for Nginx.

Method to Set Up Hotlink Protection in Nginx

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 above code is added directly to the LNMP virtual host configuration file. If you are also an LNMP user, you should see
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
the above content. Add the rules based on this. The values after `valid_referers none blocked` are the domains allowed to access images and not blocked. The content after `rewrite ^/` is the image displayed when hotlinking is blocked on other sites. This image cannot be placed directly on your server; otherwise, it will also be blocked by hotlink protection and won't display. If you want to add other hotlink protection content, simply add it 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 configuration file yourself, just like the nginx hotlink protection mentioned earlier, by going to the backend of Baota Panel, selecting the website, and editing the website configuration.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` to reboot. For Baota Panel, go to Software Store, find nginx in the runtime environment, click on it, and then click Restart in the service section.Reference:http://nginx.org/en/docs/http/ngx_http_referer_module.html http://www.ttlsa.com/nginx/nginx-referer/

Add Watermark to Images for Hotlink Protection

Of course, another method for hotlink protection is to not set up hotlink protection initially, but instead add watermarks to images, allowing others to copy them and advertise your website. Later, when traffic is insufficient or you feel it's time to close the net, enable hotlink protection and set the hotlinked images to display 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 instead.

「Naibabiji WordPress Website Building Coaching」 — From selecting a domain and purchasing hosting to installing themes and publishing posts, I「ll guide you through every step, helping you avoid detours and reach your goals directly.

👉 Learn about 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 to Follow