0

First, sorry for my english.

I try do some special hotlink prevent on my website.

For example i have images on

http://s1.example.com/i/img_123.jpg And if someone use this image on your own website in code, not redirect. But if he visit directly this image, nginx redirect to http://example.com/z/img_123.jpg <- not image but page with html code, etc.

How can I get it?

Zdunek
  • 21
  • 4

2 Answers2

0

This should do the job:

server {                                                 
  root /var/www;

  index index.htm;
  server_name s1.sitename;

  location ~ \.(jpe?g|png|gif)$ {
    valid_referers blocked example.com;
    if ($invalid_referer) { 
      # returning an htm. You can replace this with an image
      return 403 /error.htm;
    }
  }
}

EDIT - Second version of my answer

location ~ \.(jpe?g|png|gif)$ {
  valid_referers blocked example.com;
  if ($invalid_referer) {
    return 403 /error.htm;
  }

  # If not invalid. we make a URL rewrite
  rewrite i/(.*) http://www.example.com/z/$1 redirect;

  # this is a suggestion. Enable expire. It's static data, and you should not
  # waste resources serving these files every single time.
  expires 30;

  break;
}
alfredocambera
  • 3,155
  • 34
  • 29
  • Hello, not what I meant. It's should not block views with referers. But thanks for answer. – Zdunek Dec 01 '13 at 23:21
  • It doesn't block everything. It allows traffic from sites on the list "valid_referers". You may add more domains or epxressions like "*.example.com" to allow any subdomain. – alfredocambera Dec 01 '13 at 23:52
-1

I have answer for my problem :)

if ($http_referer = '') {
    rewrite i/(.*) http://www.example.com/z/$1 redirect;
    expires off;
    break;
}
Zdunek
  • 21
  • 4
  • What you are doing here is redirecting clients with not 'referal' to the other site. Clients coming from any other site will be allowed. I think you should update update $http_referer with something more specific. – alfredocambera Dec 09 '13 at 01:00