# Nginx mass dynamic virtual hosts like apache?

**URL:** <https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050>\
**Category:** NGINX\
**Created:** [November 19, 2011, 11:26pm UTC](https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050 "2011-11-19T23:26:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [November 19, 2011, 11:26pm UTC](https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050/1 "2011-11-19T23:26:18Z")

</div>

Hello

I’m new to nginx and was wondering if there is a configuration that is  
similar to apaches mass dynamic virtual hosts  
[http://httpd.apache.org/docs/2.0/vhosts/mass.html](http://httpd.apache.org/docs/2.0/vhosts/mass.html)

I’m having an SaaS developed where users will get a subdomain and would  
like to set this up as efficiently as possible. As of now i’m using  
apache but would like to move to nginx and get this setup

Thanks

Posted at Nginx Forum:

> **[Nginx mass dynamic virtual hosts like apache?](https://forum.nginx.org/read.php?2,218617,218617#msg-218617)**
>
> Hello I'm new to nginx and was wondering if there is a configuration that is similar to apaches mass dynamic virtual hosts http://httpd.apache.org/docs/2.0/vhosts/mass.html I'm having an SaaS developed where users will get a subdomain and would like...

---

<div class="post-metadata">

**Author:** ![anagio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anagio](https://www.ruby-forum.com/u/anagio)\
**Post date:** [November 19, 2011, 11:54pm UTC](https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050/2 "2011-11-19T23:54:42Z")

</div>

On Sun, Nov 20, 2011 at 12:25 AM, anagio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello
> 
> I’m new to nginx and was wondering if there is a configuration that is  
> similar to apaches mass dynamic virtual hosts  
> [Dynamically Configured Mass Virtual Hosting - Apache HTTP Server](http://httpd.apache.org/docs/2.0/vhosts/mass.html)
> 
> I’m having an SaaS developed where users will get a subdomain and would  
> like to set this up as efficiently as possible. As of now i’m using  
> apache but would like to move to nginx and get this setup

Well, the idea is to set your root dynamically from the default  
server block depending on the ‘host’ header.

You can either use $http\_host as a part of your root, which  
is probably not a good thing, or map it to some subdir, and  
use its value instead:

```
map $http_host $subdir {
    hostnames;
    default "default";
    .foo.bar.com "foo";
    .baz.bar.com "baz";
}

server {
    root /path/to/$subdir;

}

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![anagio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anagio](https://www.ruby-forum.com/u/anagio)\
**Post date:** [November 20, 2011, 1:18am UTC](https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050/3 "2011-11-20T01:18:42Z")

</div>

Hi,

In your example does that mean I would need to add entries for each  
sub-domain as needed. If that’s the case, wouldn’t I have to restart  
nginx each time a new entry is made? I plan to have sub-domains created  
when users register. Will I have to restart?

Posted at Nginx Forum:

> **[Re: Nginx mass dynamic virtual hosts like apache?](https://forum.nginx.org/read.php?2,218617,218620#msg-218620)**
>
> Hello I'm new to nginx and was wondering if there is a configuration that is similar to apaches mass dynamic virtual hosts http://httpd.apache.org/docs/2.0/vhosts/mass.html I'm having an SaaS developed where users will get a subdomain and would like...

---

<div class="post-metadata">

**Author:** ![anagio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anagio](https://www.ruby-forum.com/u/anagio)\
**Post date:** [November 20, 2011, 1:42am UTC](https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050/4 "2011-11-20T01:42:17Z")

</div>

Thanks now if I wanted to let a member who has a sub-domain on our  
domain, “[user.domain.com](http://user.domain.com)” map one of their own domains or sub-domains  
using a cname. I would need to create a ServerAlias for their  
sub-domain, in apache2 it would be something like below. So using the  
regex probably wouldn’t be the best method, so just adding them as you  
listed in the first reply is how to go about it?

ServerName [user.domain.com](http://user.domain.com)  
ServerAlias \*.user.domain.com  
ServerAlias [usersowndomain.com](http://usersowndomain.com)

Posted at Nginx Forum:

> **[Re: Nginx mass dynamic virtual hosts like apache?](https://forum.nginx.org/read.php?2,218617,218622#msg-218622)**
>
> Hello I'm new to nginx and was wondering if there is a configuration that is similar to apaches mass dynamic virtual hosts http://httpd.apache.org/docs/2.0/vhosts/mass.html I'm having an SaaS developed where users will get a subdomain and would like...

---

<div class="post-metadata">

**Author:** ![anagio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anagio](https://www.ruby-forum.com/u/anagio)\
**Post date:** [November 20, 2011, 1:33am UTC](https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050/5 "2011-11-20T01:33:38Z")

</div>

On Sun, Nov 20, 2011 at 2:17 AM, anagio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> In your example does that mean I would need to add entries for each  
> sub-domain as needed. If that’s the case, wouldn’t I have to restart  
> nginx each time a new entry is made? I plan to have sub-domains created  
> when users register. Will I have to restart?

No, just reload, which is safe.

But if that’s a problem, you can extract subdir with regex  
in the same map, something like this:

```
~^(?<sub>[a-zA-Z0-9-]+)\.bar\.com$ $sub;

```

---

<div class="post-metadata">

**Author:** ![anagio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anagio](https://www.ruby-forum.com/u/anagio)\
**Post date:** [November 20, 2011, 1:58am UTC](https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050/6 "2011-11-20T01:58:20Z")

</div>

On Sun, Nov 20, 2011 at 2:41 AM, anagio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks now if I wanted to let a member who has a sub-domain on our  
> domain, “[user.domain.com](http://user.domain.com)” map one of their own domains or sub-domains  
> using a cname. I would need to create a ServerAlias for their  
> sub-domain, in apache2 it would be something like below. So using the  
> regex probably wouldn’t be the best method, so just adding them as you  
> listed in the first reply is how to go about it?

Using those methods together is fine too. You can go either or both  
ways.

---

<div class="post-metadata">

**Author:** ![anagio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anagio](https://www.ruby-forum.com/u/anagio)\
**Post date:** [November 20, 2011, 10:43am UTC](https://www.ruby-forum.com/t/nginx-mass-dynamic-virtual-hosts-like-apache/214050/7 "2011-11-20T10:43:11Z")

</div>

Given I use this method for sub-domains

map $http\_host $subdir {  
hostnames;  
default “default”;  
.foo.bar.com “foo”;  
.baz.bar.com “baz”;  
}

Is there a method to redirect any requests for sub-domains which do not  
exist (404s) to a custom html/php page?

Posted at Nginx Forum:

> **[Re: Nginx mass dynamic virtual hosts like apache?](https://forum.nginx.org/read.php?2,218617,218634#msg-218634)**
>
> Hello I'm new to nginx and was wondering if there is a configuration that is similar to apaches mass dynamic virtual hosts http://httpd.apache.org/docs/2.0/vhosts/mass.html I'm having an SaaS developed where users will get a subdomain and would like...
