# How to get a 404 logged with try\_files?

**URL:** <https://www.ruby-forum.com/t/how-to-get-a-404-logged-with-try-files/206469>\
**Category:** NGINX\
**Created:** [May 2, 2011, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-get-a-404-logged-with-try-files/206469 "2011-05-02T16:23:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 2, 2011, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-get-a-404-logged-with-try-files/206469/1 "2011-05-02T16:23:28Z")

</div>

Hi! Just started with Nginx after reading it reached 1.0.  
Congratulation!

I’m trying to get my test site to return a “missing image” icon when an  
image is missing, but have a 404 logged so I know I messed up something.  
I want to specify the images name without file extensions, in case I  
move some from PNG to JPG, or the other way around. My problem is that  
try\_files doc says last path MUST exist. I would like last path to be  
$uri and cause a 404, not a 500. Tried using error\_page to convert 500  
to 404, but did not have any effect. Thanks for any advice!

location ~ ^/img/[^.]\*$ {  
try\_files $uri.png $uri.jpg $uri.gif $uri;  
error\_page 500 =404 /img/missing;  
}

location /img/ {  
error\_page 404 /img/missing;  
}

location = /img/missing {  
rewrite .\* /img/icon/missing.png;  
}

Posted at Nginx Forum:

> **[How to get a 404 logged with try\_files?](https://forum.nginx.org/read.php?2,194965,194965#msg-194965)**
>
> Hi! Just started with Nginx after reading it reached 1.0. Congratulation! I'm trying to get my test site to return a "missing image" icon when an image is missing, but have a 404 logged so I know I messed up something. I want to specify the...

---

<div class="post-metadata">

**Author:** ![monster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ab992/32.png) [@monster](https://www.ruby-forum.com/u/monster)\
**Post date:** [May 2, 2011, 8:22pm UTC](https://www.ruby-forum.com/t/how-to-get-a-404-logged-with-try-files/206469/2 "2011-05-02T20:22:01Z")

</div>

Found it! It must look like this, putting =404 as last location in  
try\_files:

location ~ ^/img/[^.]\*$ {  
try\_files $uri.png $uri.jpg $uri.gif $uri =404;  
error\_page 404 /img/missing;  
}

That would be useful information to put in the try\_files wiki…

Posted at Nginx Forum:

> **[Re: How to get a 404 logged with try\_files?](https://forum.nginx.org/read.php?2,194965,195019#msg-195019)**
>
> Hi! Just started with Nginx after reading it reached 1.0. Congratulation! I'm trying to get my test site to return a "missing image" icon when an image is missing, but have a 404 logged so I know I messed up something. I want to specify the...

---

<div class="post-metadata">

**Author:** ![monster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ab992/32.png) [@monster](https://www.ruby-forum.com/u/monster)\
**Post date:** [May 3, 2011, 2:45pm UTC](https://www.ruby-forum.com/t/how-to-get-a-404-logged-with-try-files/206469/3 "2011-05-03T14:45:41Z")

</div>

On Mon, May 02, 2011 at 10:22:50AM -0400, monster wrote:

> ```
> rewrite .* /img/icon/missing.png;
> 
> ```
> 
> }

location /img/ {  
error\_page 404 /img/icon/missing.png;

```
location ~ ^/img/[^\.]*$ {
    try_files $uri.png $uri.jpg $uri.gif $uri =404;
}

```

}

location = /img/icon/missing.png {  
# nothing  
}

–  
Igor S.
