Search code examples
nginxproxyforward

Proxy pass / Forward request to specific server according to the Host header on Nginx


so I have an Nginx server. And I'm trying to make it so if the host header is test1.example.com, it will proxy pass / forward the request to the specific IP. How could I do that? I've searched everywhere and can't find anything about it.

This is a repost by the way.


Solution

  • I think what you want can be done using a map:

    map $http_host     $my_host_map{
        "foo"         "192.168.1.1";
        "bar"         "192.168.1.2";
        "blort"       "192.168.1.3";
    }
    server{
        listen 80;
        server_name foo;
        location / {
            proxy_pass http://$my_host_map;
        }
    }
    

    with this I can curl my nginx server and set the host in the header:

    curl -v --header Host:"blort" 192.168.1.100