nginx rewrite for cached images

Andreas Lehr mail at
Fri May 4 07:18:53 UTC 2012

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list