location /image_mini {
#  try_files /static_images/$2.jpg;
  rewrite ^/image_mini/([^.]+)-00([0-9]+)\.jpg$ /image.php?a=$2 last;

I use this rewrite rule to display images on my website. For example, if I want to display Image ID #123456789, I would use https://www.example.com/image_mini/some_cool_seo_keywords-00123456789.jpg

Then, image.php will check if /static_images/00123456789.jpg is found in local cache, if not it will create the file.

I assume I would get much better performance if nginx could look for the local static file before fallback to image.php

I tried doing this with try_files but I am unsure how to extract the image ID from the URL and i'm stuck here :( Any help would be appreciated


You can use your regular expression in the location statement. For example:

location ~* ^/image_mini/[^.]+-00([0-9]+)\.jpg$ {
    try_files /static_images/$1.jpg /image.php?a=$1;

Note that the evaluation of regular expression location statements is ordered. See this document for more. Also, this document on the correct syntax for try_files.

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.