# Dynamic Subdomain Configuration

**URL:** <https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062>\
**Category:** NGINX\
**Created:** [February 20, 2012, 8:29pm UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062 "2012-02-20T20:29:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 20, 2012, 8:29pm UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/1 "2012-02-20T20:29:10Z")

</div>

Hello,

We provide a subdomain for each user, for example:

```
 paul.ourdomain.com
 jay.ourdomain.com
 bob.ourdomain.com
 xxxxx.ourdomain.com

```

Currently, I am doing this manually by adding another config in  
/etc/nginx/conf.d for each subdomain. A typical conf looks like:

server {  
listen 80;

server\_name [paul.ourdomain.com](http://paul.ourdomain.com);

root /srv/www/users/paul/wp;

index index.php;

access\_log /var/log/nginx/vhosts/paul.access.log;  
error\_log /var/log/nginx/vhosts/paul.error.log;

include /etc/nginx/excludes.conf;  
include /etc/nginx/wordpress.conf;  
include /etc/nginx/expires.conf;  
}

Is there I way I can abstract this, and prevent creating a configuration  
for each subdomain? I was reading something about map, but don’t fully  
understand it.

Thanks.

Posted at Nginx Forum:

> **[Dynamic Subdomain Configuration](https://forum.nginx.org/read.php?2,222733,222733#msg-222733)**
>
> Hello, We provide a subdomain for each user, for example: paul.ourdomain.com jay.ourdomain.com bob.ourdomain.com xxxxx.ourdomain.com Currently, I am doing this manually by adding another config in /etc/nginx/conf.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 20, 2012, 8:40pm UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/2 "2012-02-20T20:40:55Z")

</div>

On Tue, Feb 21, 2012 at 2:28 AM, justin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> /etc/nginx/conf.d for each subdomain. A typical conf looks like:  
> access\_log /var/log/nginx/vhosts/paul.access.log;

Probably something like this:

map $host $username {  
[some.domain.com](http://some.domain.com) usera;  
[another.one.net](http://another.one.net) userb;  
}

server {  
root /srv/www/users/$username/wp;  
access\_log /var/log/nginx/vhosts/$username.access.log;  
…  
}

Or this (without map):

server {  
server\_name ~^(?.+).domain.com$;  
root /srv/www/users/$username/wp;  
…  
}

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 20, 2012, 8:56pm UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/3 "2012-02-20T20:56:24Z")

</div>

Edho,

Thank you very much for the assistance. Here is what I used, and seems  
to be working perfectly:

server {  
listen 80;

server\_name ~^(?.+).mydomain.com$;

root /srv/www/users/$user/wp;

index index.php;

access\_log /var/log/nginx/vhosts/$user.access.log;  
error\_log /var/log/nginx/vhosts/$user.error.log;

include /etc/nginx/excludes.conf;  
include /etc/nginx/wordpress.conf;  
include /etc/nginx/expires.conf;  
}

Posted at Nginx Forum:

> **[Re: Dynamic Subdomain Configuration](https://forum.nginx.org/read.php?2,222733,222740#msg-222740)**
>
> Hello, We provide a subdomain for each user, for example: paul.ourdomain.com jay.ourdomain.com bob.ourdomain.com xxxxx.ourdomain.com Currently, I am doing this manually by adding another config in /etc/nginx/conf.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 20, 2012, 10:15pm UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/4 "2012-02-20T22:15:51Z")

</div>

Hello!

On Mon, Feb 20, 2012 at 02:55:39PM -0500, justin wrote:

> Edho,
> 
> Thank you very much for the assistance. Here is what I used, and seems  
> to be working perfectly:

[…]

> access\_log /var/log/nginx/vhosts/$user.access.log;  
> error\_log /var/log/nginx/vhosts/$user.error.log;

Note: the “error\_log” directive doesn’t support variables, and  
this will log all errors into “$user.error.log” file.

Maxim D.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 21, 2012, 1:19am UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/5 "2012-02-21T01:19:02Z")

</div>

Actually, I think I found how to set the 404:

location ~ .php$ {  
if (!-f $document\_root/$fastcgi\_script\_name) {  
log\_not\_found off;  
return 404;  
}

include /etc/nginx/fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_intercept\_errors off;  
fastcgi\_pass [php1.local.pagelines.com:9000](http://php1.local.pagelines.com:9000);  
}

But I can’t use log\_not\_found off, getting an error about the ability to  
use this in the location. Basically, I don’t want an error logged if  
somebody types:

[foobar.mydomain.com](http://foobar.mydomain.com)

Posted at Nginx Forum:

> **[Re: Dynamic Subdomain Configuration](https://forum.nginx.org/read.php?2,222733,222747#msg-222747)**
>
> Hello, We provide a subdomain for each user, for example: paul.ourdomain.com jay.ourdomain.com bob.ourdomain.com xxxxx.ourdomain.com Currently, I am doing this manually by adding another config in /etc/nginx/conf.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 21, 2012, 2:11am UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/6 "2012-02-21T02:11:39Z")

</div>

On Tue, Feb 21, 2012 at 7:18 AM, justin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> fastcgi\_intercept\_errors off;  
> fastcgi\_pass php1.local.pagelines.com:9000;  
> }
> 
> But I can’t use log\_not\_found off, getting an error about the ability to  
> use this in the location. Basically, I don’t want an error logged if  
> somebody types:

Try

try\_files $uri =404;

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 21, 2012, 12:45am UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/7 "2012-02-21T00:45:31Z")

</div>

How do I handle the fallback, i.e. somebody types:

```
foobar.mydomain.com

```

Which does not exists, right now I am getting: no input file. Instead,  
would love to simply return a 404 error, or even better serve a custom  
static 404 error page.

Maxim:

Bummer that error\_log does not support variables, anyway to do this  
dynamically?

Posted at Nginx Forum:

> **[Re: Dynamic Subdomain Configuration](https://forum.nginx.org/read.php?2,222733,222746#msg-222746)**
>
> Hello, We provide a subdomain for each user, for example: paul.ourdomain.com jay.ourdomain.com bob.ourdomain.com xxxxx.ourdomain.com Currently, I am doing this manually by adding another config in /etc/nginx/conf.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 21, 2012, 2:33am UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/8 "2012-02-21T02:33:15Z")

</div>

On Tue, Feb 21, 2012 at 8:28 AM, justin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is for Wordpress, I already have a try\_files. I want to log not  
> found everywhere else, just not in the dynamic subdomain

You can (should) have another try\_files in location ~ .php$

> location / {  
> try\_files $uri $uri/ /index.php?$args;  
> }
> 
> location ~ .php$ {  
> try\_files $uri =404;  
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 21, 2012, 2:40am UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/9 "2012-02-21T02:40:40Z")

</div>

Edho,

Still seeing logged:

2012/02/20 17:37:18 [error] 12840#0: \*27 testing “/srv/www/users/boo/wp”  
existence failed (2: No such file or directory) while logging request

With:

location ~ .php$ {  
try\_files $uri =404;  
}

Posted at Nginx Forum:

> **[Re: Dynamic Subdomain Configuration](https://forum.nginx.org/read.php?2,222733,222752#msg-222752)**
>
> Hello, We provide a subdomain for each user, for example: paul.ourdomain.com jay.ourdomain.com bob.ourdomain.com xxxxx.ourdomain.com Currently, I am doing this manually by adding another config in /etc/nginx/conf.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 21, 2012, 2:28am UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/10 "2012-02-21T02:28:18Z")

</div>

This is for Wordpress, I already have a try\_files. I want to log not  
found everywhere else, just not in the dynamic subdomain

location / {  
try\_files $uri $uri/ /index.php?$args;  
}

location ~ .php$ {  
if (!-f $document\_root/$fastcgi\_script\_name) {  
log\_not\_found off;  
return 404;  
}

include /etc/nginx/fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_intercept\_errors off;  
fastcgi\_pass [php1.local.pagelines.com:9000](http://php1.local.pagelines.com:9000);  
}

Posted at Nginx Forum:

> **[Re: Dynamic Subdomain Configuration](https://forum.nginx.org/read.php?2,222733,222750#msg-222750)**
>
> Hello, We provide a subdomain for each user, for example: paul.ourdomain.com jay.ourdomain.com bob.ourdomain.com xxxxx.ourdomain.com Currently, I am doing this manually by adding another config in /etc/nginx/conf.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 21, 2012, 7:46pm UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/11 "2012-02-21T19:46:41Z")

</div>

Still trying to track down what is causing the following to be logged in  
the error log:

2012/02/21 10:42:13 [error] 13884#0: \*19 testing  
“/srv/www/users/foobar/wp” existence failed (2: No such file or  
directory) while logging request, client: X.X.X.X, server:  
~^(?.+).mydomain.com$, request: “GET / HTTP/1.1”, host:  
“[foobar.pagelines.com](http://foobar.pagelines.com)”

I believe the problem is since I am configuring the server\_name and root  
dynamically:

server\_name ~^(?.+).mydomain.com$;  
root /srv/www/users/$user/wp;

And obviously the root directory does not exist for foobar. Is there a  
way to suppress these errors, or maybe check if the root directory  
exists?

Posted at Nginx Forum:

> **[Re: Dynamic Subdomain Configuration](https://forum.nginx.org/read.php?2,222733,222800#msg-222800)**
>
> Hello, We provide a subdomain for each user, for example: paul.ourdomain.com jay.ourdomain.com bob.ourdomain.com xxxxx.ourdomain.com Currently, I am doing this manually by adding another config in /etc/nginx/conf.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 21, 2012, 2:53am UTC](https://www.ruby-forum.com/t/dynamic-subdomain-configuration/217062/12 "2012-02-21T02:53:01Z")

</div>

On Tue, Feb 21, 2012 at 8:40 AM, justin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Edho,
> 
> Still seeing logged:
> 
> 2012/02/20 17:37:18 [error] 12840#0: \*27 testing “/srv/www/users/boo/wp”  
> existence failed (2: No such file or directory) while logging request

The errors seems like coming from root directive. I don’t know how to  
prevent logging that, sorry. Anyway, the try\_files in location php  
block should allow you to use custom static 404 error page.

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)
