Nginx Hotlink Protection

A few clients have recently complained about other blogs linking to their images and leeching off their bandwidth. Bandwidth is expensive and should be protected.

Here’s a working solution:

location ~ \.(jpg|jpeg|bmp|jif|bmp|gif|png)$ {
                root   /path/to/public_html/;
                valid_referers server_names none blocked;
                if ($invalid_referer) {
                return 403;
                }
            }

Some sites will need to allow some other sites to link to their images. If this is the case you can use the following rewrites – just modify and add domains as required

location ~ \.(jpg|jpeg|bmp|jif|bmp|gif|png)$ {
                root   /path/to/public_html/;
                valid_referers server_names none blocked www.theothersite.com *.theothersite.com;
                if ($invalid_referer) {
                return 403;
                }
            }

Tags: , ,

2 Comments

Leave a comment
  1. demonsked 27. Feb, 2010 at 1:32 am #

    It still not working. Other can leeching my files. Sorry for my bad english.

    location ~ \.(gif|jpg|jpeg|png|js|css|rar|exe|sis|sisx|swf|txt|tpl)$ {
    root /home/skedteam/www/public_html/;
    valid_referers server_names none blocked ~\.google\. images.yandex.ru;
    if ($invalid_referer) {
    return 403;
    }
    }

  2. Uncorrupted-Michael 28. Feb, 2010 at 8:51 pm #

    Try removing the wildcard google exception and see if it works. My guess is that the syntax isn’t quite right.

Leave a Reply

Spam Protection by WP-SpamFree