I am using nginx as a reverse proxy with apache.

I have added this code to prevent hotlinking, and it is working fine so far.

#hotlinking protection
location ~* \.(png|gif|jpg|jpeg)$ {
valid_referers none blocked mydomain.com *.mydomain.com;
    if ($invalid_referer){
    return 403;

How can I make it so when somebody tries to hotlink an image, instead of a 403 error, he will get a specific anti-hotlinking image?

I tried

return 301 h++p://i.imgur.com/mypicturename.png;

but it didnt work.

I don't know very much about nginx yet, every help will be very appreciated!

  • Is there a specific reason why you wrote h++p instead of http? Aug 9, 2014 at 17:27
  • Since it wasn't the real filename, I didn't see the purpose for writing http here Aug 9, 2014 at 17:44

1 Answer 1


You can put a rewrite rule inside of the if instead of the return.

Somthing like

rewrite (.*) http://i.imgur.com/mypicturename.png redirect;

might do the trick.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .