0

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:

  1. The "/user01" part symlinks to a "/imageDB" local folder.
  2. 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.

Jason
  • 121
  • 5

1 Answers1

0

I believe the correct configuration for your use case would be to set the location to /gallery/ and the error_page to /gallery/imageDB/handleImage.php.

This should intercept all requests, attempt to return the file requested, and if not found hit the handleImage script to return it.

You'll want to make sure the disable_symlinks directive is turned off for that to work -- I assume it is already for your example folder.

solocommand
  • 111
  • 3