Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

Hairpin NAT vs. Split DNS — Which Is the Right Choice for Your Network?

Posted on 2025-11-042025-11-04 by Rico

💡 Hairpin NAT (NAT Loopback) — Allow Internal Users to Access Internal Servers Using Public Domain Names

🧱 1. The Scenario

In many corporate or home networks, administrators use NAT (Network Address Translation) to expose internal servers to the Internet.
For example:

  • Server IP: 192.168.1.10
  • Public IP: 122.116.109.114
  • Service: mail.example.com → 122.116.109.114 → 192.168.1.10

External users can access mail.example.com without issues.
However, when internal users try to access the same domain, they often find that:

❌ The connection times out or fails entirely.

This is a classic Hairpin NAT (also known as NAT Loopback) problem.


🧭 2. Understanding the Problem

A NAT device (such as a router, firewall, or Linux gateway) typically handles traffic like this:

  1. An internal client (192.168.1.20) tries to connect to mail.example.com (which resolves to 122.116.109.114).
  2. The router sees that the destination IP is its own WAN address, but the source is inside the LAN.
  3. Without Hairpin NAT configured, the router won’t forward the packet back into the LAN — the connection simply fails.

In short, traditional NAT only handles external → internal translation.
It doesn’t loop traffic originating from inside the same network.

Think of it like mailing a letter to your own house using your public mailing address — the postman doesn’t know he should just hand it back to you.


⚙️ 3. How Hairpin NAT Works

Hairpin NAT allows a packet that originates inside the LAN and targets the router’s public IP to “loop back” to the correct internal server.

Here’s what happens step by step:

  1. Client (192.168.1.20) connects to 122.116.109.114:443.
  2. The gateway rewrites the destination (via DNAT) to 192.168.1.10 (the internal server).
  3. The gateway also rewrites the source (via SNAT) to its own LAN IP, 192.168.1.1.
  4. The server (192.168.1.10) sends its reply to 192.168.1.1, which then SNATs it back to 192.168.1.20.

The key step is #3 — SNAT:

Without changing the source IP, the server would see another local IP (192.168.1.20) and reply directly, bypassing the NAT.
This causes asymmetric routing and broken connections.


🧩 4. Common Configuration Methods

🔹 1️⃣ Linux / iptables

# DNAT: Forward external IP to internal server
iptables -t nat -A PREROUTING -d 122.116.109.114 -p tcp --dport 443 -j DNAT --to-destination 192.168.1.10

# SNAT: Ensure return traffic loops correctly for same subnet
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -d 192.168.1.10 -j SNAT --to-source 192.168.1.1

🔹 2️⃣ OPNsense / pfSense

In the GUI:

  • Go to Firewall → NAT → Outbound
  • Choose Hybrid Outbound NAT
  • Add a rule:
    • Source: LAN subnet
    • Destination: LAN subnet (or specify the internal server IP)
    • Translation: Interface address
  • Save and apply changes.

🔹 3️⃣ Routers / Home Gateways

Some routers (ASUS, Mikrotik, etc.) include a built-in “NAT Loopback” or “Hairpin NAT” feature.
Just enable it, and LAN users can access internal services via the public domain.

In Mikrotik, for example, a masquerade rule automatically handles hairpin NAT behavior.


🧰 5. Alternative Solution — Split DNS

Instead of configuring Hairpin NAT, you can implement Split DNS (also known as internal/external DNS separation):

Network ZoneDNS Response
Externalmail.example.com → 122.116.109.114
Internalmail.example.com → 192.168.1.10

Internal clients directly resolve the local IP, bypassing NAT entirely.

📘 Tip: If you’re running your own DNS server (like BIND9, Unbound, or AdGuard Home),
Split DNS is often a cleaner and more efficient solution.


✅ 6. Conclusion

SolutionProsCons
Hairpin NATNo DNS modification; same domain inside and outsideRequires correct NAT/firewall setup
Split DNSFast and stable; DNS-based controlRequires managing internal DNS zones

If your environment already uses an internal DNS server, Split DNS is the better long-term approach.
Otherwise, enabling Hairpin NAT is a quick and practical fix to keep internal and external access consistent.

Recent Posts

  • Postfix + Let’s Encrypt + BIND9 + DANE Fully Automated TLSA Update Guide
  • Postfix + Let’s Encrypt + BIND9 + DANE TLSA 指紋自動更新完整教學
  • Deploying DANE in Postfix
  • 如何在 Postfix 中部署 DANE
  • DANE: DNSSEC-Based TLS Protection

Recent Comments

  1. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on High Availability Architecture, Failover, GeoDNS, Monitoring, and Email Abuse Automation (SOAR)
  2. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on MariaDB + PostfixAdmin: The Core of Virtual Domain & Mailbox Management
  3. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Daily Operations, Monitoring, and Performance Tuning for an Enterprise Mail System
  4. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Final Chapter: Complete Troubleshooting Guide & Frequently Asked Questions (FAQ)
  5. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Network Architecture, DNS Configuration, TLS Design, and Postfix/Dovecot SNI Explained

Archives

  • December 2025
  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2025 Nuface Blog | Powered by Superbs Personal Blog theme