Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I want to configure my nginx to forbid images hotlinking to other websites. Or better said I want to link image from imgur with some message instead of someone using my badwidth. This is the code I've gotten.

location ~ \.(jpe?g|png|gif)$ {
     valid_referers none blocked mywebesite.com *.mywebsite.com;
     if ($invalid_referer) {
        rewrite ^ http://imageiwanttodisplay.jpg last;

But this didn't work since the imgur image wasn't displayed on other websites and this even banned my own images on the site. Also the website is set in



So I need the location of nginx file I have to edit and to input all of these modifications to make them work.



I used this code (I removed the true links)

location / {
  if ($http_referer !~ "^http://(www\.)?mywebsite.com(/)?.*$"){
    rewrite \.(gif|jpg|png)$ http://redirectimage.jpg redirect;

And some other variations of the first code with change in location like:


Still no luck. My images are shown on a lot of other sites.

share|improve this question
If it's blocking your own site, then the rule is matching. If you copy the URL of the image, open a new window and the go straight to the image, does it still get blocked? –  Danack May 22 '13 at 23:06
I didn't test it like that and I remove the code for now. Are you saying i have to put if (!$invalid_referer) syntax ? What file to edit and is there a any change in location? –  IceD May 22 '13 at 23:34
"Are you saying i have to put if (!$invalid_referer) syntax ?" No - I'm saying that your config looks fine, so was suggesting a way to debug it. I think you may just have a typo somewhere or something else simple. –  Danack May 22 '13 at 23:42
So I edit virtual.conf file and place this code and I am good to go? –  IceD May 22 '13 at 23:43
"I am good to go?" Not if it's not working no. Were you testing the config locally rather than against the real server name? If so I suggest adding a testing domain name e.g. mywebsite.test - set it's IP address to in your hosts file and make sure the config is working before putting it live. –  Danack May 23 '13 at 0:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.