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 porting this rewrite rules to nginx?

Thank you very much!


This would be a whole lot easier if you showed us what you'd already tried, but off the top of my head, something like this should do the trick (assuming you've got a vhost-wide root parameter set; if not, you'll want to fix that anyway):

location ~ ^.*/.*\.jpg$ {
    try_files $uri @image_processor;

location @image_processor {
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $documentroot/image_processing.php;
    fastcgi_pass unix:/home/user/something/php.sock;

I have no idea what you're trying to achieve in that first block; I can't comprehend why you'd use the query string like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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