How to create a subdomain pointing to a different port number?

2

1

In my use case I would like to create a sub domain with the main domain's IP address but with different port number. For example:

I have the the main domain as abc.com pointing to 192.168.1.1

@              192.168.1.1

I would like to create to a subdomain with port 8081 like below,

api.abc.com    192.168.1.1:8081

But it looks like it is not possible. So I tried to create a SRV record with the following information,

Name: api
Target: api.abc.com
Protocol: _http
Service: _http
Priority: 5
Weight: 5
Port: 8081
TTL: 1 hour

But this didn't help. Is there any way to achieve this? I am using Godaddy to manage my domains.

Could someone help me with this?

Dany

Posted 2016-07-20T05:07:18.657

Reputation: 121

You should read about VirtualHosts. DNS record of the subdomain should point to your server's external IP address (and should not include a port number) – Alex – 2016-07-20T05:24:40.137

Maybe this can help you: <VirtualHost *:80> ServerAdmin me@mydomain.com ServerName dev.mydomain.com ProxyPreserveHost On # setup the proxy <Proxy *> Order allow,deny Allow from all </Proxy> ProxyPass / http://localhost:8888/ ProxyPassReverse / http://localhost:8888/ </VirtualHost> - sorry for not editing the post - I am still trying to learn how to do this on my phone.

– Alex – 2016-07-20T05:27:52.343

Answers

8

You cannot assign a port number to a domain name or statically-defined host name.

A host name is translated into an IP address and port number is a separate number added to the IP address.


A DNS Service record (SRV) allows you to define the location of a service including its IP address and port number, but this requires a specific application which would query the DNS for the SRV record (RFC2782 calls them "SRV-cognizant clients").

A browser connecting with the HTTP protocol will always connect to port number 80 (unless the port was specified on the client side http://...:8080/). It won't query DNS for the SRV record.


For your use case, you can configure your HTTP server (or a reverse proxy) to redirect the connection based on the Host HTTP header given by the browser. A feature commonly referred to as "virtual hosts".

In this scenario your server would listen to external connections on port 80 and check what address did the client request. If it was api.abc.com it would forward the connection to the internal port 8081 (that is most commonly configured add internal, i.e. 127.0.0.1:8081) otherwise it would serve the content of your regular website.

It is also possible for the HTTPS connections if the client browser supports SNI (and most current browsers do).

techraf

Posted 2016-07-20T05:07:18.657

Reputation: 4 428