Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you dynamically set nginx root based on location?

Tags:

regex

nginx

I can't find any information on doing this specifically but I am basically trying to catch a location like:

http://domain.com/project/Content/Images/image.png

and I want it to point to root like so:

/var/www/$project/Content/Images/image.png

This is what I tried to put together but it doesnt seem to be working:

location ~ ^/(?<project>.+)/Content/^(?<content>.+)$ {
    root /var/www/$project/Content/$content;
}

I't doesn't seem to be catching this location as I get a 404 error which is setup with a php page I have with try_files in a location for /. This makes me think the regexp is wrong but I am not sure.

like image 630
DigitalFiz Avatar asked Sep 30 '22 04:09

DigitalFiz


1 Answers

I think you are really close. You have an extra ^ in your regex search string. ^ means "match from the beginning of the line"

location ~ ^/(?<project>.+)/Content/(?<content>.+)$ {
    root /var/www/$project/Content/$content;
}
like image 144
Venice Avatar answered Oct 04 '22 03:10

Venice