How to serve subdomain from subfolder of the domain root?

In a server as

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

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
}

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

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,242841,242841#msg-242841

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 D. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs