# RegEx VHost name and the default VHost

**URL:** <https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692>\
**Category:** NGINX\
**Created:** [January 23, 2013, 7:37pm UTC](https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692 "2013-01-23T19:37:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [January 23, 2013, 7:37pm UTC](https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692/1 "2013-01-23T19:37:54Z")

</div>

Hello!

VHost names with RegEx is an absolutely imazing feature of nginx. I  
llllllllllllllove it! 🙂 But now I’ve got an issue with it.

On my VM I have a VHost templates ax-common-vhost and use it for VHsots  
like  
project.area.loc, so the server\_name rule is:

server\_name ~^(._).(._).loc$;

It works fine and has already saved much time for me. But the default  
VHost  
seems to get a trouble with my template. When a site, that uses it, is  
active, the default VHost also is trieng to use the template.

Example:

/etc/nginx/sites-available:

> default  
> – root /usr/share/nginx/html  
> – server\_name localhost;  
> test.sandbox.loc  
> – include /etc/nginx/sites-available/ax-common-vhost;  
> ax-common-vhost  
> – server\_name ~^(._).(._).loc$;  
> – if ($host ~ ^(._).(._).loc$) {  
> set $project $1;  
> set $area $2;

```
    set $folder "$area/$project";
    set $domain "$project.$area.loc";
}

```

– root /var/www/$folder/;  
– test.sandbox.loc (based on the ax-common-vhost)

/etc/nginx/sites-enabled:

> default  
> test.sandbox.loc

By access to the server default VHost (over IP of the VM) an error  
occures:

2013/01/23 18:41:19 [error] 4051#0: _1 directory index of “/var/www//”  
is  
forbidden, client: 192.168.56.1, server: ~^(._).(.\*).loc$, request:  
“GET /  
HTTP/1.1”, host: “192.168.56.101”

When I change the server root rule in the template e.g. to /var/www/ and  
place a test file (index.html) into my webroot folder, it is displayed.

That means: Nginx uses my template for the default host. But  
“192.168.56.101” cannot be mached to “^(._).(._).loc$”! Is it a bug?

Posted at Nginx Forum:

> **[RegEx VHost name and the default VHost](https://forum.nginx.org/read.php?2,235490,235490#msg-235490)**
>
> Hello! VHost names with RegEx is an absolutely imazing feature of nginx. I llllllllllllllove it! :) But now I've got an issue with it. On my VM I have a VHost templates ax-common-vhost and use it for VHsots like project.area.loc, so the server\_name...

---

<div class="post-metadata">

**Author:** ![automatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@automatix](https://www.ruby-forum.com/u/automatix)\
**Post date:** [January 23, 2013, 8:17pm UTC](https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692/2 "2013-01-23T20:17:40Z")

</div>

On Wednesday 23 January 2013 22:37:08 automatix wrote:

> It works fine and has already saved much time for me. But the default VHost
> 
> > test.sandbox.loc
> 
> – include /etc/nginx/sites-available/ax-common-vhost;
> 
> > ax-common-vhost
> 
> – server\_name ~^(._).(._).loc$;  
> – if ($host ~ ^(._).(._).loc$) {  
> set $project $1;  
> set $area $2;

This is ugly equivalent to: server\_name  
~^(?.+).(?.+).loc$;

> ```
> set $folder "$area/$project";
> set $domain "$project.$area.loc";
> 
> ```

And the $domain variable is effectively equal to $host. What’s the  
point?

> ```
> }
> 
> ```
> 
> – root /var/www/$folder/;

root /var/www/$area/$project;

> / HTTP/1.1", host: “192.168.56.101”
> 
> When I change the server root rule in the template e.g. to /var/www/ and  
> place a test file (index.html) into my webroot folder, it is displayed.
> 
> That means: Nginx uses my template for the default host. But  
> “192.168.56.101” cannot be mached to “^(._).(._).loc$”! Is it a bug?

Are you sure that your “default” is actually the default server  
configuration  
for the listening address:port?

wbr, Valentin V. Bartenev

–

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![automatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@automatix](https://www.ruby-forum.com/u/automatix)\
**Post date:** [January 23, 2013, 8:42pm UTC](https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692/3 "2013-01-23T20:42:14Z")

</div>

Thank you for your reply!

> server\_name ~^(?.+).(?.+).loc$;  
> Yes, you are right, it’s the better way to extract values from a RegEx  
> into  
> vars.

> Are you sure that your “default” is actually the default server  
> configuration for the listening address:port?  
> No, I’m not. How can I check it?

Ilya

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,235490,235497#msg-235497](http://forum.nginx.org/read.php?2,235490,235497#msg-235497)

---

<div class="post-metadata">

**Author:** ![automatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@automatix](https://www.ruby-forum.com/u/automatix)\
**Post date:** [January 23, 2013, 9:22pm UTC](https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692/4 "2013-01-23T21:22:32Z")

</div>

On Wednesday 23 January 2013 23:41:36 automatix wrote:

> No, I’m not. How can I check it?

You should check the listen directive.

Please see this article:

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html)**

and also the documentation: [Module ngx\_http\_core\_module](http://nginx.org/r/listen)

wbr, Valentin V. Bartenev

–

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![automatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@automatix](https://www.ruby-forum.com/u/automatix)\
**Post date:** [January 23, 2013, 11:31pm UTC](https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692/5 "2013-01-23T23:31:06Z")

</div>

On Thursday 24 January 2013 00:54:22 automatix wrote:

> stored in different files?
> 
> In which order are the vhost files processed?

Actually there is no such thing like “the vhost files” in nginx.  
You probably mean those files included from nginx.conf by the  
“include” directive (see: [Core functionality](http://nginx.org/r/include) ).

Before nginx 1.3.10 the order was arbitrary. Since version  
1.3.10 they sorted alphabetically on unix systems.

Please note, such directories like “sites-enabled” and  
“sites-available” are not something common for nginx.  
In fact, they created by nginx package on some linux systems  
because the maintainers of these packages find it convenient.

wbr, Valentin V. Bartenev

–

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![automatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@automatix](https://www.ruby-forum.com/u/automatix)\
**Post date:** [January 23, 2013, 9:54pm UTC](https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692/6 "2013-01-23T21:54:42Z")

</div>

Thank you very much for the usefull links and the tip. Yes, the vhost  
default was not default. Now I’ve set it to default explicitly with the  
flaf  
default\_server and everything works fine!

Thank you, Valentin!

Since “default server is the first one”  
([How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html)), the problem  
must  
have been, that the server couldn’t find a vhost for the request and  
just  
took the first vhost. But what ist “the first” vhost, when all vhosts  
are  
stored in different files? In which order are the vhost files processed?

Posted at Nginx Forum:

> **[Re: RegEx VHost name and the default VHost](https://forum.nginx.org/read.php?2,235490,235505#msg-235505)**
>
> Hello! VHost names with RegEx is an absolutely imazing feature of nginx. I llllllllllllllove it! :) But now I've got an issue with it. On my VM I have a VHost templates ax-common-vhost and use it for VHsots like project.area.loc, so the server\_name...

---

<div class="post-metadata">

**Author:** ![automatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@automatix](https://www.ruby-forum.com/u/automatix)\
**Post date:** [January 24, 2013, 12:06am UTC](https://www.ruby-forum.com/t/regex-vhost-name-and-the-default-vhost/225692/7 "2013-01-24T00:06:09Z")

</div>

> such directories like “sites-enabled” and  
> “sites-available” are not something common for nginx.  
> In fact, they created by nginx package on some linux systems  
> because the maintainers of these packages find it convenient.

Good to know, thanks for the info!

> Actually there is no such thing like “the vhost files” in nginx.  
> You probably mean those files included from nginx.conf by the  
> “include” directive (see: [Core functionality](http://nginx.org/r/include) ).

I mean the files in “sites-available”. I know, that they are just  
included  
(over the links in “sites-enabled”) to the nginx.conf, but I find it  
clearer  
and better maintainable to use one file for each vhost.

OK, now everything is clear. Thanks a lot for your help! 🙂

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,235490,235514#msg-235514](http://forum.nginx.org/read.php?2,235490,235514#msg-235514)
