# Virtualhosts and map

**URL:** <https://www.ruby-forum.com/t/virtualhosts-and-map/226933>\
**Category:** NGINX\
**Created:** [February 26, 2013, 10:32am UTC](https://www.ruby-forum.com/t/virtualhosts-and-map/226933 "2013-02-26T10:32:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![varix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e8c25b/32.png) [@varix](https://www.ruby-forum.com/u/varix)\
**Post date:** [February 26, 2013, 10:32am UTC](https://www.ruby-forum.com/t/virtualhosts-and-map/226933/1 "2013-02-26T10:32:29Z")

</div>

What is the best way to configure virtualhost with map?

Varix

Posted at Nginx Forum:

> **[Virtualhosts and map](https://forum.nginx.org/read.php?2,236611,236611#msg-236611)**
>
> What is the best way to configure virtualhost with map? Varix

---

<div class="post-metadata">

**Author:** ![varix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e8c25b/32.png) [@varix](https://www.ruby-forum.com/u/varix)\
**Post date:** [February 26, 2013, 2:53pm UTC](https://www.ruby-forum.com/t/virtualhosts-and-map/226933/2 "2013-02-26T14:53:42Z")

</div>

On 26 February 2013 09:31, Varix [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the best way to configure virtualhost with map?

The question you have asked is very wide-ranging yet vague.  
The only answer I can give you is “it depends on what you’re trying to  
achieve”.

I suggest you formulate a better question.

If you include details of the things that you have tried already, this  
will serve as a good indicator to people on the list that you’re not  
just being lazy, can’t be bothered to google, or want people to do  
your job for you.

## HTH, Jonathan

Jonathan M. // Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**

---

<div class="post-metadata">

**Author:** ![varix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e8c25b/32.png) [@varix](https://www.ruby-forum.com/u/varix)\
**Post date:** [February 27, 2013, 12:40pm UTC](https://www.ruby-forum.com/t/virtualhosts-and-map/226933/3 "2013-02-27T12:40:48Z")

</div>

Hallo Jonathan,

I have some proplems with the english language. This is bad in the IT  
section.

My problem is, i can’t find an complett example to do that for  
education.  
All I find is  
the “old Way” with sites-availbled and sites-enabled, what I have done  
for  
years.

In January I chance nginx to the new version.

My files in the folder sites-availabled  
default

> **[Example 1](http://example1.com)**

[example2.com](http://example2.com)  
[example3.com](http://example3.com)

My default file

# default server

# 

server {  
listen 80 default\_server;  
server\_name \_;  
access\_log logs/default/default.access.log;  
error\_log logs/default/default.error.log;

```
root /www/default;

location / {
    root /www/default;
    index index.html index.htm;
}

location /i/ {
     alias /www/123/;
    }

# redirect server error pages to the static page /40x.html
#

error_page 400 401 402 403 404 /40x.html;
location = /40x.html {
     root /www/default/html;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /www/default/html;
}

```

If the IP (xxx.xxx.xxx.xxx) is type in the browser, it shows only domain  
[example1.com](http://example1.com).

After a few days I found the reason for this:

Change: now if the “include” directive with mask is used on Unix  
systems, included files are sorted in alphabetical order.

My solution for this is to make a new big nginx.conf  
In the part with the serverblocks the latest is the default one and all  
is  
OK.  
This solution is OK for a few domains, but not for many domains.  
I read that this can be done with map. But I can’t find an example for  
this.

Now I am looking for examples how I can do that with map.

This from the nginx docu is not enough for me.

map $http\_host $name {  
hostnames;

```
default 0;

example.com 1;
*.example.com 1;
example.org 2;
*.example.org 2;
.example.net 3;
wap.* 4;

```

}

Varix

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

---

<div class="post-metadata">

**Author:** ![varix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e8c25b/32.png) [@varix](https://www.ruby-forum.com/u/varix)\
**Post date:** [February 27, 2013, 4:43pm UTC](https://www.ruby-forum.com/t/virtualhosts-and-map/226933/4 "2013-02-27T16:43:26Z")

</div>

Hallo Maxim D.,

thanks for your answer. It helps me.

> This can’t be a reason as long as you have “default\_server”  
> parameter of the “listen” directive properly set for listen  
> sockets used.

My thought was, the default file is the first alphabetical file  
in the folder. And the next domainfile namend with do\*.\*.  
As I make the big nginx.conf for the first time I had the same problem.  
I had the default at the first serverblock. Then I write this  
serverblock  
to the end of the serverblocks and all was OK. This alphabetical sort  
managed something in a other way, I thought.

I will test same things in the next days. Thanks

Varix

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

---

<div class="post-metadata">

**Author:** ![varix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e8c25b/32.png) [@varix](https://www.ruby-forum.com/u/varix)\
**Post date:** [February 27, 2013, 2:04pm UTC](https://www.ruby-forum.com/t/virtualhosts-and-map/226933/5 "2013-02-27T14:04:21Z")

</div>

Hello!

On Wed, Feb 27, 2013 at 06:40:27AM -0500, Varix wrote:

> In January I chance nginx to the new version.
> 
> # default server
> 
> ```
> location / {
> 
> }
> 
> ```
> 
> If the IP (xxx.xxx.xxx.xxx) is type in the browser, it shows only domain  
> [example1.com](http://example1.com).
> 
> After a few days I found the reason for this:
> 
> Change: now if the “include” directive with mask is used on Unix  
> systems, included files are sorted in alphabetical order.

This can’t be a reason as long as you have “default\_server”  
parameter of the “listen” directive properly set for listen  
sockets used.

> ```
> wap.* 4;
> 
> ```
> 
> }

Maps may be used to handle multiple domains in one server block,  
e.g. to set document root depending on a domain:

```
map $host $server_root {
    hostnames;

    default /www/default;
    foo.example.com /www/foo;
    foo.example.org /www/foo;
    bar.example.com /www/bar;
}

server {
    listen 80;
    server_name foo.example.com foo.example.org bar.example.com ...;

    root $server_root;

    ...
}

```

This aproach may be usable if you have mostly identical handling  
for many domains, with some minor differences which can be handled  
using variables. It only makes sense if you have really many  
domains (thousands of), and can’t afford distinct server{} blocks  
for them.

–  
Maxim D.

> **[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.
