Skip to content

Disable image hotlinking #34

Closed
Closed
@Fleshgrinder

Description

@Fleshgrinder

Hi perusio!

Just implemented this in my own website and thought you might be interested to include this as an optional (or standard) part of your nginx configuration. It works with Imagecache and AJAX field UI of Drupal 7. I only tested it with a Drupal 7 installation, Drupal 6 might need some adoptions.

blacklist-hotlinking.conf

### This file implements a blacklist for certain referrers. It's used to
### prevent hotlinking of your images. Must be included in server context.


## Initialize our nested if variable. For more information on this
## technique have a look at: http://wiki.nginx.org/RewriteMultiCondExample
set $invalid "";

## If the file exists set our variable to 1.
if (-f $request_filename) {
    set $invalid 1;
}

## Add here all valid referrers, use the RegEx pattern
## ~(mydomain.com) do include all possible subdomains and the
## pattern ~(mydomain.) to include all possible subdomains and
## TLDs as well. You can separate many hosts with a pipe.
## ~(mydomain.com|google.|bing.|yahoo.)
##
## More info at:
## http://www.cyberciti.biz/tips/linux-unix-bsd-nginx-webserver-security.html
## http://linuxsysadminblog.com/2009/08/using-wildcards-in-nginx-valid_referers/
valid_referers none blocked server_names ~(example.com|google.|bing.|yahoo.);

## Check the actual referer and if it's invalid set our variable to
## the previously set variable + 1.
if ($invalid_referer) {
    set $invalid "${invalid}1";
}

## If the file exists and we have an invalid referer somebody is
## hotlinking our image - prevent it!
if ($invalid = 11) {
    #return 444;
    return 403;
    ## It's wise to redirect to an image hosting service, so your
    ## bandwith won't be used to serve this picture.
    #rewrite ^.*\.(gif|jpe?g|png)$ http://example-imagehoster.com/hotlinking.jpg last;
}

static-files.conf

### This template can be included in location contexts where static
### files are going to be served.

access_log off;
expires max;
## No need to bleed constant updates. Send the all shebang in one fell
## swoop.
tcp_nodelay off;
## Set the OS file cache.
open_file_cache max=3000 inactive=120s;
open_file_cache_valid 45s;
open_file_cache_min_uses 2;
open_file_cache_errors off;

Implementation example in sites-available/drupal.conf

    ## We allow hotlinking of our banners!
    location ~* /files/banner/ {
        ## Include configuration for static files.
        include static-files.conf;
    }

    ## Drupal 7 generated image handling, i.e., imagecache in core. See:
    ## https://drupal.org/node/371374.
    location ~* /files/styles/ {
        ## Prevent hotlinking!
        include blacklist-hotlinking.conf;

        ## Include configuration for static files.
        include static-files.conf;

        ## Delegate to Drupal if file doesn't exist for Imagecache
        ## generation of the image.
        try_files $uri @drupal;
    }

    ## All static files will be served directly.
    location ~* ^.+\.(?:css|js|jpe?g|gif|ico|png|html|xml)$ {
        ## Prevent hotlinking!
        include blacklist-hotlinking.conf;

        ## Include configuration for static files.
        include static-files.conf;
    }

By default I allow no referers (valid users might not submit one), blocked (if users try to to stay anonymous) our server names, our domain including all subdomains and of course the big search engines including all subdomains and TLDs.

Hope you like it and keep up the great work!

Regards
Richard

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions