Domain – Forward all traffic on a specific port depending on the domain to another port

domaindomain-name-systemipportport-forwarding

Imagine I have 10 different domains. All these domains have an A record to a single IP Address.
There are ten different services hosted on this one IP. Let's assume they listen from port :80 to :90. The default port of the used protocol is :80 and I can't force the users to append an :82 to the domain name.

And here's the problem: I want to internally forward the :80 depending on the used domain name.

  • domain0.com:80 –> :80
  • domain1.com:80 –> :81
  • domain2.com:80 –> :82
  • domain3.com:80 –> :83

I googled around and came across iptables. Is this what I'm looking for?

Best Answer

Generally this can not be done like that. When the client connects via TCP and/or UDP the server has no information on what domain the client used to request the server's IP address.

Since you are mentioning port 80 you could be talking about HTTP traffic. In that case this distinction can be made since the HTTP protocol also transfers the selected domain name. You could use a HTTP server like Apache and configure a virtual host for each domain that acts as a reverse proxy and forwards the traffic to the correct port. A configuration like this could look something like that:

<VirtualHost *:80>
    ServerName domain1.com

    ProxyPass / http://server-ip:81/
</VirtualHost>

For this to work the modules proxy and proxy_http need to be activated.

Related Topic