Image hotlinking prevention via nginx, valid referers always got 404

i have tried tweaking the nginx config to prevent image hotlinking in my current ghost setup, by adding a location block like this :

location ~ .(gif|ico|png|jpe?g)$ { 
  valid_referers 
      server_names 
      example.com 
      ;             
  
  if ($invalid_referer) {
    return 403;
  }
}

and included this conf inside :

/etc/nginx/site-enabled/example.com-ssl.conf

before the location / block.

and now, invalid referers will get 403 as expected, but all valid referers will get 404 instead of 200.

anyone has any ideas why ?

It looks like you don’t have any routing configured (e.g. try_files or proxy_pass directives)

2 Likes

@vikaspotluri123 ,

yes, you’re right, and thanks for your help again ~