Skip to content

Nuface Blog

隨意隨手記 Casual Notes

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

Mail Server 全系統架構與流程總覽(含架構圖)

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

【Mail Server 系列文:第 14 篇】

在前 1–13 篇中,我們已經完成了一整套企業級郵件系統:
Postfix、Dovecot、Amavis、ClamAV、SpamAssassin、Roundcube、Piler、Manticore、MariaDB、Proxy、SSL、SNI…
所有組件都已經成功被容器化、整合並投入實際運作。

本篇是整個系列的收尾篇,將完整呈現 系統架構圖、流量路徑、元件互動與流程圖,並清楚整理整個系統的全貌。


📌 一、Mail Server 系統完整架構圖(總覽)

以下架構圖描述了所有模組與 Docker 架構:

                   ┌───────────────────────────────────────┐
                   │                 Internet               │
                   │  (Sender SMTP / Receiver SMTP/IMAP)   │
                   └───────────────────────────────────────┘
                                   │
                                   ▼
                       ┌────────────────────┐
                       │   Proxy / Firewall │
                       │   (OPNsense / iptables)│
                       └────────────────────┘
                                   │
                                   ▼
          ┌────────────────────────────────────────────────────────────┐
          │                   Docker Host (172.18.0.1)                 │
          │                  intranet-net 虛擬網路                     │
          └────────────────────────────────────────────────────────────┘
                                   │
                                   ▼
────────────────────────────────  郵件主要服務  ────────────────────────────────

Postfix(SMTP)
──────────────
 - 收信(25)
 - 寄信(587 / 465)
 - DKIM / SNI
 - SQL 虛擬網域 / 郵件路由(transport)
 - Postfix-Milter → Piler(X-Envelope)

Dovecot(IMAP/POP/LMTP/Sieve)
─────────────────────────────
 - IMAP/POP3
 - LMTP 投遞
 - Sieve 過濾規則
 - Spam/Ham 遠端學習(SpamAssassin)
 - Quota(超限通知)

Amavis → ClamAV + SpamAssassin
─────────────────────────────
 - 病毒掃描
 - 垃圾信過濾
 - DKIM 簽章
 - 內容過濾

Piler(Mail Archive)
─────────────────────────────
 - Email Store
 - Metadata
 - Permission Mapping
 - Web Portal

Manticore Search(全文檢索)
─────────────────────────────
 - CJK 分詞
 - Real-time Index
 - SQL Search

Roundcube(Webmail)
─────────────────────────────
 - Webmail UI
 - ManageSieve
 - SMTP/IMAP 連線

MariaDB(後端資料庫)
─────────────────────────────
 - PostfixAdmin
 - Domain & Mailbox
 - Alias / Transport
 - Piler DB

Apache Proxy(webmail / archive)
───────────────────────────────
 - HTTPS
 - 反向代理
 - Let's Encrypt SSL

Memcached
───────────────────────────────
 - Piler Cache
 - Session Cache

📌 二、郵件收信流程(Inbound Flow)

Internet → Postfix(25) → content_filter → Amavis → ClamAV/SpamAssassin
       → 回投至 Postfix(10025) → LMTP → Dovecot → 存入 Maildir
       → Piler Milter(X-Envelope-To) → Piler Archive → Manticore Index

步驟解析:

  1. 外部郵件伺服器透過 SMTP port 25 將郵件送至 Postfix
  2. Postfix 將信交給 Amavis 做內容過濾
  3. Amavis 呼叫:
    • ClamAV(病毒掃描)
    • SpamAssassin(垃圾信評分)
    • DKIM/Anti-Spam Header 注入
  4. Amavis 將信回送至 Postfix(port 10025)
  5. Postfix 計算收件人 → 使用 LMTP 投遞至 Dovecot
  6. Dovecot 寫入 maildir
  7. Piler Milter 在 SMTP 阶段寫入 Envelope Header
  8. Piler 接收郵件(由 always_bcc)→ 存檔 → Manticore 索引
  9. 郵件完成入庫 + 入信箱

📌 三、郵件寄信流程(Outbound Flow)

Roundcube / Outlook / Client → Submission(587)/SMTPS(465)
→ SASL(Dovecot Auth) → Postfix → Amavis(10026) → DKIM Sign
→ Postfix Relay → Internet

步驟解析:

  1. 使用者登入 Roundcube / Outlook
  2. Client 使用:
    • 587 (STARTTLS)
    • 465 (SSL wrapper)
  3. Postfix 透過 Dovecot SASL 驗證使用者
  4. 信件送入 Amavis(10026)埠:
    • DKIM 簽章
    • Anti-Spam Header 設定
  5. Postfix Relay → Internet

Outbound 的安全辯證:

  • SASL 強制認證
  • TLS Encryption
  • 避免寄件者假冒(reject_authenticated_sender_login_mismatch)
  • milter_macro_daemon_name=ORIGINATING 區分內外部來源

📌 四、Piler(郵件歸檔)與 Manticore(全文檢索)流程

Piler 主要任務:

✔ 長期信件保存
✔ Metadata + Envelope Mapping
✔ 分群權限(誰能看哪些信)
✔ 提供 Web 介面搜尋/預覽
✔ IMAP Restore(復原至 Dovecot)

Manticore 主要任務:

✔ 中文分詞(ngram + ICU)
✔ 即時寫入索引(RT Index)
✔ Full-Text Query(subject / body / address / attachment name)

整體流程

Postfix always_bcc → piler@[local]
   ↓
Piler SMTP
   ↓
Metadata → MariaDB (piler)
Raw Mail → Piler Store (/var/piler/store)
   ↓
Manticore RT Index
   ↓
User Search → Archive Web UI → SQL Query → Result + Preview

📌 五、Spam/Ham 遠端學習(IMAPSieve → SpamAssassin Remote)

使用者在 Roundcube:

  • 將信件拖到垃圾信(SPAM) → sa-learn spam
  • 將信件拖回 INBOX → sa-learn ham

架構:

Dovecot IMAPSieve → global/learn-spam.sieve → sa-remote-learn-spam.sh
                                               ↳ spamc -L spam
Dovecot IMAPSieve → global/learn-ham.sieve  → sa-remote-learn-ham.sh
                                               ↳ spamc -L ham

所有學習都透過 spamassassin 容器 完成,使 SpamAssassin Bayes 好好運作。


📌 六、Roundcube Webmail 與 Apache 反向代理

使用方式

瀏覽器 → https://webmail.it.demo.tw/

Apache Proxy:

443 → ProxyPass → http://webmail:8000/

Roundcube → Dovecot:

  • TLS IMAP 143(STARTTLS)
  • ManageSieve 4190

Roundcube → Postfix:

  • SMTP 587(STARTTLS)

📌 七、主機防火牆與 Docker 網路規劃

重要原則:

  • 所有容器均位於 intranet-net 172.18.0.0/16
  • 主機(172.18.0.1)對容器提供 TLS Proxy
  • DOCKER-USER chain 最後要留一個 REJECT
  • 所有內部服務必須加:
iptables -I DOCKER-USER 1 -s 172.18.0.0/16 -d 172.18.0.1 -j ACCEPT

以避免 Webmail 的 IMAP/TLS 被擋。


📌 八、所有容器服務總表

服務ContainerPort主要功能
Postfixpostfix25 / 587 / 465SMTP 收發、SASL、DKIM、SNI
Dovecotdovecot110 / 995 / 143 / 993 / 24 / 4190IMAP/POP/LMTP/Sieve/Quota
Amavisamavis10024 / 10026Anti-Spam / Anti-Virus / DKIM
ClamAVclamavlocal socket病毒掃描
SpamAssassinspamassassin783Spam 評分 + Remote Learn
MariaDBmaildb3306PostfixAdmin + Piler DB
Pilerpiler25 / Web UI郵件歸檔
Manticoremanticore9306 / 9312全文檢索
Roundcubewebmail8000Webmail
Apache Proxywwwapp80 / 443Webmail / Archive Proxy
Memcachedmemcached11211Cache

📌 九、整套 Mail Server 架構完成後的能力

✔ 可支援多網域

✔ 支援 SNI TLS

✔ 支援 DKIM / SPF / DMARC

✔ 支援 Anti-Spam、Anti-Virus

✔ 支援 Webmail

✔ 支援 Mail Archive + Full-Text Search

✔ 支援 Outlook / Thunderbird / Mobile Client

✔ 支援 即時 Spam/Ham 學習

✔ 100% Docker 化,多主機可搬移

✔ 企業級可用性(高彈性 + 高可維護性)

此架構已適用於中小企業甚至大型企業,功能與商用郵件系統相同甚至更強。

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