Simple hotlink protection implemented in nginx

This blog does not use more than four or five gigabytes per month, which is only about one hundredth of the available bandwidth. Still, I do not like bandwidth leechers, so I have implemented simple hotlink protection using nginx HTTP server.

I want to protect assets directory, which contains images, documents and other static files.

Examine the following directive to implement it in your own configuration.

location /assets/ {
        valid_referers none blocked server_names example.org *.example.org;
        if ($invalid_referer) {
            return   403;
        }
        expires 4w;
}

location / {
        try_files $uri $uri/ =404;
}

It won't solve every possible problem, but is sufficient to provide simple protection against unwanted behavior.

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a system administrator working for a successful Polish company and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu