Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx get subdomain of the url and assing the document root

Tags:

regex

php

nginx

Im no expert on nginx but i got my problem solved by doing this:

server_name ~^(?<vhost>.*)$;
root /var/www/$vhost/public;

For example if a user types in a web browser http://sysadmin.mydomain.com nginx will use the doc root of /var/www/sysadmin.mydomain.com/public

And this works fine, but in doing so I have created another problem with PHP $_SERVER['SERVER_NAME']; and its that this will print: ~^(?.*)$ causing problems somewhere else in my code.

My solution works for nginx but its not the right solution. How can i achieve this and still retain a 'readable' server name?

like image 964
ray sn0w Avatar asked Dec 06 '25 21:12

ray sn0w


1 Answers

This does the trick for what I want:

server_name mydomain.com ~^(?<vhost>.+)\.mydomain\.com$;
root /var/www/$vhost.mydomain.com/public;

$_SERVER['SERVER_NAME'] will answer: mydomain.com and thats fine for what I want.

like image 108
ray sn0w Avatar answered Dec 09 '25 12:12

ray sn0w



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!