反向代理、HTTPS 与健康检查
正确转发 Host 与 X-Forwarded-*,申请证书,并用健康检查确认服务可用。
CMS 进程只监听本机回环地址,公网访问由 Nginx 负责。反向代理配置不正确时,常见表现是后台跳转域名错误、评论 IP 全部相同,或者 HTTPS 一直 502。
反向代理配置
location / {
proxy_pass http://127.0.0.1:23280;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 300s;
proxy_send_timeout 300s;
client_max_body_size 32m;
}
Host 必须保留原始域名,否则 CMS 生成的 canonical 链接和后台跳转可能指向 127.0.0.1。如果前面还有一层 CDN,需要把 CDN 回源网段加入 server.trusted_proxies。
申请 HTTPS
域名解析生效后,在宝塔站点里申请 Let's Encrypt 免费证书,并开启强制 HTTPS。手工使用 acme.sh 时可以这样签发并部署:
/root/.acme.sh/acme.sh --issue -d cms.example.com \
-w /www/wwwroot/java_node_ssl --keylength ec-256
/root/.acme.sh/acme.sh --install-cert -d cms.example.com --ecc \
--key-file /www/server/panel/vhost/cert/djmcms/privkey.pem \
--fullchain-file /www/server/panel/vhost/cert/djmcms/fullchain.pem \
--reloadcmd "nginx -s reload"
健康检查
| 地址 | 用途 |
|---|---|
/health/live | 进程是否存活,不检查数据库 |
/health/ready | 数据库、迁移与依赖是否就绪 |
监控系统建议只采集 /health/ready,并在失败时查看应用日志:
tail -f /www/wwwlogs/go/djmcms.log
tail -f /www/wwwroot/cms.example.com/shared/logs/*.log
常见 502 排查
ss -lntp | grep 23280确认进程还在监听。- 在服务器本地执行
curl http://127.0.0.1:23280/health/ready,区分应用问题与代理问题。 - 检查
runtime.env是否真的被服务加载,数据库密码是否包含特殊字符未转义。 nginx -t与nginx -s reload确认代理配置语法与证书路径。
讨论
发表评论