Looking for a method for Nginx hotlink protection?
A quick way to prevent other websites from using your media files is to restrict its access.
However, this requires modification in the Nginx files.
That’s why we frequently deal with hotlinking protecting requests as part of our Server Management Services.
Today, we’ll see how we quickly set up hotlink protection in Nginx servers.
What is hotlink protection?
Let’s now try to understand more details of hotlinking.
In simple terms, hotlinking is a way in which other websites use your images, video files, etc. on their pages. Thus when any person accesses these pages, it leads to the bandwidth usage on your website. This, in turn, causes the website to hit bandwidth limits. Or you will have to pay for bandwidth overages.
Therefore, the website owners enable hotlinking protection for their website. It prevents others from using your media files. In other words, these files will be restricted to your website.
Where do we use Nginx hotlink protection?
It’s time to check a couple of hotlink protection requests that we normally receive.
Recently, one of our customers came up with a requirement.
We want only the website itself to display the videos.
If any other website tries to display the video, then it should be blocked with a 403 error or redirect to our website.
Similarly, most WordPress websites restrict their media file access. This can be their image files, videos, etc.
Again, the hotlink protection depends a lot on the type of web server too.
Steps for enabling Nginx hotlink protection
We’ll now check the steps to enable hotlink protection in the Nginx server.
It works based on the location directive in Nginx.
Here, the customer wanted to restrict access to the .mp4 files. So our Support Engineers added these rules in the Nginx configuration file for the domain.
location ~* \.(mp4)$ {
root /home/xxx/web/xxx.com/public_html;
access_log /var/log/httpd/domains/xxx.com.log combined;
access_log /var/log/httpd/domains/xxx.com.bytes bytes;
expires max;
valid_referers none blocked xxx.com;
if ($invalid_referer) {
return 403;
}
}
In this code snippet, the parameter ‘valid_referers‘ mention the websites that can link to the media files. For all other websites, it returns a 403 forbidden error.
Similarly, for restricting access to the image files, CSS, etc, the location directive will be:
location ~ .(gif|png|jpg|jpe?g|css|ico)$
Common errors in Nginx hotlink protection
Let’s now check the common errors that come up in hotlink protection.
1. Incorrect Plesk settings
Often incorrect Plesk settings can cause the hotlink protection to fail. For instance, a customer added Nginx rules to prevent linking to their image files.
However, the images were still accessible on other websites.
On a detailed check, our Support Engineers found that the option Serve static files directly by nginx option was disabled. Thus, the request for image files did not reach Nginx.
Therefore, to fix the problem, we enabled the option and hotlink protection worked fine.
2. Syntax error in rules
Likewise, syntax errors in the Nginx rules can also create problems with hotlinking protection. For instance, to exclude all the subdomains from hotlinking protection, we include *.domain name in the valid_referer parameter.
Similarly, specifying the rules in the wrong location also makes the rule ineffective.
Thus, our Support Engineers always double-check and add the Nginx rules in the right location.
[Need help with hotlink protection? We are available 24×7 to make it right.]
Conclusion
In short, Nginx hotlink protection prevents media file theft from the website. Today, we saw how our Support Engineers enable the hotlinking protection in the Nginx server.
0 Comments