virtualhosts and listen (Listen to all, handle some)

Maxim Dounin mdounin at mdounin.ru
Thu Mar 10 12:39:41 MSK 2011


Hello!

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

> 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 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;
> }
> 
> 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 to handle requests on 192.168.1.1:80 as 
well - you have to explicitly include this listen into 
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 Dounin



More information about the nginx mailing list