There is a link of this type:

domain.com/rw/image/12/56/99/180/31255312/bvywefgt5w1gh2s5tr.jpg

and need to check if the file

domain.com/cache/12/56/99/31255312/bvywefgt5w1gh2s5tr. (jjpg|png )g

there is then show it, and if not then show it

domain.com/image.php?id=31255312&hash=bvywefgt5w1gh2s5tr

Note that the extension is still being checked. I mean, if there is no this file with jpg extension, then you need to check if there is a png or gif extension.

  • I know about this directive, but I could not apply it in this particular case. Can you show with an example? - FÉ™qan lÉ™bizadÉ™

1 answer 1

Use the try_files directive. Sort of

 location ~ ^/rw/image/(.+)/(\d+)/([a-z0-9]+)\.jpg$ { try_files /cache/$1/$2/$3.jpg /cache/$1/$2/$3.png /cache/$1/$2/$3.gif /image.php?id=$2&hash=$3 ; 
  • Only without =404 . And there still /180/ in the original request you need to remove. - Alexey Ten
  • 404 - this is the details, the vehicle knows better what to give, if everything is broken. 180 I considered a typo in the TS, and therefore offered not a copy-paste solution, but “something like”. - Sergiks
  • These are not details, this is a mistake. In this form, if the images are not found, the source code of the image.php file will be image.php . Because the internal redirect is done only on the last argument, and all the others are processed in the current location . - Alexey Ten
  • location blocks can contain nested blocks. Here I assume that the processing rules for *.php will be specified inside this location , as well as the rules for the static from the cache location ~* \.(jpg|png|gif)$ { expires 365d; } location ~* \.(jpg|png|gif)$ { expires 365d; } . - Sergiks
  • Will not help. nginx will not search for a new location for files specified in try_files - Alexey Ten