第 7 章 維運與管理
7.1 命令列
| 參數 | 作用 |
|---|---|
-c <path> |
設定檔,預設 ./conf/wxserver.toml,其次 /etc/wxserver/wxserver.toml |
-w <dir> |
工作目錄,相對路徑的 pid/logs/run/html 以它為基準 |
-t |
僅驗證設定語法與語意 |
-s reload / stop / quit |
向 PID 發送訊號:SIGHUP 重載,SIGTERM 優雅停止(Windows 為控制檔案) |
--service install|uninstall|start|stop|restart|status |
系統服務管理(按平台自動選擇管理器) |
--reset-password [密碼] |
重設管理密碼,留空則產生隨機密碼;多用戶加上 --manager-user |
--reset-totp |
刪除管理後台 TOTP 設定 |
-no-manager |
本次運行停用管理後台 |
-v |
顯示版本 |
wxserver -c /etc/wxserver/wxserver.toml
wxserver -t -c /etc/wxserver/wxserver.toml
wxserver -s reload -c /etc/wxserver/wxserver.toml
wxserver -s stop -c /etc/wxserver/wxserver.toml
wxserver --service install -c /etc/wxserver/wxserver.toml
wxserver --service start -c /etc/wxserver/wxserver.toml
wxserver --service status -c /etc/wxserver/wxserver.toml
配套工具:wxserver-tool htpasswd(bcrypt 密碼條目)、wxserver-tool bench(ab 風格壓測)、wxserver-convert(伺服器設定 / CRS 規則轉 TOML)。
Windows 可攜式部署可用 wxserver.exe -w "C:\WxServer" -c conf\wxserver.toml 固定相對路徑;服務帳戶為一般本機用戶。
7.2 系統服務
服務名稱固定為 wxserver,--service install 按平台自動選擇管理器:
| 平台 | 管理器 | 安裝位置 |
|---|---|---|
| Linux | systemd → OpenRC → SysV 自動偵測 | /etc/systemd/system 或 /etc/init.d |
| FreeBSD | rc.d(daemon(8)) |
/usr/local/etc/rc.d |
| macOS | launchd | /Library/LaunchDaemons |
| Windows | SCM | sc create 註冊 |
--service install 寫入服務定義並註冊,start、stop、restart、status 轉發給對應管理器(Windows 為 SCM 服務模式)。無服務管理器時回退到 PID 檔案 + -s reload|stop|quit 模式。
7.3 重載與停機
-s reload 或管理後台「儲存並重載」會原子替換運行設定(arc-swap),管理 API 不中斷;監聽地址變化時需要重啟進程。設定錯誤時 reload 失敗,舊設定繼續服務。
-s stop 與 -s quit 等價,發送 SIGTERM 後等待現有請求完成,最長 graceful_timeout_sec(預設 30 秒)。多 worker 場景 master 負責轉發訊號給各 worker。
修改
user或監聽端口範圍時,建議安排維護視窗;僅修改站點、WAF、緩存等運行參數可直接熱重載。
7.4 日誌與輪轉
預設日誌位於 /var/log/wxserver/:access.$year-$month-$day.log(combined 格式)、error.log、waf.log、stream.log、manager-login.log、manager-audit.log。路徑支援 $year/$month/$day 變數。
max_size_mb 與 max_backups 控制單檔案大小與保留數量;存取日誌預設脫敏,設定 encrypt = true 後逐行 AES-256-GCM 加密。查看加密日誌透過管理後台日誌頁或 GET /api/logs/{name},服務端自動解密。
7.5 備份與帳戶還原
需要備份 conf/(含 vhost/、certs/、manager.toml、acme.toml、cert.toml)與站點 html/;run/ 下是 PID、共用記憶體與緩存,可不備份。升級時替換二進位檔後先 -t 驗證,再 -s reload;跨大版本前備份設定。
管理密碼遺失用 wxserver --reset-password [新密碼](多用戶加 --manager-user),動態驗證器遺失用 --reset-totp,兩者只改 manager.toml 或 TOTP 狀態檔案;變更後熱重載,僅憑據實際變化時失效舊 token。
7.6 設定與規則轉換(wxserver-convert)
# nginx -> TOML(檔案模式自動展開 include;未給 -out 時寫 stdout)
wxserver-convert -from nginx -in nginx.conf -out conf/wxserver.toml
# nginx -> 主設定 + 每站點一個 conf/vhost/*.toml
wxserver-convert -from nginx -in nginx.conf -out conf/wxserver.toml --split-vhosts conf/vhost
# Apache .htaccess -> 可貼上的 location 片段(檔案名 .htaccess 自動識別)
wxserver-convert -in .htaccess --prefix /blog/ -out /tmp/blog.toml
# OWASP CRS / ModSecurity -> TOML(可多個 -in 合併)
wxserver-convert -from crs -in crs-setup.conf -in rules/*.conf -out conf/waf/crs.toml
--split-vhosts按顯式name或首個domain命名;同域 80⁄443 兩個站點時非 TLS 站點命名為<domain>-http;目錄必須位於主設定所在目錄內。- 未識別的指令以
warning:輸出到 stderr(-quiet靜默),不中斷轉換;輸出先經 TOML 解析驗證再寫入磁碟。 - nginx 要點:
gzip*按層寫入;set_real_ip_from/real_ip_header映射到[modules.http_realip](real_ip_recursive on→mode="recursive");ssl_ciphers只保留 rustls 支援的 AEAD 套件;image_filter resize|crop→type="image"+default_mode;valid_referers+if ($invalid_referer)→referer;add_header ... always→add_headers_always;log_format→access_log.format_template;命名 location 與try_files ... @name直接可用;常見if子集轉為結構化if。
7.7 wxserver-tool
# htpasswd:bcrypt 條目寫入 0600 檔案;互動模式隱藏輸入
wxserver-tool htpasswd -f conf/htpasswd # 互動式
wxserver-tool htpasswd -f conf/htpasswd -u alice -p 'secret'
wxserver-tool htpasswd --check -f conf/htpasswd -u alice -p 'secret'
# bench:ab 風格壓測,輸出 RPS 與 P50/P90/P99 延遲
wxserver-tool bench http://127.0.0.1:8080/ -n 1000 -c 50
wxserver-tool bench http://127.0.0.1:8080/api -n 200 -c 10 -m POST -H 'Content-Type: application/json' -d '{"x":1}'
wxserver-tool bench https://self-signed.local/ -n 50 -c 5 -k # 略過 TLS 驗證
# toml:按 TOML 1.1 解析輸出 JSON(python tomllib 讀不了多行內嵌表,指令碼用它代替)
wxserver-tool toml conf/wxserver.toml # 轉 JSON
wxserver-tool toml --rewrite conf/vhost/example.toml # 就地規範化(JSON 風格 + 保留註解)
# precompress:預先產生 .gz/.br/.zst
wxserver-tool precompress --dir html