跳到主要内容

用 HAProxy 配 HTTPS

HAProxy 适合高连接数、负载均衡和更细粒度流量控制场景。它可以作为 OPL 数据空间前面的 TLS 终止层和反向代理。

安装

RedHat 系:

sudo dnf install haproxy certbot openssl -y

Debian / Ubuntu:

sudo apt install haproxy certbot openssl -y

基本思路

  • 让 HAProxy 监听 80443
  • /.well-known/acme-challenge/ 放行,让 Let's Encrypt 验证域名
  • 把目标域名或路径转发到 OPL 数据空间
  • 正确透传 X-Forwarded-Proto、WebSocket 相关头和较长超时

重要提醒

  • 需要对外开放 80 / 443
  • DNS 必须指向 HAProxy 所在机器
  • 如果遇到 WebSocket 假死,可尝试强制客户端回退到 HTTP/1.1

证书申请

先生成一个临时自签名证书供 HAProxy 启动:

openssl req -x509 -newkey rsa:2048 -keyout /tmp/haproxy.key -out /tmp/haproxy.crt -days 3650 -nodes -subj "/CN=localhost"
cat /tmp/haproxy.crt /tmp/haproxy.key > /etc/haproxy/certs/haproxy.pem

注册 certbot:

certbot register --agree-tos --email your@email.com --non-interactive

申请证书:

certbot certonly -n --standalone --preferred-challenges http --http-01-port-8688 -d yourdomain.com

最后把完整链和私钥合并成 HAProxy 可读取的 .pem 后重启 HAProxy。