Proxy HTTP requests to servers on different ports, using subdomain

Say I have a minimalist Python web server running with several instances, each with different ports specified as command line arguments.

I'd like requests to my server to be redirected like this, using the Host header:

name1.mydomain.com -> localhost:8000
name2.mydomain.com -> localhost:8001
name3.mydomain.com -> localhost:8002

Is this something best done with a server like Lighttpd and doing some virtualhost configuration - is that possible?

I'd prefer not to use something heavy weight like Apache.

Thanks!


Solution 1:

With nginx you could use something like following:

server {
  server_name name1.domain.com;
  location / {
    proxy_pass http://localhost:8000;
  }
}

server {
  server_name name2.domain.com;
  location / {
    proxy_pass http://localhost:8001;
  }
}

server {
  server_name name3.domain.com;
  location / {
    proxy_pass http://localhost:8002;
  }
}

BTW, there is another method to achieve same effect using map directive:

map $http_host  $port {
    hostnames;

    default               8000;
    name1.example.com     8000;
    name2.example.com     8001;
    name3.example.com     8002;
}

server {
    listen       80;
    server_name ~^name\d.example.com;
    location / {
        proxy_pass http://127.0.0.1:$port;
    }
}