Firstly I want to state that I'm rather new to nginx, I basically only know what I've learned over the last week.
That said, I currently have a Nginx server with a standard configuration of:
server {
listen 80;
server_name site.com;
root /var/www/site.com/;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location /microsite/first {
try_files $uri $uri/ /microsite/first/;
}
location /microsite/second {
try_files $uri $uri/ /microsite/second/;
}
...
}
This works fine, although for every microsite I add to the existing ones, it requires that a new location be added referring to the path of the new microsite.
My question is: is it possible to dynamically set the location
parameter in a way that it catches and references whatever sub-directory exists within the microsite/ directory?
e.g. something along the line of the rewrite rule rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
(taken from the nginx site) but applied to the location
parameter, like:
location ~ ^/microsite/(.*)$ {
try_files $uri $uri/ /microsite/$1/;
}
In which the $1
would catch the sub-directory name passed in (.*)
?
(I tried this snippet that I built refering to the answer to (another) Nginx dynamic location configuration question, although it did not work)
Also I'm not a Regex expert, I've tweaked a bit with it in the past but it was a while ago and I don't recall the precise terminology, so that may be part of the problem, perhaps?!
Anyway, all help is appreciated.
Thanks in advance!!
You probably need to limit the capture to the first two path segments:
location ~ ^(/microsite/[^/]+) {
try_files $uri $uri/ $1/;
}
The [^/]
character class matches anything that is not a /