Score:0

Nginx configure location rewrite for all subfolders

cn flag

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?

djdomi avatar
za flag
why not just using sub domains?
cn flag
Could you elaborate? I am not sure I understand what you mean
djdomi avatar
za flag
my question is, does it has to be a specific path or can it be replaced by sub domains?
cn flag
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.
djdomi avatar
za flag
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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.