proxy_pass based on (possibly changed) $host

Igor Sysoev is at rambler-co.ru
Wed Jan 28 19:26:24 MSK 2009


On Wed, Jan 28, 2009 at 08:13:05AM -0800, Dave Bailey wrote:

> Hi Igor,
> 
> (response at bottom)
> 
> On Wed, Jan 28, 2009 at 7:01 AM, Igor Sysoev <is at rambler-co.ru> wrote:
> >> >   map $remote_addr  $back1 {
> >> >       default       10.0.0.1:80;
> >> >       192.168.1.1   10.0.0.2:80;
> >> >       192.168.1.5   10.0.0.2:80;
> >> >   }
> >> >
> >> >   server {
> >> >       listen   80;
> >> >       server_name   bar.foo.com;
> >> >
> >> >       location  / {
> >> >           proxy_pass  http://$back1$request_uri;
> >> >       }
> >> >   }
> >> >
> >> >   map $remote_addr   $back2 {
> >> >       default        10.0.0.2:80;
> >> >       192.168.10.1   10.0.0.1:80;
> >> >       192.168.10.5   10.0.0.1:80;
> >> >   }
> >> >
> >> >   server {
> >> >       listen   80;
> >> >       server_name   baz.foo.com;
> >> >
> >> >       location  / {
> >> >           proxy_pass  http://$back2$request_uri;
> >> >       }
> >> >   }
> 
> I see, so there is no restriction on the number of server { .. }
> blocks that bind to a given port - so the nginx server { .. } is a
> name-based virtual host, like with Apache.

[ 1st part of response. ]

Yes, and actually server {} is mix of IP-, name-, and port-based virtual hosts:

    http {

        server {
            listen  192.168.10.1;
            listen  192.168.10.1:8000;

            server_name   one.example.com  www.one.example.com;

            ...
        }

        server {
            listen  192.168.10.1;
            listen  192.168.10.2:8000;
            listen  9000;

            server_name   two.example.com  www.two.example.com
                          three.example.com  *.three.example.com;

            ...
        }

        server {
            listen  9000  default;

            server_name   four.example.com  www.four.example.com;

            ...
        }

    }

The request is routed according

1) IP:port
2) exact server_name
3) *.name
4) name.*
5) regex server_name


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list