How to Redirect DNS to a Specific Port?

Published July 10, 2024

Problem: Redirecting DNS to a Specific Port

DNS usually sends traffic to a server's default port. Sometimes, you need to send it to a different port. This can happen when you run multiple services on one IP address or set up special networks. The challenge is telling DNS to send traffic to a specific port without changing the domain name or asking clients to make changes.

Solution: Using SRV Records for Port-Specific DNS Redirection

What are SRV Records?

SRV records are DNS records that provide information about available services. They allow you to specify the location of servers for specific services within a domain. SRV records contain details such as the hostname, port number, and protocol for a service.

The main parts of an SRV record include:

  • Service: The name of the service
  • Protocol: The protocol used (usually TCP or UDP)
  • Domain name: The domain offering the service
  • TTL (Time to Live): How long the record should be cached
  • Priority: Determines which server to try first
  • Weight: Used for load balancing between servers with the same priority
  • Port: The port number where the service is running
  • Target: The hostname of the server providing the service

Tip: Understanding SRV Record Priority and Weight

Priority and Weight in SRV records work together for load balancing and failover. Lower priority values are tried first. If multiple records have the same priority, the weight determines the probability of selection. For example, two records with weights 60 and 40 will be selected roughly 60% and 40% of the time, respectively.

How SRV Records Enable Port-Specific Redirection

SRV records follow a specific structure that allows for service mapping:

_service._protocol.name TTL class SRV priority weight port target

This structure lets you redirect DNS queries to specific ports. By specifying the port number in the SRV record, you can direct traffic to the correct service, even if it's running on a non-standard port.

SRV records map services to specific ports by:

  1. Identifying the service and protocol (e.g., _http._tcp)
  2. Specifying the domain name
  3. Setting the priority and weight for load balancing
  4. Defining the port number for the service
  5. Pointing to the target hostname

For example, an SRV record might look like this:

_http._tcp.example.com. 3600 IN SRV 10 60 8080 server1.example.com.

This record would direct HTTP traffic for example.com to server1.example.com on port 8080, instead of the default HTTP port 80.

Step-by-Step Guide to Implementing DNS Redirection to Specific Ports

Setting Up SRV Records

To set up SRV records for different subdomains and ports:

  1. Access your DNS management interface.
  2. Create a new SRV record for each subdomain and port combination.
  3. Use this format for your SRV records:

_service._protocol.subdomain.domain.com. TTL IN SRV priority weight port target

Example:

_http._tcp.sub1.example.com. 3600 IN SRV 10 100 8080 server1.example.com. _http._tcp.sub2.example.com. 3600 IN SRV 10 100 8081 server2.example.com.

In these examples:

  • Service: HTTP
  • Protocol: TCP
  • TTL: 3600 seconds (1 hour)
  • Priority: 10 for both records
  • Weight: 100 for both records
  • Ports: 8080 and 8081
  • Targets: server1.example.com and server2.example.com

Tip: Understanding SRV Record Fields

Each field in an SRV record serves a specific purpose:

  • Priority: Lower values have higher priority.
  • Weight: Used for load balancing among records with the same priority.
  • Port: The port number where the service is running.
  • Target: The hostname of the machine providing the service.

Configuring A Records

A records are needed when using SRV records as they provide the IP address for the target hostname. To set up A records:

  1. In your DNS management interface, create an A record for each target hostname in your SRV records.
  2. Use this format:

hostname.domain.com. TTL IN A IP_address

Example:

server1.example.com. 3600 IN A 192.0.2.10 server2.example.com. 3600 IN A 192.0.2.11

These A records link the target hostnames in your SRV records to their IP addresses. This allows DNS to resolve the path from the domain name to the IP address and port for each service.