I'm trying to save some time on my development machine by not creating a new nginx server config for each new site I test. I'm wondering if there is a way for nginx to change the root directory based on the domain I'm loading.
So if I have
mysite.loc
myothersite.loc
something.loc
Nginx will look inside
www/mysite.loc
www/myothersite.loc
www/something.loc
That would allow me to test new things on mock domains just a little bit faster since I don't have to mess with a new /etc/nginx/sites-available config for each one.
server {
listen 80;
server_name *.loc;
root /www/$host;
index index.html;
}
Is this possible? Is there another way to do it?
The nginx configuration has something that can help you:
I think that it is exactly what you want right? Search for named captures to get to this part of the documentation, but I urge you to read all description of
server_name
.you could also do
Not sure if this is a good idea but I have tested it and it works.
As root directive is not allowed in if condition. If you have complicated judgement, please refer to this solution.