Reputation: 5203
I have a server where I host multiple domain names. Depending on the domain name I'd like to serve files for a corresponding directory based on that domain name. For example:
www.domainA.com
serves files from /var/www/html/domainA.com
www.domainB.com
serves files from /var/www/html/domainB.com
etc. This way I do not have to edit my config file every time I add a site. In Apache I did it like this:
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{HTTP_HOST}} ^(www\.)?(.*)$
RewriteRule ^(.*) /var/www/html/%2/$1
How can I do the same thing in nginx?
Upvotes: 0
Views: 160
Reputation: 10314
Capture the domain name from the $host
variable using a map outside of your server block.
map $host $domain_name {
~(?<domain>[^.]+\.[^.]+)$ $domain;
}
Then add the new $domain_name
variable to your root statement. Note that $host
is normalized to lowercase, so your domain directories should also be lowercase.
root /var/www/html/$domain_name;
Make sure you've setup the server as the default.
Upvotes: 1