Skip to content

Nuface Blog

隨意隨手記 Casual Notes

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

網路架構、DNS 設定、TLS 與 Postfix/Dovecot SNI 設計全面解析

Posted on 2025-11-202025-11-21 by Rico

【Mail Server 系列文:第 2 篇】

本篇文章是 Mail Server 系列的第二篇。
在前一篇裡,我介紹了整體系統架構與服務角色。本篇將深入解釋:

  • 為什麼 Mail Server 的成功關鍵在於「DNS + 憑證 + 端口」的正確組合
  • 如何設計現代化、多網域、多容器的 Postfix/Dovecot 架構
  • 如何規劃 SNI,讓多網域 IMAP/SMTP 在同一個 Postfix/Dovecot 上安全運作
  • 實務環境中最常遇到的錯誤、盲點與排查方式

這篇內容偏向架構篇,是後續實作篇(Postfix、Dovecot、Amavis、Piler、Roundcube)的基礎。


1. 系統網路架構概觀

以下是本文使用的 Mail Server 佈署架構(即你目前環境的版本):

                    +----------------------+
                    |  Public Internet     |
                    +----------------------+
                           |   (MX)
                        [Firewall]
                           |
                 +---------------------+
                 |   Docker Host (172.18.0.1) |
                 |   intranet-net external    |
                 +---------------------+
                           |
       ---------------------------------------------------------------------
       |           |             |              |                |
   Postfix     Dovecot       Amavis        ClamAV          SpamAssassin
    25/587    110/995/143    10024/10026      3310               783
       |
    pilermilter 33333
       |
     Piler Archive + Manticore
       |
    Roundcube Webmail

🧩 核心概念:所有 Mail 功能都在同一個 Docker Network(intranet-net)之中

此設計的好處:

  • 服務間可直接用容器名稱解析(postfix → dovecot)
  • 不需要複雜 NAT/MASQUERADE
  • 防火牆可以集中在 Host 控制,容器之間僅允許 mail 相關端口

✨ Host(172.18.0.1)作為 Gateway 的價值

  • Roundcube 要連 IMAP/SMTP(tls://it.demo.tw)
  • Postfix 需要連 Dovecot SASL、Amavis、ClamAV、SpamAssassin
  • Piler 需要連到 Manticore、memcached

若 Host Firewall 規則錯誤(例如 REJECT ALL),容器之間會互相無法連線。

實務提醒:在 DOCKER-USER 加入一條允許 intranet-net 的規則非常重要

iptables -I DOCKER-USER 1 -s 172.18.0.0/16 -d 172.18.0.1 -j ACCEPT

否則 Roundcube 會出現:

  • IMAP TLS 錯誤
  • SMTP 無法登入
  • sieve filter 連不上 4190

2. Mail Server 端口與防火牆規劃

Mail Server 涉及非常多 port,如果未先規劃好,會出現各種奇怪問題。

以下為本架構的實際使用端口:

服務Port用途
Postfix25公網收信(MX)與外寄
587Submission(需 TLS + SASL)
465SMTPS(可選、TLS 包裝)
Dovecot110/995POP3 / POP3S
143/993IMAP / IMAPS
24LMTP 給 Postfix 投遞
4190ManageSieve
Amavis10024/10026收郵 / 回投
ClamAV3310病毒掃描
SpamAssassin783spamc 連線
Piler2525Postfix archive transport
Manticore9306/9312全文索引(SQL / Sphinx)
pilermilter33333收件人展開(X-Envelope-To)

3. DNS 設定(多網域架構中最重要的基礎)

若要 Mail Server 正常運作,至少需要:

✔ A Record

it.demo.tw → 公網IP
mail.it.demo.tw → 公網IP(可選)
webmail.it.demo.tw → 公網IP
archive.it.demo.tw → 公網IP

✔ MX Record

it.demo.tw.   MX 10 it.demo.tw.

MX 不能指向 CNAME
必須指向 A Record

✔ SPF(建議使用 -all 或 ~all)

例:

"v=spf1 ip4:x.x.x.x -all"

若需要允許其他寄信來源:

"v=spf1 ip4:x.x.x.x include:_spf.google.com ~all"

✔ DKIM

Amavis 產生:

/etc/amavis/dkim/it.demo.tw.mail2025.pub

TXT 放在:

mail2025._domainkey.it.demo.tw

✔ DMARC

建議起始設定:

_dmarc.it.demo.tw    TXT   "v=DMARC1; p=none; rua=mailto:[email protected]"

後期可改成:

  • p=quarantine
  • p=reject(最高安全等級)

✔ PTR(Reverse DNS)

郵件是否能正常寄到 Gmail / Outlook,PTR 非常重要:

公網 IP → it.demo.tw

4. HTTPS 與 Let’s Encrypt 憑證架構

所有 Web(Roundcube, Piler, Postfixadmin)都透過 Apache Proxy:

[Internet] → apache → (http://container:port)

該架構的優勢:

  • 憑證集中管理
  • 更新憑證不需重建 container
  • Postfix/Dovecot 可直接掛載 /etc/letsencrypt
  • Let’s Encrypt 續約簡單(acme-challenge 由 Apache 處理)

5. Postfix SNI 架構(Inbound/Outbound)

Postfix 支援 Server-Side SNI(接收端)

也就是說:

不同網域連進來時,可以提供不同 TLS 憑證。

例如:

  • it.demo.tw
  • nuface.tw
  • ho-tso.tw
  • awin.idv.tw

都可以用不同的 cert,而不必共用一張 Wildcard。

✔ SNI Maps 必須為 LMDB(不是 hash)

smtpd_tls_server_sni_maps = lmdb:/etc/postfix/tls_sni

範例:

it.demo.tw /etc/letsencrypt/live/it.demo.tw/privkey.pem /etc/letsencrypt/live/it.demo.tw/fullchain.pem
nuface.tw /etc/letsencrypt/live/nuface.tw/privkey.pem /etc/letsencrypt/live/nuface.tw/fullchain.pem

更新後:

postmap -F lmdb:/etc/postfix/tls_sni

Outbound 不需要 SNI

因為 SMTP client 不會使用 hostname 建立 TLS,而是由 MX 伺服器提供憑證。


6. Dovecot SNI 架構(IMAP/POP3/ManageSieve)

Dovecot SNI 必須使用:

local_name it.demo.tw {
    ssl_cert = </etc/letsencrypt/live/it.demo.tw/fullchain.pem
    ssl_key  = </etc/letsencrypt/live/it.demo.tw/privkey.pem
}

local_name nuface.tw {
    ssl_cert = </etc/letsencrypt/live/nuface.tw/fullchain.pem
    ssl_key  = </etc/letsencrypt/live/nuface.tw/privkey.pem
}

必須注意:

  • Roundcube 連線上 IMAP 時,peer_name 必須與 cert CN 相同
  • 若使用 internal host 例如 tls://dovecot 會造成錯誤

所以 Roundcube 指向:

tls://it.demo.tw

而不是 tls://dovecot


7. 常見錯誤與排查方式

1. TLS peer name mismatch

症狀:

  • Roundcube login 無法登入
  • Thunderbird 無法連線
  • openssl s_client 顯示錯誤

原因:

  • cert CN=it.demo.tw,但 client 連到 dovecot:143
  • 或使用內網 IP

2. Docker container 互相連不到

原因:

  • Host firewall 阻擋 172.18.0.0/16 → 172.18.0.1

解法:

iptables -I DOCKER-USER 1 -s 172.18.0.0/16 -d 172.18.0.1 -j ACCEPT

3. DKIM 不生效

常見原因:

  • TXT 行尾多了「;」
  • key 過長導致 DNS 分段錯誤
  • domain selector 拼錯(如 mail20S5 打成 mails025)

4. SPF 錯誤

常見:

v=spf1 include:spf.example.com-all

中間要有空白!


5. SMTP submission(587)無法登入

原因:

  • Dovecot auth 不通
  • Postfix 未正確設定:
smtpd_sasl_path = inet:dovecot:12345

6. Postfix → Amavis → Postfix 回投錯亂

排查順序:

  1. Postfix master.cf 中 10024/10026 是否正確
  2. 回投進 10025/10027 是否有 SNI 或 SASL 被覆寫
  3. 防火牆有沒有擋住 amavis container → postfix container 連線

🎉 結語

本篇介紹了 Mail Server 成功運作的核心要素:

  • 網路拓樸
  • 端口規劃
  • DNS 必要設定
  • TLS/憑證架構
  • Postfix/Dovecot 的 SNI 多網域模式
  • 實際問題排查方式

這些概念是後續文章的基礎,下一篇將正式開始進入:

1 thought on “網路架構、DNS 設定、TLS 與 Postfix/Dovecot SNI 設計全面解析”

  1. Pingback: 打造企業級郵件系統的完整指南(總綱篇) - Nuface Blog

Comments are closed.

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