Redirect subdomains to services on different ports of same server

8,933

Solution 1

You cannot directly use a DNS name to map a specific port, in this case where you have a single IP for multiple DNS names.

Your best bet would be to setup Apache or NGinx as a Reverse Proxy to map service port based on "host header" (or Server Name).

Here is a sample for Apache :

<VirtualHost *:80>
        ServerName service1.mydomain.com
        ProxyPreserveHost On
        ProxyPass / http://service1.mydomain.com:8080/
        ProxyPassReverse / http://service1.mydomain.com:8080/
</VirtualHost>

<VirtualHost *:80>
        ServerName service2.mydomain.com
        ProxyPreserveHost On
        ProxyPass / http://service2.mydomain.com:8081/
        ProxyPassReverse / http://service2.mydomain.com:8081/
</VirtualHost>

So, you will need to setup a Reverse Proxy in front of your Websocket servers.


You will get :

  • http://service1.mydomain.com map and serve http://service1.mydomain.com:8080

and

  • http://service2.mydomain.com map and serve http://service2.mydomain.com:8081

Transparent for end-user.

Solution 2

You cannot map services to ports using DNS. If you open up 8080 and 8081 to the Internet, either domain can be used to access either port. However, if you are publishing URLs including the port number traffic will be routed appropriately.

If I assume these are web services, the normal approach would be to place a proxy (which could be Apache) in front of the services. Users would request service1.example.com or service2.example.com and the proxy would connect to the appropriate service. (I am using example.com as that is one of the domains assigned for examples.)

Share:
8,933

Related videos on Youtube

user2370460
Author by

user2370460

Updated on September 18, 2022

Comments

  • user2370460
    user2370460 over 1 year

    I have a VPS which has 1 IP. If I run two socket servers, one on port 8080 and the other on port 8081, is there a way I can map service1.mydomain.com to the socket server on port 8080 and service2.mydomain.com to the socket server on port 8081?

    I am NOT running a webserver, rather a websocket server on port 8080 and a socket server on 8081.

    I have seen this question asked before, but it always seems to be about Apache, where I could edit the VirtialHosts file. As I am not using Apache, I can't do this.

    Is it possible to do this? Or am I going to have to keep using mydomain.com:8080 and mydomain.com:8081?

    The server is running Debian 8.

    Thanks

    • TessellatingHeckler
      TessellatingHeckler almost 9 years
      How is it "obvious" you aren't using Apache? What service is this and what are you using? You can't put port information in DNS unless the service supports looking for ports in DNS, but maybe you can get a proxy...
  • user2370460
    user2370460 almost 9 years
    Sorry, the services are a websocket server and a socket server. I should have been more clear in the question.
  • BillThor
    BillThor almost 9 years
    @user2370460 In that case, controlling published ports would be appropriate. I would use a port other than 8080, as that will receive a lot of HTTP probes. 8080 is a common alternative or internal port for web services.
  • user2370460
    user2370460 almost 9 years
    could you explain how I could do this?
  • BillThor
    BillThor almost 9 years
    @user2370460 this? Not clear what you are asking. I assume you have control of the services. They should provide information on how to configure IP addresses and ports.
  • user2370460
    user2370460 almost 9 years
    So just to check I understand, on the VPS I would install Apache, and have the above as the VirtualHost file. I add A records for service1.mydomain.com and service2.mydomain.com to the VPS IP, and then run my services on port 8080 and 8081?
  • krisFR
    krisFR almost 9 years
    Yes, you understand correctly :) With Apache running on standard http port