# How to serve subdomain from subfolder of the domain root?

**URL:** <https://www.ruby-forum.com/t/how-to-serve-subdomain-from-subfolder-of-the-domain-root/236551>\
**Category:** NGINX\
**Created:** [September 14, 2013, 7:43am UTC](https://www.ruby-forum.com/t/how-to-serve-subdomain-from-subfolder-of-the-domain-root/236551 "2013-09-14T07:43:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 14, 2013, 7:43am UTC](https://www.ruby-forum.com/t/how-to-serve-subdomain-from-subfolder-of-the-domain-root/236551/1 "2013-09-14T07:43:18Z")

</div>

In a server as

server {  
server\_name [domain.com](http://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](http://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:

> **[How to serve subdomain from subfolder of the domain root?](https://forum.nginx.org/read.php?2,242841,242841#msg-242841)**
>
> 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 fro

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [September 14, 2013, 10:58am UTC](https://www.ruby-forum.com/t/how-to-serve-subdomain-from-subfolder-of-the-domain-root/236551/2 "2013-09-14T10:58:26Z")

</div>

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

Hi there,

> server {  
> server\_name [domain.com](http://domain.com) \*.domain.com  
> root /var/www/$server\_name;  
> }

[http://nginx.org/r/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](http://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. removed\_email\_address@domain.invalid
