# Nginx rewrite for cached images

**URL:** <https://www.ruby-forum.com/t/nginx-rewrite-for-cached-images/219480>\
**Category:** NGINX\
**Created:** [May 4, 2012, 9:19am UTC](https://www.ruby-forum.com/t/nginx-rewrite-for-cached-images/219480 "2012-05-04T09:19:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 4, 2012, 9:19am UTC](https://www.ruby-forum.com/t/nginx-rewrite-for-cached-images/219480/1 "2012-05-04T09:19:40Z")

</div>

Hi there,

we are currently migratiing all services from apache2 to nginx but are  
currently stuck with a specific rewrite rule.  
The rule in apache is checking if a cached image/thumbnail exists and if  
so, delivers the output directly.  
otherwise it will rewrite the request to an image-processor.

```
# if cached version exists, output directly
RewriteCond %{QUERY_STRING} ^(Guest|Member|avatar|tagged|thumb)$
RewriteCond %{DOCUMENT_ROOT}/cache/$1/%{QUERY_STRING}.jpg -f
RewriteRule (.*)\.jpg$ /cache/$1/%{QUERY_STRING}.jpg [L]

# otherwise redirect all jpg-image-requests to processing
# script if they are not in cache dir
RewriteCond %{REQUEST_URI} !^/cache
RewriteRule \.jpg$ /image_processing.php [QSA,L]

```

we tried several try\_files approaches but we failed so far.  
could someone please help in migrating this rewrite rules to nginx?

Thank you very much!

–

Andreas Lehr
