Nginx – Redirect to different url if an image is directly access

imagenginxredirectSecurity

I want to redirect users if they access the gif images directly.

http://www.example.com/uploads/file.gif
This should be redirected to home page or some other page.

Or

If the image is directly accessed, I want to show a html form or button to go to html form.

location ~* (\.gif)$ {
  rewrite ^/* /imagedisplay/ last;
}

I tried to use redirect but it didn't help.
Please advice to prevent images to be hot linked.

Best Answer

Probably you ask how to prevent hotlinking? Nginx has built in functionality to do that: http://nginx.org/en/docs/http/ngx_http_referer_module.html

See an example below:

valid_referers none blocked server_names
               *.example.com example.* www.example.org/galleries/
               ~\.google\.;

if ($invalid_referer) {
    return 301 http://example.com;
}
Related Topic