第 7 章 运维与管理

Last Updated: 2026-11-30 14:51 Views: 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 用控制文件实现同语义)
-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;跨大版本前备份配置。

管理口令遗失时由其他管理员在用户管理中重置,或以管理员身份编辑 manager.toml 后重启;动态验证器遗失时删除 TOTP 状态文件(默认 run/manager-totp.json)后重启。

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
← Previous Article 第 6 章 性能与缓存 Next Article → 第 8 章 模块

Comments

No comments yet