Skip to content

Nuface Blog

隨意隨手記 Casual Notes

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

Hairpin NAT vs Split DNS:哪種才是正確做法?

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

🧱 一、發生的情境

在企業或家庭網路中,我們常透過 NAT(Network Address Translation) 將內部伺服器對外提供服務。例如:

  • 伺服器 IP:192.168.1.10
  • 公網 IP:122.116.109.114
  • 服務:mail.example.com → 122.116.109.114 → 192.168.1.10

這時候,外部用戶從網際網路訪問 mail.example.com 一切正常。
但公司內部用戶如果也輸入相同網址(mail.example.com),卻發現:

❌ 無法連線、逾時、或導向錯誤的主機。

這就是典型的 Hairpin NAT(又稱 NAT Loopback) 問題。


🧭 二、問題的本質

NAT 裝置(例如 Router、防火牆、OPNsense、Linux Gateway)收到封包時的邏輯如下:

  1. 內部用戶 192.168.1.20 嘗試連線 mail.example.com(實際指向 122.116.109.114)。
  2. 路由器發現目標 IP 是自己的 WAN 介面(122.116.109.114),但來源來自內部網段。
  3. 沒有正確設定 Hairpin NAT 時,路由器不會將封包「轉回」內部伺服器,導致連線中斷。

簡單說,NAT 預設只處理外部 → 內部 的轉換,而不會自我迴圈。
這就像你寄信到自己的郵局地址,但郵差不知道該送回自己家。


⚙️ 三、Hairpin NAT 的運作原理

Hairpin NAT 讓「內部客戶端 → NAT 公網 IP → 內部伺服器」的封包能正確繞回內網。

流程如下:

  1. Client(192.168.1.20) → 連線 122.116.109.114:443
  2. Gateway(192.168.1.1)在 PREROUTING 階段將目的 IP 122.116.109.114 DNAT 成 192.168.1.10
  3. Gateway 同時在 POSTROUTING 階段將來源 IP 192.168.1.20 SNAT 成 192.168.1.1
  4. Server(192.168.1.10)回覆封包 → 回到 Gateway(192.168.1.1) → 再 SNAT 回 192.168.1.20

最關鍵的部分是第 3 步的 SNAT:

如果不改變來源 IP,Server 看到來源為同網段(192.168.1.20),會直接回封包,不經 NAT,導致連線中斷。


🧩 四、常見環境設定方式

🔹 1️⃣ Linux / iptables

# DNAT:將外部 IP 映射回內部伺服器
iptables -t nat -A PREROUTING -d 122.116.109.114 -p tcp --dport 443 -j DNAT --to-destination 192.168.1.10

# SNAT:針對同網段的來源流量,讓它繞回 NAT
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

在 GUI 中:

  • Firewall → NAT → Outbound
    • 選擇「Hybrid Outbound NAT」
    • 新增規則:
      • Source:LAN subnet
      • Destination:LAN subnet (或指定伺服器 IP)
      • Translation:介面地址(Interface Address)
    • 儲存並套用即可。

🔹 3️⃣ Router / 家用設備

某些 Router(如 ASUS、Mikrotik)內建 “NAT Loopback” 或 “Hairpin NAT” 選項:

  • 開啟該功能即可讓 LAN 用戶使用外部網域存取內部服務。
  • Mikrotik 可用 masquerade 規則自動實現。

🧰 五、另一種解法:Split DNS

若不想在防火牆處理 Hairpin NAT,也可採用 Split DNS(內外 DNS 分離):

網路區域DNS 回應
外部網路mail.example.com → 122.116.109.114
內部網路mail.example.com → 192.168.1.10

內部用戶直接解析到內部 IP,不會經過 NAT,連線更直接也更快。

📘 建議:若公司內部有自架 DNS(如 BIND9、Unbound、AdGuard Home),Split DNS 是更優雅、可控的方案。


✅ 六、結論

解法優點缺點
Hairpin NAT不需改 DNS、外內皆用同一網域對防火牆設定有要求
Split DNS高效穩定、容易維護需自行維護 DNS 區域記錄

若環境中已有本地 DNS,建議採用 Split DNS;
若無 DNS 管理能力,則可啟用 Hairpin NAT 支援內外一致存取。

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