5

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!

Luke Stanley
  • 161
  • 1
  • 8

1 Answers1

4

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;
    }
}
AlexD
  • 8,179
  • 2
  • 28
  • 38
  • Is it working with local domains ? I am trying to exactly this is a virtual machine, and I added it's IP in my `/etc/hosts` folder – nha Jul 31 '15 at 15:48