# Try\_files and filter modules

**URL:** <https://www.ruby-forum.com/t/try-files-and-filter-modules/216754>\
**Category:** NGINX\
**Created:** [February 13, 2012, 1:47pm UTC](https://www.ruby-forum.com/t/try-files-and-filter-modules/216754 "2012-02-13T13:47:30Z")\
**Posts on this page:** 4\
**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:** [February 13, 2012, 1:47pm UTC](https://www.ruby-forum.com/t/try-files-and-filter-modules/216754/1 "2012-02-13T13:47:30Z")

</div>

Hello,  
is there a reason (or workaround) why try\_files doesn’t work with filter  
modules (in my case its image\_filter)?

For example:

location ~ (._)/small\_(._) {  
try\_files $uri $1/medium\_$2 $1/large\_$2;  
image\_filter crop 175 175;  
}

The crop filter is never applied.

The idea is to generate the thumbnail in a chain fashion - first detect  
if there is already an image in needed size/name then try to  
resize it from medium sized version and if it doesn’t exist use the  
original source file.

I can rewrite it to something like:

location ~ /small\_ {  
try\_files $uri @smallresize;  
}

location @smallresize {  
internal;  
rewrite (._)/small\_(._) $1/large\_$2 break;  
image\_filter crop 175 175;  
}

But obviously it misses the “medium” step. Also wouldn’t like to use  
multiple 'if’s.

p.s. it also seems that try\_files doesn’t work with more than one @named  
location - every except the last one is checked ( stat() )  
as physical files.

rr

---

<div class="post-metadata">

**Author:** ![Reinis\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@Reinis\_R](https://www.ruby-forum.com/u/Reinis_R)\
**Post date:** [February 13, 2012, 2:38pm UTC](https://www.ruby-forum.com/t/try-files-and-filter-modules/216754/2 "2012-02-13T14:38:38Z")

</div>

On Monday 13 February 2012 16:46:55 Reinis R. wrote:

> }
> 
> The crop filter is never applied.  
> […]

Probably, because $uri and $1/medium\_$2 doesn’t exist or  
smaller than your crop.

location ~ (._)/small\_(._) {  
try\_files $uri $1/medium\_$2 $1/large\_$2 =404;  
image\_filter crop 175 175;  
}

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Reinis\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@Reinis\_R](https://www.ruby-forum.com/u/Reinis_R)\
**Post date:** [February 13, 2012, 2:56pm UTC](https://www.ruby-forum.com/t/try-files-and-filter-modules/216754/3 "2012-02-13T14:56:06Z")

</div>

> ```
> try_files $uri $1/medium_$2 $1/large_$2 =404;
> 
> ```

Hmm, thx the ‘= 404’ is all that I missed.

p.s. the files indeed didn’t exist (expected) but the request always  
returned the source/original file (without cropping it), but  
now with the additional 404 it works nicely.

rr

---

<div class="post-metadata">

**Author:** ![Reinis\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@Reinis\_R](https://www.ruby-forum.com/u/Reinis_R)\
**Post date:** [February 13, 2012, 3:07pm UTC](https://www.ruby-forum.com/t/try-files-and-filter-modules/216754/4 "2012-02-13T15:07:56Z")

</div>

On Monday 13 February 2012 17:55:35 Reinis R. wrote:  
[…]

> p.s. the files indeed didn’t exist (expected) but the request always  
> returned the source/original file (without cropping it), but now with the  
> additional 404 it works nicely.

It was handled by another location. The last part in the try\_files  
directive  
always does internal redirect (to $1/large\_$2 in your case, so then it  
didn’t  
catch by “location ~ (._)/small\_(._)”).

wbr, Valentin V. Bartenev
