DNS (Domain Name System) fundamentally doesn't support port specification in records. A common misconception is that you can append ports to hostnames like example.com:8080
in DNS records - this simply doesn't work at the protocol level.
Reverse Proxy Solution
The most robust approach is using a reverse proxy like Nginx:
server { listen 80; server_name www.example.com; location / { proxy_pass http://localhost:87; proxy_set_header Host $host; } }
SRV Records (Application-Specific)
Some protocols (like SIP, XMPP) support SRV records:
_service._proto.name. TTL class SRV priority weight port target _http._tcp.example.com. 86400 IN SRV 10 5 87 server.example.com.
HTTP Host Header Routing
Modern web servers can route based on Host header:
// Node.js express example const app = require('express')(); app.listen(87, () => { console.log('App listening on port 87'); }); app.get('/', (req, res) => { if(req.hostname === 'www.example.com') { res.send('Port 87 content'); } });
Port Forwarding at OS Level
Using iptables on Linux:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 87
Client-Side Configuration
For non-web services, implement client configuration:
# Python client example import socket def connect_service(): host = 'example.com' port = 87 if host == 'www.example.com' else 80 s = socket.socket() s.connect((host, port))
When dealing with multiple services running on a single server, traditional DNS records (A, AAAA, CNAME) only resolve to IP addresses without port information. This creates a fundamental limitation since port specification is crucial for service differentiation.
SRV Records (Service Records): The proper DNS solution for port specification:
_http._tcp.example.com. 300 IN SRV 10 5 87 webserver.example.com.
_http._tcp.www.example.com. 300 IN SRV 10 5 80 webserver.example.com.
Limitations:
- Requires client-side SRV record support (most browsers don't support HTTP SRV lookups)
- Widely supported by protocols like SIP, XMPP, LDAP
Reverse Proxy Configuration (Nginx example):
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://localhost:87;
}
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://localhost:3000;
}
}
HTTP/HTTPS: Use Host headers for virtual hosting
Database Protocols:
// MongoDB connection string with port
mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
CNAME Weighting: For load balancing across ports
service1.example.com. 300 IN CNAME server-a:8080.example.com.
service1.example.com. 300 IN CNAME server-b:8081.example.com.
- Consul service registry
- Kubernetes Services and Ingress
- Docker Swarm load balancing
URL Redirection:
HTTP/1.1 301 Moved Permanently
Location: http://example.com:8080/newpath
Client-Side Configuration:
# SSH config example
Host myserver
HostName example.com
Port 2222