AtulHost
  • Business
    • Career and Education
    • Finance and Investments
    • Marketing Strategies
  • Productivity
  • Technology
    • Consumer Electronics
    • Solar Energy
  • Tutorials

How to enable hotlink protection in Nginx?

By AtulHost in Tutorials ⋅ August 1st, 2017
Facebook Tweet LinkedIn

Hotlinking is the biggest issue when we have limited bandwidth to serve and we all webmasters should enable hotlink protection to stay away from resource limit is reached issues.

Nginx is our favorite platform and probably yours too. Here I will explain how to enable hotlink protection in Nginx?

Hotlink Protection

We need to add this location directive in our Nginx configuration file.

location ~ \.(jpe?g|png|gif)$ {
    valid_referers none blocked example.com *.example.com;
    if ($invalid_referer) {
        return 403;
    }
}

Lets understanding the configuration.

  • Use “PIPE” Symbol, “|” to separate file extensions.
  • The directive valid_referers contains the list of site for whom hotlinking is allowed.
  • None – Matches the requests with no Referrer header.
  • Blocked – Matches the requests with blocked Referrer header.
  • *.example.com – Matches all the subdomains of example.com.

SEO Note: Enabling hotlink protection is a nice idea but it can hurt SEO rankings badly if not used properly. Third party services like Search Engine Images, Social Networking Websites relies on hotlinking method, so whitelist them all. Just add more domains to valid_referers. Some trusted domains are listed below.

  • *.google.com
  • *.googleusercontent.com
  • *.bing.com
  • *.facebook.com
  • *.twitter.com
  • *.pinterest.com

Another Scenario: If you want to block specific directory where you have tons of extension and it is hard to write all extension in the configuration file. Then we can simply tell Nginx to block everything under a directory using the following configuration.

location /images/ {
    valid_referers none blocked example.com *.example.com;
    if ($invalid_referer) {
        return 403;
    }
}

Now this will protect every file in that directory.

I hope this will tutorial will help you to enable hotlink in Nginx. In case you have any doubt or issue raise your voice in the comment section below.

Tagged with Hotlink, Hotlink Protection, nginx.
Facebook Tweet LinkedIn

Published by AtulHost

Atul is a creative blogger who loves to do experiments with the latest business initiatives and tech trends like automation, artificial intelligence, cloud and edge computing, data science, hardware as well as networking, and the internet of things.

Users also read these

  • How to Configure FastCGI Cache with Nginx?
  • How to Enable HTTP/2 on NGINX?
  • How to Start, Stop, or Restart Nginx?
  • Install Latest Stable Version of NGINX in Ubuntu
  • Nginx Config (Explained by Example)

Make a comment Cancel reply

Required fields are asterisked and your email address will not be shared.

Subscribe
10x your productivity
Increase your conversion rates

We use cookies to serve the best experience on our site. Got it!

  • About
  • Contact
  • Privacy Policy
  • Terms and Conditions
  • Write for Us
This website and its content are copyrighted © 2014-2021 by atulhost.com and individual contributors.