# Virtualhosts and listen (Listen to all, handle some)

**URL:** <https://www.ruby-forum.com/t/virtualhosts-and-listen-listen-to-all-handle-some/204346>\
**Category:** NGINX\
**Created:** [March 10, 2011, 9:39am UTC](https://www.ruby-forum.com/t/virtualhosts-and-listen-listen-to-all-handle-some/204346 "2011-03-10T09:39:56Z")\
**Posts on this page:** 4\
**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:** [March 10, 2011, 9:39am UTC](https://www.ruby-forum.com/t/virtualhosts-and-listen-listen-to-all-handle-some/204346/1 "2011-03-10T09:39:56Z")

</div>

hello (sorry for my bad english)

in the following example, nginx do bind on all interfaces because exists  
a “listen \*: 80” right?

OK, so I want to do the following, nginx listens on all interfaces  
(localhost, LAN and Internet) but some virtualhost only respond to some  
interfaces.

The problem I have is that requests do not process the block server as  
it should.

[www.site.com](http://www.site.com) respond forbidden 403 on block with server\_name phpmyadmin,  
why?

I’m doing wrong?

server {  
listen \*:80 default\_server;  
server\_name \_;  
return 444;  
}

server {  
listen 192.168.1.1:80;  
server\_name phpmyadmin;  
}

server {  
listen \*:80;  
server\_name [www.site.com](http://www.site.com);  
}

server {  
listen 127.0.0.1:80;  
server\_name bots;  
}

Posted at Nginx Forum:

> **[virtualhosts and listen (Listen to all, handle some)](https://forum.nginx.org/read.php?2,181900,181900#msg-181900)**
>
> hello (sorry for my bad english) in the following example, nginx do bind on all interfaces because exists a "listen \*: 80" right? OK, so I want to do the following, nginx listens on all interfaces (localhost, LAN and Internet) but some...

---

<div class="post-metadata">

**Author:** ![pepejose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/58f4c7/32.png) [@pepejose](https://www.ruby-forum.com/u/pepejose)\
**Post date:** [March 10, 2011, 10:26am UTC](https://www.ruby-forum.com/t/virtualhosts-and-listen-listen-to-all-handle-some/204346/2 "2011-03-10T10:26:16Z")

</div>

This makes it valid? why wildcard \*: 80 not working properly?  
thanks!!!

server {  
listen 127.0.0.1:80 default\_server;  
listen 192.168.1.1:80 default\_server;  
listen public\_ip:80 default\_server;

server\_name \_;  
return 444;  
}

server {  
listen 192.168.1.1:80;

server\_name phpmyadmin;  
}

server {  
listen 127.0.0.1:80;  
listen 192.168.1.1:80;  
listen public\_ip:80;

server\_name [www.site.com](http://www.site.com);  
}

server {  
listen 127.0.0.1:80;

server\_name bots;  
}

Posted at Nginx Forum:

> **[Re: virtualhosts and listen (Listen to all, handle some)](https://forum.nginx.org/read.php?2,181900,181907#msg-181907)**
>
> hello (sorry for my bad english) in the following example, nginx do bind on all interfaces because exists a "listen \*: 80" right? OK, so I want to do the following, nginx listens on all interfaces (localhost, LAN and Internet) but some...

---

<div class="post-metadata">

**Author:** ![pepejose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/58f4c7/32.png) [@pepejose](https://www.ruby-forum.com/u/pepejose)\
**Post date:** [March 10, 2011, 10:45am UTC](https://www.ruby-forum.com/t/virtualhosts-and-listen-listen-to-all-handle-some/204346/3 "2011-03-10T10:45:41Z")

</div>

Hello!

On Thu, Mar 10, 2011 at 03:38:28AM -0500, pepejose wrote:

> it should.  
> }
> 
> server {  
> listen 127.0.0.1:80;  
> server\_name bots;  
> }

By defining listen on separate ip you exclude it from matching  
on servers with listen \*, very much like what happens with two  
separate listening sockets.

I.e. with your config only “phpmyadmin” will respond on  
192.168.1.1:80, and only “bots” will respond on 127.0.0.1:80.

If you want [www.site.com](http://www.site.com) to handle requests on 192.168.1.1:80 as  
well - you have to explicitly include this listen into  
[www.site.com](http://www.site.com) server, i.e. use

```
server {
    listen *:80;
    listen 192.168.1.1:80;
    server_name www.site.com;
}

```

The same applies to other servers/listens.

Maxim D.

---

<div class="post-metadata">

**Author:** ![pepejose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/58f4c7/32.png) [@pepejose](https://www.ruby-forum.com/u/pepejose)\
**Post date:** [March 10, 2011, 10:57am UTC](https://www.ruby-forum.com/t/virtualhosts-and-listen-listen-to-all-handle-some/204346/4 "2011-03-10T10:57:00Z")

</div>

thanks for answering maxim.

then it not makes sense to use \*: 80 in this case??

the following is correct?

THANK YOU!

## DEFAULT SERVER FOR ALL NETWORK INTERFACES

server {  
listen 127.0.0.1:80 default\_server;  
listen 192.168.1.1:80 default\_server;  
listen public\_ip:80 default\_server;

server\_name \_;  
return 444;  
}

## HANDLE ONLY LAN

server {  
listen 192.168.1.1:80;

server\_name phpmyadmin;  
}

## HANDLE LOCALHOST LAN AND INTERNET##

server {  
listen 127.0.0.1:80;  
listen 192.168.1.1:80;  
listen public\_ip:80;

server\_name [www.site.com](http://www.site.com);  
}

## HANDLE ONLY LOCALHOST##

server {  
listen 127.0.0.1:80;

server\_name bots;  
}

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