Keywords: DNS | SRV records | port redirection
Abstract: This article explores how to map multiple subdomains to different ports on the same IP address via DNS SRV records, addressing access issues in multi-server deployments on home networks. Using Minecraft servers as an example, it details the structure, configuration, and working principles of SRV records with client support. Alternative solutions like load balancing are compared, providing practical guidance for network administrators.
The Technical Challenge of DNS Port Redirection
In traditional DNS resolution, domain names typically map to IP addresses, with port information specified by applications or users. However, in scenarios such as running multiple services on different ports of the same IP, users may desire direct access to specific ports via subdomains without manual port entry. This raises the question of whether DNS can include port information.
SRV Records: DNS Port Mapping Mechanism
SRV (Service) records are a DNS resource record type designed to specify service locations, including hostnames and port numbers. The standard format is: _service._proto.name. TTL class SRV priority weight port target. Here, service denotes the service name, proto is the protocol (e.g., TCP or UDP), port specifies the port, and target points to the host providing the service.
For Minecraft servers, since version 1.3.1, clients support SRV record lookup using the service name _minecraft and protocol _tcp. Suppose a user owns the domain arboristal.com with three servers on ports 25565, 25566, and 25567, corresponding to subdomains mc.arboristal.com, tekkit.arboristal.com, and pvp.arboristal.com. Configure SRV records as follows:
_minecraft._tcp.arboristal.com. 86400 IN SRV 10 40 25565 mc.arboristal.com.
_minecraft._tcp.arboristal.com. 86400 IN SRV 10 30 25566 tekkit.arboristal.com.
_minecraft._tcp.arboristal.com. 86400 IN SRV 10 30 25567 pvp.arboristal.com.Here, priority and weight facilitate load balancing, with lower priority preferred and higher weight increasing selection probability for equal priorities. TTL is set to 86400 seconds, controlling cache duration.
Configuration and Implementation Steps
First, in the DNS provider's interface (e.g., webs.com), add A records to point subdomains to the public IP address (e.g., 71.82.237.27). Then, create SRV records, ensuring correct service and protocol names. For Minecraft, use _minecraft._tcp. When clients connect, they query SRV records for ports and resolve IPs via A records, enabling seamless access.
Sample code demonstrates SRV record query simulation in Python:
import dns.resolver
# Query SRV records
def query_srv(domain):
try:
answers = dns.resolver.resolve('_minecraft._tcp.' + domain, 'SRV')
for rdata in answers:
print(f'Port: {rdata.port}, Target: {rdata.target}')
except Exception as e:
print(f'Error: {e}')
query_srv('arboristal.com')This code outputs ports and target hosts from SRV records, aiding configuration verification.
Alternative Solution: Load Balancers
Beyond SRV records, load balancers like Nginx offer server-side solutions. By configuring Nginx to listen on a single port (e.g., 25565) and proxy traffic to multiple backend ports, port redirection is achieved. Example configuration:
upstream minecraft_servers {
ip_hash;
server 127.0.0.1:25566 weight=1;
server 127.0.0.1:25567 weight=1;
server 127.0.0.1:25568 weight=1;
}
server {
listen 25565;
proxy_pass minecraft_servers;
}This method does not rely on client SRV support but introduces Nginx as a single point of failure. In contrast, SRV records are lighter and suitable for simple deployments.
Advantages and Disadvantages Analysis
Advantages of SRV records include: no additional software required, reducing attack surface; leveraging DNS caching for performance; supporting client-side load balancing. Disadvantages are: dependency on client implementation (e.g., Minecraft 1.3.1+); configuration changes affected by TTL; potential fallback to default ports for non-SRV-aware clients.
Load balancer advantages: unified entry point, simplifying firewall rules; immediate configuration effect; support for multiple protocols. Disadvantages: increased system complexity; potential performance bottleneck.
Practical Recommendations
For Minecraft servers on home networks, SRV records are ideal as they integrate directly into DNS without maintaining extra services. Ensure correct router port forwarding and monitor connection status. If scaling to more services or requiring high availability, consider combining with load balancers.
In summary, SRV records provide a standardized way to achieve port redirection via DNS, particularly for applications like Minecraft that support it. Understanding their principles and configuration methods can help network administrators optimize service deployments.