Image hotlink protection

Материал из Webko Wiki
Перейти к навигации Перейти к поиску

I had the following Nginx image hotlink protection code within the server blocks in my virtual host file:

#Prevent hotlinking
location ~ .(gif|png|jpe?g)$ {
     valid_referers none blocked .domain.com;
     if ($invalid_referer) {
        return   403;
    }
}

Alternatively, instead of sending a 403, you can replace the actual image with a custom image on the fly, using this code:

#Prevent hotlinking
location ~ .(gif|png|jpe?g)$ {
     valid_referers none blocked .domain.com;
     if ($invalid_referer) {
        rewrite (.*)\.(jpg|jpeg|png|gif)$ http://www.domain.com/images/hotlink-warning.jpg;
    }
}

Using the above code in your site’s virtual host file will replace the hotlinked image with your custom image (example shown below). Again do not forget to replace the domain name and the path to the custom image.