Nginx mass dynamic virtual hosts like apache?

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 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:

On Sun, Nov 20, 2011 at 12:25 AM, anagio [email protected] 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

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.

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:

Thanks now if I wanted to let a member who has a sub-domain on our
domain, “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
ServerAlias *.user.domain.com
ServerAlias usersowndomain.com

Posted at Nginx Forum:

On Sun, Nov 20, 2011 at 2:17 AM, anagio [email protected] 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;

On Sun, Nov 20, 2011 at 2:41 AM, anagio [email protected] wrote:

Thanks now if I wanted to let a member who has a sub-domain on our
domain, “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.

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: