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

I have the following configuration in nginx:

  location /static/ {
       root /srv/kose/;
       expires 2w;
       access_log off;
 }


 location / {
    proxy_pass http://127.0.0.1:8089;

}

If a file is not found in /static/, I want to serve a default image, and not proxy_pass to 8089. Currently, it looks for the file in the root for static, if it cannot find it, it tries the proxy.

I have tried the following, but it doesn't work. How can I tell nginx to serve the default image? I have also tried try_files to no avail.

  location /static/ {
       root /srv/kose/;
       expires 2w;
       access_log off;
       error_page 404 /srv/static/defaultimage.jpg;
 }


 location / {
    proxy_pass http://127.0.0.1:8089;

}

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

The following works:

 location /static/ {
       root /srv/kose/;
       expires 2w;
       access_log off;
       try_files $uri /static/img/yazar/authornoimage.jpg;

 }

Note that instead of the file location, I had to write the URL of the default image for try_files.

share|improve this answer
    
Keep in mind that this is bad way because status will be 200 and this image will be indexed endlessly by search engines. Moreover catch-all techniques are rather penalized by search engines. –  gertas Mar 20 '13 at 7:38
add comment
location /static/ {
    root /srv/kose;
    expires 2w;
    access_log off;
    error_page =200 /defaultimage.jpg;
}

location = /defaultimage.jpg {
    internal;
    root /srv/static;
    expires 2w;
    access_log off;
}

or

location /static/ {
    root /srv;
    expires 2w;
    access_log off;
    try_files /kose$uri /static/defaultimage.jpg =404;
}
share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.