In our existing Apache setup, we have a .htaccess file in a folder which redirects all requests to a PHP handler. For example:
- www.example.com/gallery/user01/xyz.png
For this URL request:
- The "/user01" part symlinks to a "/imageDB" local folder.
- In the "/imageDB" folder, a .htaccess file redirects to a "handleImage.php" script which interprets the "/user01/xyz.png" part and returns an appropriate image.
The .htaccess is a simple 404 redirect:
ErrorDocument 404 handleImage.php
There are lots of "userXX" folders which symlink to the same "/imageDB" folder to be handled in the same way and because the .htaccess file resides there, it's picked up each time, e.g.
- www.example.com/gallery/user021/abc.png
- www.example.com/gallery/user508/def.png
BUT there are also lots of "userXX" folders which symlink to a different folder for cached images, "/imageCache", e.g.
- www.example.com/gallery/user921/ghi.png
- www.example.com/gallery/user882/jkl.png
These are straight image fetches, no PHP script involved.
We're now moving to a Nginx setup and I learn that Nginx doesn't permit .htaccess files (due to the performance hit each time they're read) and any redirect settings have to be done in the main Nginx config file and read on startup/reload.
I have a working Nginx config to an example folder as follows:-
location /gallery/user01/ {
fastcgi_intercept_errors on;
error_page 404 = /gallery/user01/handleImage.php;
}
...but as you can see, that just handles "user01" folder but we want to add lots of "userXX" folders that symlink to either the "/imageDB" or "/imageCache" folders.
It's as if I want to create a regular expression that detects the "/gallery/userXX" part of the incoming request, check if a "handleImage.php" file exists in the end folder and then call it, otherwise it's a direct cached image fetch.
Would that be the right way of implementing this? And any help with the regex would be appreciated, thanks.