Skip to content

Nuface Blog

隨意隨手記 Casual Notes

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

郵件系統完整架構、郵件流程示意、監控、備份、強化與最佳化建議

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

【Mail Server 系列文:第 10 篇】

到目前為止,我們已經完成:

  • MariaDB + PostfixAdmin(使用者管理)
  • Postfix(SMTP 收發信)
  • Dovecot(IMAP/POP3 + Sieve)
  • Amavis + SpamAssassin + ClamAV(垃圾郵件與病毒防護)
  • Piler(郵件留存與查詢)
  • Manticore(搜尋引擎,支援中文斷詞)
  • Roundcube(Webmail)
  • Pilermilter(封包收件人標頭擷取)
  • 全套 Docker 化、自動 SSL 更新、反向代理整合

在本篇第 10 篇,我們將進行:「總結與系統最佳化」。
提供完整的 郵件收發流程圖、架構圖、監控建議、備份方案、資安強化及效能調校策略。


🔶 1. 完整郵件架構示意圖(最終版本)

                 ┌────────────┐
                 │   Internet │
                 └─────┬──────┘
                       │ 25/465/587/993
                ┌──────▼──────┐
                │   Nginx/Apache│  ← HTTPS 反向代理
                └──────┬──────┘
                       │(Docker Network)
 ┌──────────────────────────────────────────────────────────┐
 │                   Docker intranet-net                     │
 │                                                          │
 │   ┌──────────┐     ┌──────────────┐     ┌──────────┐    │
 │   │ Postfix  │◄───►│   Amavis      │◄───►│ ClamAV   │    │
 │   │ (SMTP)   │     │ SA + ClamAV   │     │ Antivirus│    │
 │   └─────┬────┘     └──────▲───────┘     └─────┬────┘    │
 │         │25           Spam/Ham  |            3310│        │
 │         │                          ┌───────────▼─────────┐
 │   ┌─────▼────────┐                 │ SpamAssassin Server │
 │   │   Dovecot    │                 │ (remote spamc 783)   │
 │   │IMAP/LMTP/Sieve│◄───────────────└─────────────────────┘
 │   └─────┬────────┘
 │         │LMTP
 │   ┌─────▼────────┐
 │   │   Piler      │←─── From Postfix always_bcc
 │   │ Email Archive│
 │   └─────┬────────┘
 │         │SQL + Index
 │   ┌─────▼────────┐
 │   │ Manticore     │←── 中文搜尋、索引
 │   └───────────────┘
 │
 │   ┌─────────────┐
 │   │ Roundcube    │→ IMAP/SMTP + ManageSieve
 │   └─────────────┘
 └──────────────────────────────────────────────────────────┘

此架構具備:

  • 高度模組化
  • 各服務獨立容器化
  • 防毒 / 防垃圾信 / DKIM
  • 即時存檔
  • 中文搜尋強化
  • 多網域支援
  • SNI 多憑證 SMTP TLS
  • Webmail + Sieve

🔶 2. 郵件收發全流程解析(Inbound / Outbound)


📥 Inbound 收信流程(外部 → 你的 Mail Server)

Internet → Postfix(25) → Amavis → SpamAssassin → ClamAV → Dovecot(LMTP) → 使用者信箱
                                         │
                                         └→ Piler (via always_bcc)

流程說明:

  1. 外部 SMTP 伺服器透過 Port 25 投遞
  2. Postfix 送至 Amavis 進行內容檢查
  3. SA + ClamAV 判斷垃圾 / 惡意程式
  4. Amavis 回送已標記信件至 Postfix 10025
  5. Postfix 透過 LMTP 傳給 Dovecot 儲存
  6. 同時 always_bcc 將副本送至 Piler 做存檔

📤 Outbound 寄信流程(你的使用者 → 外部)

Roundcube / Client → Postfix(587) → SASL Auth → Amavis → ClamAV → Internet

流程說明:

  1. 使用者透過 TLS/587 驗證(SASL)
  2. Postfix 先交由 Amavis → ClamAV → SA
  3. DKIM 於 Amavis 層完成簽名
  4. 寄往 Internet 外部郵件伺服器

🔶 3. 監控與 Log 分析建議

完整郵件系統不可缺少 監控機制,以下列出最重要的監控項目。


📌 3.1 SMTP / IMAP 與流量監控

服務Port監控方式
Postfix SMTP25 / 587檢查 smtp banner、隊列大小、TLS 狀態
Dovecot IMAP143/993檢查登入成功率、TLS Cipher、連線數
Amavis10024/10026監控處理時間、錯誤率
SpamAssassin783學習紀錄、規則更新
ClamAV3310freshclam 更新、病毒碼版本

可加入 Prometheus Exporter:

  • postfix-exporter
  • dovecot-exporter
  • node-exporter

📌 3.2 重要 Log 檢查項目

ComponentPath重點
Postfix/var/log/postfix/postfix.logmail rejected / auth / queue
Dovecot/var/log/dovecot/dovecot.logIMAP 錯誤、login 失敗
Amavis/var/log/amavis/amavis.log掃描時間 / timeout
ClamAV/var/log/clamav/clamd.log病毒碼更新問題
Piler/var/log/piler存檔錯誤
Roundcube/var/log/webmailWebmail login、SMTP failures

🔶 4. 備份策略(非常重要)

郵件系統中有「三大需要備份的區塊」。


📌 4.1 使用者信箱(Dovecot Maildir)

/opt/docker/mail/dovecot/usermail

建議:

  • 每日快照(rsync)
  • 每週完整備份

📌 4.2 MariaDB(PostfixAdmin / Piler / SpamAssassin)

資料庫表包含:

  • mailbox / domain / alias
  • Piler metadata(id/subject/body pointer)
  • SA Bayes / TxRep

建議備份:

mysqldump --single-transaction --default-character-set=utf8mb4

📌 4.3 Piler Archive Storage

/opt/docker/mail/piler/piler_store

此區域為「真正的郵件存檔檔案」。

必須定期備份到:

  • 本機 RAID
  • NAS
  • 雲端(S3 Glacier)

🔶 5. 資安強化(Security Hardening)


📌 5.1 TLS 與憑證管理

  • Postfix/Dovecot 使用 Let’s Encrypt
  • SNI 管理多網域憑證
  • 強制 TLS on submission (587)
  • 維持 modern cipher suites

📌 5.2 防暴力破解

Dovecot 建議加入 fail2ban:

  • 考慮放在 Host OS(避免容器過度複雜)
  • 過濾 IMAP / SMTP auth 錯誤
  • 自動加入 iptables reject

📌 5.3 限制 IP 與網路區段

Postfix:

mynetworks = 127.0.0.0/8 172.18.0.0/16

Do not open SMTP submission to everyone unless you enforce:

  • strong authentication
  • rate limits

📌 5.4 前端反向代理安全強化

例如 Apache:

  • HSTS
  • Disable SSLv3 / TLS1.0
  • Strict-Transport-Security
  • Content-Security-Policy

📌 5.5 DKIM, SPF, DMARC

三者皆需啟用。

  • DKIM:由 Amavis 簽章
  • SPF:DNS TXT
  • DMARC:policy + report

🔶 6. 效能最佳化(Performance Tuning)

以下是中大型郵件量建議:


📌 6.1 Postfix 調校

  • queue 位置使用 SSD
  • 避免過度使用 DNS 查詢
  • workers 設定與 CPU 核心數一致

📌 6.2 Dovecot 調校

  • mailbox dir 使用 SSD
  • LMTP 佇列增加 concurrency

📌 6.3 Amavis + SA 調校

  • 開啟 sa-compile 提升規則效能
  • 增加預先 fork 子程序 $max_servers
  • 加大 TEMP (通常 1~2GB)

📌 6.4 ClamAV 調校

  • 512M~1G RAM for clamd
  • 增加 thread 數

📌 6.5 Manticore 調校

  • 高速 SSD(因為索引頻繁)
  • 預設中文斷詞已優化(icu + ngram)

🔶 7. 全系統自動化維運


📌 自動 SSL 更新(Certbot)

你之前已建立:

renewssl.sh

並加入 crontab:

0 1 * * * /opt/docker/wwwapp/renewssl.sh

📌 自動 SA 更新

Amavis 已配置 cron:

sa-update && sa-compile && HUP amavisd

📌 自動清理 mail queue

postsuper -d ALL deferred

🔶 8. 最終建議與部署注意事項


✔ 建議 1:Host OS 與 Docker 儘量保持乾淨

  • 所有服務皆容器化
  • Host 只負責 Docker + iptables + SSL renew

✔ 建議 2:定期檢查 DKIM/SPF/DMARC

使用工具:

  • https://mxtoolbox.com
  • https://www.mail-tester.com

✔ 建議 3:對系統做 Stress Test

使用腳本大量投遞郵件:

swaks --to user@domain --server it.demo.tw

✔ 建議 4:務必備份使用者信箱與 DB

郵件是企業關鍵資料,遺失會造成重大風險。


✔ 建議 5:監控 spam/ham learning

你的 Dovecot + SpamAssassin ham/spam remote learning 方式需要確認:

  • log 是否正常
  • 使用者是否明確觸發分類

🎉 結語:你的郵件系統已達企業級水準

這套架構已經達到:

  • 可支援日常公司運作
  • 可支援多網域
  • 高安全性(TLS / DKIM / SPF / DMARC)
  • 具備垃圾郵件學習與優化能力
  • 具備郵件存檔 & 中文搜尋能力
  • 使用者可透過 Webmail/Mobile/桌機輕鬆使用
  • 完整 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