Skip to content

接入 Hub 统一网关 HTTPS 配置指南 ​

本文档指导如何在已有的 Hub 统一网关环境中,为 Neat 项目(包含 Web 端和 Admin 端)配置自动续期的 HTTPS 证书。

1. 前置条件 ​

  • Hub 服务正常运行:Hub 的 Nginx 和 FRP 服务已启动。
  • Neat 已接入 Hub (HTTP):已完成《Neat 服务接入 Hub 统一网关集成指南》中的 Hub 接入步骤,可以通过 http://<web_domain> 正常访问。
  • DNS 解析正确:<web_domain> 和 <admin_domain> 均已解析到 Hub 服务器的公网 IP。
  • acme.sh 已安装:Hub 服务器上已安装 acme.sh 工具(Hub 项目标配)。

2. 确认 ACME 验证环境 ​

在申请证书前,必须确保 Hub Nginx 能正确响应 Let's Encrypt 的验证请求。

2.1 检查 Nginx 配置 ​

确保 hub/nginx/conf/<project>.conf 的 80 端口配置中包含以下验证块(如果没有请添加):

nginx
server {
    listen 80;
    server_name <web_domain> <admin_domain>;

    # ACME 验证专用路径
    location ^~ /.well-known/acme-challenge/ {
        # 对应 Hub docker-compose 挂载的 static 目录
        alias /usr/share/nginx/html/.well-known/acme-challenge/;
        try_files $uri =404;
    }

    # ... 其他原有配置 ...
}

注:Hub 的 Nginx 容器通常将宿主机的 ./nginx/static/ 挂载为容器内的 /usr/share/nginx/html/。

2.2 重载 Nginx ​

如果修改了配置,执行以下命令重载:

bash
cd ~/hub
docker compose exec nginx nginx -s reload

3. 申请 SSL 证书 ​

我们将为两个域名申请同一个证书(SAN 多域名证书)。

3.1 执行申请命令 ​

在 Hub 项目根目录(通常是 ~/hub)执行:

bash
# 确保在 hub 根目录,因为 --webroot 指向的是相对路径 ./nginx/static/
cd ~/hub

acme.sh --issue \
  -d <web_domain> \
  -d <admin_domain> \
  --webroot ./nginx/static/ \
  --server letsencrypt

# 比如 neat.hongjy.cn admin.neat.hongjy.cn
acme.sh --issue \
  -d neat.hongjy.cn \
  -d admin.neat.hongjy.cn \
  --webroot ./nginx/static/ \
  --server letsencrypt \
  --debug
  • 成功标志:看到 "Cert success" 提示,且显示证书文件路径。
  • 失败排查:如果报错,请检查 DNS 解析是否生效,以及 http://<web_domain>/.well-known/acme-challenge/test 是否能访问(可手动创建文件测试)。

4. 安装证书 ​

将申请到的证书安装到 Nginx 挂载的 SSL 目录,并配置自动重载钩子。

bash
# 确保 ssl 目录存在
mkdir -p ./nginx/ssl/

# 安装证书 (将两个域名合并到一个证书文件)
acme.sh --install-cert \
  -d <web_domain> \
  -d <admin_domain> \
  --key-file       ./nginx/ssl/<project>.key  \
  --fullchain-file ./nginx/ssl/<project>.crt  \
  --reloadcmd     "docker exec hub-nginx nginx -s reload"

## 具体实例
acme.sh --install-cert \
  -d neat.hongjy.cn \
  -d admin.neat.hongjy.cn \
  --key-file       ./nginx/ssl/neat.hongjy.cn.key  \
  --fullchain-file ./nginx/ssl/neat.hongjy.cn.crt  \
  --reloadcmd     "docker exec hub-nginx nginx -s reload"
  • ./nginx/ssl/ 是宿主机目录,对应容器内的 /etc/nginx/ssl/。

5. 配置 Nginx 启用 HTTPS ​

修改 hub/nginx/conf/<project>.conf,添加 443 端口监听并配置 SSL。

5.1 推荐配置内容 ​

bash
# 反向代理配置
# 包含 Web 端与 Admin 端的域名
# 上游服务:gateway 容器 (已加入 hub_network)

# ==========================================
# 1. HTTP -> HTTPS 重定向
# ==========================================
server {
    listen 80;
    server_name neat.hongjy.cn admin.neat.hongjy.cn;

    # ACME 验证专用路径 (必须保留,用于证书续期)
    location ^~ /.well-known/acme-challenge/ {
        # 对应 Hub docker-compose 挂载的 static 目录
        alias /usr/share/nginx/html/.well-known/acme-challenge/;
        try_files $uri =404;
    }

    # 日志配置
    access_log /var/log/nginx/neat_access.log;
    error_log /var/log/nginx/neat_error.log;

    # 强制跳转 HTTPS
    location / {
        return 301 https://$host$request_uri;
    }
}

# ==========================================
# 2. HTTPS 服务配置
# ==========================================
server {
    listen 443 ssl;
    server_name neat.hongjy.cn admin.neat.hongjy.cn;

    # SSL 证书路径 (容器内路径)
    # 请确保已通过 acme.sh --install-cert 命令将证书安装到 hub/nginx/ssl/ 目录
    ssl_certificate /etc/nginx/ssl/neat.hongjy.cn.crt;
    ssl_certificate_key /etc/nginx/ssl/neat.hongjy.cn.key;

    # SSL 优化配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    # 日志配置
    access_log /var/log/nginx/neat_access.log;
    error_log /var/log/nginx/neat_error.log;

    # 上传体积限制
    client_max_body_size 50m;

    # 全局 UTF-8 编码
    charset utf-8;

    # 运行时DNS解析,避免容器IP变化导致502
    resolver 127.0.0.11 ipv6=off valid=30s;
    set $app_up neat-gateway;

    # 反向代理到 gateway 容器
    location / {
        proxy_pass http://$app_up:80;
        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 https; # 告诉后端是 HTTPS
        proxy_set_header X-Forwarded-Host $host;

        # 超时设置
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;

        # 缓冲设置
        proxy_buffering off;
        proxy_request_buffering off;
    }
}

5.2 重载生效 ​

bash
docker compose exec nginx nginx -s reload

6. 验证 ​

  1. 访问 https://<web_domain>。
  2. 访问 https://<admin_domain>。
  3. 检查浏览器地址栏锁图标,确认证书由 Let's Encrypt 签发。
  4. acme.sh 会自动配置 Cron 任务,每 60 天自动检测并续期证书。

6.1 证书续期命令 ​

bash
# 1. 查看当前用户的 Cron 任务
crontab -l
# 2. 编辑当前用户的 Cron 任务
crontab -e
# 3. 删除当前用户的所有 Cron 任务(慎用)
crontab -r
# 4. 检查 Cron 服务状态
systemctl status cron
# 5. 启动 Cron 服务
sudo systemctl start cron
# 6. 启用 Cron 服务开机自启
sudo systemctl enable cron
# 7. 查看 Cron 日志
grep CRON /var/log/syslog

# 2. 看证书剩余天数
acme.sh --list
# 2. 手动检查续期逻辑(不真正续)
acme.sh --cron --home ~/.acme.sh
# 3. 手动强制续期测试
acme.sh --renew -d <web_domain> --force
# 4. 强制续期所有证书。万一自动服务挂了  不推荐日常用  这是重新签发 极容易撞 Let’s Encrypt 限流 没必要
acme.sh --renew-all --force --debug
# 安装 / 重新安装证书到指定路径
acme.sh --install-cert \
  -d <web_domain> \
  --key-file       /path/to/<project>.key \
  --fullchain-file /path/to/<project>.crt \
  --reloadcmd     "nginx -s reload"
# 查看 acme.sh 日志
tail -n 50 ~/.acme.sh/acme.sh.log

# 4. 看证书真实有效期
openssl x509 -in ~/<your-project>/deploy/nginx/ssl/<project>.crt -noout -dates

7. 常见问题 (FAQ) ​

Q: FRP 需要配置 443 端口吗? A: 不需要额外配置。 只要 Hub 的 FRP 已经配置了通用的 HTTPS 穿透(通常是 [photoprism-https] 或 [hub-https] 监听 remote_port 443 并转发到 local_port 443),所有通过 Hub Nginx 代理的 HTTPS 域名都可以共用这一个通道。Nginx 会根据 SNI (Server Name Indication) 自动区分域名。

Q: 证书续期失败怎么办? A: 手动强制续期测试:

bash
acme.sh --renew -d <web_domain> --force

查看日志排查问题 ​

7. 常见问题 (FAQ) ​

Q: FRP 需要配置 443 端口吗? A: 不需要额外配置。 只要 Hub 的 FRP 已经配置了通用的 HTTPS 穿透(通常是 [photoprism-https] 或 [hub-https] 监听 remote_port 443 并转发到 local_port 443),所有通过 Hub Nginx 代理的 HTTPS 域名都可以共用这一个通道。Nginx 会根据 SNI (Server Name Indication) 自动区分域名。

Q: 证书续期失败怎么办? A: 手动强制续期测试:

bash
acme.sh --renew -d <web_domain> --force

查看日志排查问题。