Skip to content

Nuface Blog

隨意隨手記 Casual Notes

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

郵件系統「日常維運、監控、效能優化」完整指南

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

【Mail Server 系列文:第 19 篇】

在第 1~18 篇中,我們完整打造了一套 企業級 Docker 郵件系統:

  • Postfix(SMTP)
  • Dovecot(IMAP/LMTP)
  • Amavis + SpamAssassin + ClamAV
  • SQL Bayes + TxRep + Remote Learning
  • Piler(信件稽核/備份)
  • Manticore(支援中文全文搜索)
  • Roundcube Webmail
  • HTTPS 反向代理 + SNI + Let’s Encrypt 自動更新

本篇將說明:

如何維運這套系統,讓它穩定、安全、不中斷運作?

內容包含:

  • 日常檢查項目
  • 異常排查 SOP
  • 監控設計
  • 效能最佳化
  • 安全強化建議

一、每日例行檢查(Daily Routine)

以下建議每日巡檢一次(可 5–10 分鐘完成)。


1.1 Postfix 佇列狀態

docker exec postfix mailq

注意:

  • 大量 Deferred → DNS、網路、黑名單問題
  • Queue 增長不減少 → 需檢查內容過濾、SpamAssassin、Amavis

1.2 Amavis / SpamAssassin / ClamAV 運作是否正常

SpamAssassin:

docker logs --since=5m spamassassin

Amavis:

docker logs --since=5m amavis

ClamAV:

echo PING | nc 127.0.0.1 3310

返回:

PONG

代表 OK。


1.3 Dovecot 登入與錯誤事件

docker logs --since=5m dovecot

常見狀況:

問題可能原因
auth failed密碼錯誤 / postfixadmin 未同步
timeout防火牆阻擋 / 連線太慢
quota exceeded需要擴容信箱

1.4 Piler 稽核是否正常寫入

docker logs --since=10m piler

觀察:

  • 是否成功寫入 Manticore
  • 是否成功歸檔 store/

1.5 HTTPS 憑證是否有效

docker exec wwwapp certbot certificates

若快到期(<10 天)須手動跑 renew:

docker exec wwwapp certbot renew

二、每週檢查(Weekly)


2.1 備份是否正常執行

確認:

  • MariaDB dump 是否成功
  • vmail 是否同步完成
  • Piler store 是否持續成長
  • Offsite 備份是否有送出(如 S3、NAS)

2.2 SpamAssassin 規則更新

Amavis 會自動執行 cron,但建議每週手動確認:

docker exec amavis sa-update
docker exec amavis sa-compile

2.3 Manticore 搜尋速度是否正常

執行:

mysql -h manticore -P 9306 -e "SELECT COUNT(*) FROM piler1"

若變慢,建議:

  • 清除 binlog
  • 減少不必要的 indexed columns(已在你系統中最佳化)

三、每月維運(Monthly)


3.1 Dovecot 信箱容量統計

列出大小前 20 名:

du -sh /opt/docker/mail/dovecot/usermail/* | sort -h | tail

若異常暴增 → 檢查 IMAP 客戶端是否重複下載。


3.2 Postfix 黑名單與 RBL 狀況

查是否被列黑:

  • https://mxtoolbox.com/blacklists.aspx
  • https://multirbl.valli.org/

若被黑 → 檢查:

  • 是否有帳號被盜發信
  • SASL 記錄是否出現暴力登入
  • SMTP 外送量是否異常

3.3 Piler 內容稽核搜尋一致性測試

定期抽查 3 個使用者:

  • IMAP 內信件
  • Piler archive

兩者應完全一致。


四、郵件系統效能調校(Performance Tuning)

完整調校通常只需一次,但建議每半年檢查。


4.1 Postfix 調校

(1) 提升併發連線

default_process_limit = 200
smtpd_client_connection_count_limit = 20

(2) 最佳化 DNS 查詢

smtp_dns_support_level = dnssec

4.2 Amavis 調校

增加 child processes:

$max_servers = 4;

建議:每 2 CPU → 1 個 server。


4.3 SpamAssassin 調校

移除不用的 plugins:

  • FreeMail
  • HashBL
  • URIDNSBL(若環境 DNS 慢)

可減少 30–40% CPU 使用。


4.4 ClamAV 調校

設定多核心:

MaxThreads 6

你已設定為 6,符合 4–8 core 機器。


4.5 Dovecot 調校

利用 I/O cache:

mail_prefetch_count = 20

加速 index:

mail_index_lazy_writing = yes

這能明顯加速 Roundcube 瀏覽。


4.6 Manticore 調校

你使用的是:

  • RT 表格
  • ngram 中文分詞
  • ICU tokenizer

建議保持:

rt_flush_period = 300

若 mail archive 量變大 → 調為 60。


五、完整監控架構(Monitoring)

可以用:

  • Prometheus + Grafana
  • Zabbix
  • netdata
  • 或自建 N8N webhook

監控項目如下:


5.1 SMTP 監控

指標目的
Mail Queue 大小過濾器卡住?DNS 異常?
退信率RBL、DNS、設定錯誤
25/587 port 連通ISP 是否阻擋

5.2 IMAP 監控

指標目的
登入延遲Dovecot I/O 問題
不正常登入密碼外洩警訊
Session 數量是否被攻擊

5.3 Amavis / SpamAssassin

指標目的
每分鐘掃描量併發量是否足夠
CPU 使用率規則太多?DB 連線慢?

5.4 ClamAV

指標目的
clamd PING 延遲過載會 >200ms
freshclam 更新病毒庫更新狀態

5.5 Piler / Manticore

指標目的
每小時寫入量觀察公司郵件量
搜尋延遲Manticore 負載是否變高

六、安全維運(Security Operations)


6.1 防暴力登入(SASL / IMAP)

Dovecot logs → 密碼暴力破解遍地都是,建議:

  • Fail2ban
  • 或 Nginx rate limit
  • 或 cloud firewall

6.2 SPF / DKIM / DMARC 檢查

重大變更後,必須做一次:

https://mxtoolbox.com/dmarc.aspx

6.3 TLS score 測試(A+)

https://www.ssllabs.com/ssltest/

6.4 定期修改管理者密碼(PostfixAdmin)

你已經實作:setup → 初始密碼替換 → 刪除 config.local 掛載
屬於安全最佳實務。


七、異常排查完整 SOP(Troubleshooting SOP)


7.1 SMTP 發不出去

檢查順序:

  1. mailq → queue 是否卡住
  2. Postfix log → DNS 問題?
  3. Amavis log → 卡在病毒/垃圾郵件掃描?
  4. SPF / DMARC → 對方退信?
  5. RBL 黑名單?

7.2 Webmail 連不上 IMAP

通常是 firewall:

iptables -I INPUT -s 172.18.0.0/16 -d 172.18.0.1 -j ACCEPT

常見症狀:

  • Roundcube: “IMAP connection broken”
  • openssl s_client:unknown protocol

→ 99% firewall。


7.3 Amavis 停止處理信件

檢查:

docker logs amavis

常見錯誤:

  • ClamAV 無回應
  • SA SQL 無法連線
  • tmp 目錄滿了

7.4 Piler 搜尋變慢

三個主要原因:

  1. Manticore index 大幅增長
  2. Piler store fragmentation
  3. 不完整的 ngram 中文索引

八、文章總結

經過第 19 篇,你已具備完整郵件系統維運能力:

✔ 日常巡檢

✔ 異常排查

✔ 效能最佳化

✔ 監控架構

✔ 安全強化

✔ 系統穩定度提升

到這裡,你的郵件系統不只是能用,而是達到 企業級穩定可維運。

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