Enable Hotlink Protection (Nginx)

If you are hosting a website, you almost certainly are aware of hotlinking or bandwidth theft. If not then read on. Hotlinking is essentially the practice of linking directly to a image or download hosted on a different website. This practice is typically frowned upon amongst the community of web site owners/operators however some unscrupulous characters will always exist. Below we will outline the steps that need to be taken on Nginx Webserver to prevent hotlinking. First you will want to navigate to your Nginx configuration, typically located at /etc/nginx/conf.d. Once there, open the website configuration in which you would like to disable hotlinking. Next add the directive below.

location ~ \.(jpe?g|png|gif|pdf|xls?x|doc?x|zip)$ {
		valid_referers blocked oitibs.com *.oitibs.com;
		if ($invalid_referer) {
			return   403;
		}
	}

In this example above, any file that is requested will report a 403 Forbidden when the referrer is not your site. Note you will need to replace oitibs.com with your url. If you are running WordPress, you may prefer to disable hotlinking on the entire uploads folder which can be done with the directive below.

	location /wp-content/uploads/ {  
		location ~* \.(jpe?g|png|gif|pdf|xls?x|doc?x|zip)$ {
			valid_referers blocked oitibs.com *.oitibs.com;
			if ($invalid_referer) {
				return 403;
			}
		}
	}

Once you have made the necessary configuration changes from above, simply restart Nginx and you have now enabled hotlink protection

Did you find this article useful? Why not share it with your friends?

3 thoughts on “Enable Hotlink Protection (Nginx)

  1. I saw some website give one alternative file when the link is requested. What’s the way to do it? Can be also fine if someone steal one picture, but I like if I can write “took from http://www.miosito.com, come on my website if you want this picture” on the picture and give it to who requested.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.