Hotlink Protection
# disable hotlinking code
location ~* \.(svg|svgz|jpg|jpeg|gif|png|ico|bmp)$ {
valid_referers blocked example.com;
if ($invalid_referer) {
# return 405;
rewrite ^/(.*)$ http://rtcamp.com/wp-content/uploads/2010/03/wp-nginx-460x154.png;
}
}
Is this implemented in a config file by default?
I am trying to set up hotlink protection for images with the following (inside my /etc/nginx/site-available/example.com server block) but it does not appear to be working correctly:
location ~* \.(svg|svgz|jpg|jpeg|gif|png|ico|bmp)$ {
valid_referers blocked example.com .feedburner.com;
if ($invalid_referer) {
return 403;
}
}
I am wondering if that location is specified elsewhere and I simply can't find it.
@jmn00 IT doesn't work because you need to set the hotlink protection within the already existing location block for these static files, where also caching is defined for these type fo files.
Have a look at this file /etc/nginx/common/locations.conf Change the cache location like so:
# Cache static files
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|swf)$ {
add_header "Access-Control-Allow-Origin" "*";
access_log off;
log_not_found off;
expires max;
valid_referers none blocked ~.google. ~.bing. ~.yahoo. ~.yandex. ~.ask. ~.t-online. ~.duckduckgo. ~.seobility. ~.gtmetrix.. ~.github. ~.rtcamp. ~.marketpress. ~.wordpress. server_names ~($host);
if ($invalid_referer) {
return 403;
}
}
This will now work globally for all of your vhost domains. Make sure to add google and other search engines with image search to your valid referers list. Feedburner should work with google. as referer, because feedburner runs through google subdomain Save file and restart nginx.
Should work then.
If you want to know how to set up nginx to serve a certain hotlink-protection-replacement image to the resource stealer, then read my blog post here: https://www.saskialund.de/nginx-hotlink-schutz/
It is written in german language ;)Maybe I should provide an english version, too
@s-a-s-k-i-a As I suspected, the location block was already specified. Works fine now. Cheers!
@nitun Thanks for the issue and suggested workaround.
We are trying to keep v4 minimal so not adding this to our scope.