How to serve subdomain from subfolder of the domain root?

Francis Daly francis at daoine.org
Sat Sep 14 08:57:56 UTC 2013


On Sat, Sep 14, 2013 at 01:42:49AM -0400, etrader wrote:

Hi there,

> server {
> server_name domain.com *.domain.com
> root  /var/www/$server_name;
> }

http://nginx.org/r/server_name

Look for "Named captures".

> is it possible to set locations for subdomains based on subfolders of the
> $server_name ?
> 
> location matching sub1.domain.com {
> serving from /var/www/$server_name/sub1
> }

"location" doesn't match the hostname used. You could use a "map" to
enumerate the hostnames you consider valid, with a default value for
anything else; and use that in "root".

> Currently, I am using a server for each subdomain, but when the number of
> subdomains increases, maintaining numerous servers becomes messy.

If *every* valid hostname is handled equivalently, you could get away
with it being all in one server block.

But you may want to look into "include", or using an external-to-nginx
template system to turn your starting system into an nginx.conf with
multiple server blocks, if different hostnames are handled differently.

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list