0

This question follows on in many ways from a previous one I had asked - and ended up answering myself. A brief summary of the problem I describe there and my solution to it

  • I need to perform different default actions when there is an attempt to access files in a subfolder depending on who is attempting the access

aa

  1. A "back-office" web app in which case the file is delivered if it exists or a "stub" is generated and echoed back if it does not
  2. In response to requests from a phone app, OTH, I want to deliver the file, if it exists, or a HTTP 404 if it does not

The solution I worked out is as follows

  • Create a symlink to the folder in question ln -s /path/to/folder /path/to/bo_folder where the prefix bo_ on the symlink stands for back-office
  • When fetching files from the app just try to reach them directly as /path/to/folder/file.name.extn in which case Nginx will deal with sending back a 404 or the file, as it deems right
  • When fetching files for editing in the backoffice suite access them as /path/to/bo_folder/file.name.extn

The following Nginx configuration block

location /path/to/bo_folder
{
 add_header Access-Control-Allow-Origin *;
 rewrite ^(.*)$ /path/to/folder/index.php?$1 last;
}

The issue to which I am now trying to find a more graceful solution is this one - I don't just have one such folder but rather a whole sequence of them /path/to/folder_1, /path/to/folder_2... /path/to/folder_n

While I can put in location blocks for each such folder is possible it is tedious and error prone. There is almost certainly a way to write the rewrite more generically one folder level up, i.e. in /path/to rather than for each individual sub folder.

There is a minor complication here - I cannot handle this by having a rewrite to a /path/to/index.php folder since the stub content I need to send back is quite different depending on the subfolder in which that content is supposed to be.

My rather sketchy knowledge of Nginx configuration lets me down here. How can this be done?

DroidOS
  • 173
  • 7
  • why not just using sub domains? – djdomi Oct 18 '21 at 16:01
  • Could you elaborate? I am not sure I understand what you mean – DroidOS Oct 19 '21 at 04:58
  • my question is, does it has to be a specific path or can it be replaced by sub domains? – djdomi Oct 19 '21 at 12:41
  • Ah, yes - I see what you mean now. A sub domain would do just as well, e.g. backoffice.example.com. I would have to get a new Let's Ecrypt cert for it but that is fine. However, it is still not clear to me how I should write the nginx configuration blocks so attempts to get files from, say, `backoffice.example.com/folder/subfolder` get redirected to `backoffice.example.com/folder/subfolder/index.php` **WITHOUT** writing separate configuration blocks for each folder. – DroidOS Oct 20 '21 at 08:05
  • well, please update your question, its still to unspecific - maybe with an exact example - and remind, never write here such information update the question by edit it – djdomi Oct 20 '21 at 11:07

0 Answers0