Forum: NGINX How to serve subdomain from subfolder of the domain root?

2974d09ac2541e892966b762aad84943?d=identicon&s=25 etrader (Guest)
on 2013-09-14 07:43
(Received via mailing list)
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
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2013-09-14 10:58
(Received via mailing list)
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@daoine.org
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.