Web-server – How to use DNS/Hostnames or Other ways to resolve to a specific IP:Port

domain-name-systemredirectweb-server

This is a Canonical Question about DNS/Hostnames resolution to IPs/Ports

Example 1

I'm running a web server on port 80 and another on port 87. I would like to use DNS so that www.example.com goes to port 87. How can I accomplish this using DNS only?

Example 2

I'm running a service on my server on a non-standard port. How can I get clients to connect to this non-standard port automatically? Can I use DNS? Is there some application specific support where DNS could indicate the IP and Port?

Example 3

Do some application protocols specifically support hostname awareness, and allow special actions to be taken based on this information? Are there other questions on Server Fault that cover some of these?

Commandeering: This question was originally asking about running IIS and Apache on the same server, but the same concepts can be applied to any server software receiving connections from clients. The Answers below describe the technical problems and solutions of using DNS and application protocol support to assign a port number for a client to connect.

Best Answer

You cannot use the DNS to point to a port (unless the client supports SRV records, most don't).

Websites and Protocols with Host Headers

You will have to put some front-end method in place to do this. Typically you would use a front end web server or a dedicated proxy software to forward the connection from port 80 to port !80 based on the name of the server being requested in the header. Some firewalls can also forward based on the host header too.

SRV Records

Some clients support lookups of SRV records which indicate hostname and port number of server for the specified service (ie the user specifies "example.com", the client looks up a SRV record and gets "server101.example.com" on port "255"; then connects to that). Some clients also implement this where it is not required (my last smartphone would lookup the SRV records when setting up a new e-mail account for example).

Unfortunately support for SRV records is highly uncommon. Only a few notable protocols mandate it's support (Jabber/XMPP, Kerberos, LDAP, SIP) and not every client supports it even when mandated.