Ubuntu 24.04 部署 GOST HTTPS 代理
一台轻量的 Ubuntu 24.04 云主机上,只需运行一个 GOST 2.11.5 进程,就能同时监听多个 HTTPS 代理端口——不同端口可按需转发到各自独立的上游代理,或直接从本机出站。本文整理了一套偏生产向的最小部署方案:
单进程多出口
Routes 数组在同一进程内隔离多条链路,免去维护多份进程与服务单元的负担。私钥零权限泄漏
LoadCredential 按需注入证书与私钥,GOST 以无特权账号运行,无需放宽 Let's Encrypt 目录权限。证书自动续期
小磁盘友好
systemd-journald 设定 200 MB 日志上限,避免低配云盘被日志撑满。架构概览
客户端通过标准 HTTPS Proxy(TLS)连接不同端口,GOST 根据端口路由到指定上游或本机直出:
客户端 (HTTPS Proxy / TLS)
├── :10443 ──▶ GOST (gost 用户) ──▶ 上游节点 A (upstream.example.com:10001)
├── :10444 ──▶ GOST (gost 用户) ──▶ 上游节点 B (upstream.example.com:10002)
├── :10445 ──▶ GOST (gost 用户) ──▶ 上游节点 C (upstream.example.com:10003)
└── :10446 ──▶ GOST (gost 用户) ──▶ 本机直接出站
环境与前置准备
开始部署前,先确认以下环境与网络条件已就绪:
| 项目 | 要求与说明 |
|---|---|
| 操作系统 | Ubuntu 24.04 LTS Minimal(x86_64 / amd64) |
| 域名解析 | 为 proxy.example.com 添加 A 记录 指向服务器公网 IP;仅当服务器也通过 IPv6 提供服务时才添加 AAAA 记录 |
入站 80/tcp | 供 Certbot HTTP-01 验证签发与自动续期证书 |
入站 10443-10446/tcp | GOST HTTPS 代理监听端口,建议在安全组中仅对可信客户端源 IP 放行 |
部署步骤
安装基础依赖与 GOST
Ubuntu Minimal 镜像默认缺少常用网络诊断与证书工具,初始化时一并补齐,随后下载静态编译的 GOST 2.11.5 二进制文件,并创建无登录权限的专用系统用户 gost 与配置目录:
# 1. 安装基础工具与 Certbot
sudo apt update
sudo apt install -y ca-certificates curl dnsutils python3 certbot
# 2. 下载并安装 GOST 2.11.5
curl -fSL -o gost-2.11.5.gz \
https://github.com/ginuerzh/gost/releases/download/v2.11.5/gost-linux-amd64-2.11.5.gz &&
gzip -dc gost-2.11.5.gz > gost-2.11.5 &&
sudo install -o root -g root -m 0755 gost-2.11.5 /usr/local/bin/gost &&
rm -f gost-2.11.5.gz gost-2.11.5 &&
/usr/local/bin/gost -V
# 3. 创建无登录权限的运行用户与配置目录
sudo adduser --system --group --no-create-home gost
sudo install -d -o root -g gost -m 0750 /etc/gost
签发 TLS 证书并配置自动续期
签发前先确认域名解析已生效,且 80 端口没有被 Nginx 或其他进程占用:
dig +short A proxy.example.com
sudo ss -lntp 'sport = :80'
确认无冲突后,用 Certbot Standalone 模式签发证书,启用系统自带的续期定时器,并添加续期成功后的重启钩子:
# 签发证书并核对证书信息
sudo certbot certonly --standalone --cert-name proxy.example.com -d proxy.example.com
sudo certbot certificates
# 启用定时续期任务
sudo systemctl enable --now certbot.timer
# 证书更新后自动重启 gost.service
sudo tee /etc/letsencrypt/renewal-hooks/deploy/50-restart-gost >/dev/null << 'EOF'
#!/bin/sh
set -eu
if [ "${RENEWED_LINEAGE:-}" = /etc/letsencrypt/live/proxy.example.com ]; then
systemctl try-restart gost.service
fi
EOF
sudo chmod 0755 /etc/letsencrypt/renewal-hooks/deploy/50-restart-gost
编写多端口路由与客户端鉴权配置
GOST 通过 Routes 数组在单进程内声明多条转发链路:配置了 ChainNodes 的端口经指定上游出站,未配置则由本机直接出站。
/run/credentials/gost.service/ 下的证书路径。systemd 在启动时会通过 LoadCredential 将证书和私钥以只读方式注入该目录,无需改动 /etc/letsencrypt/ 的原始属主与权限。{
"Debug": false,
"Routes": [
{
"ServeNodes": [
"https://0.0.0.0:10443?cert=/run/credentials/gost.service/fullchain.pem&key=/run/credentials/gost.service/privkey.pem&secrets=/etc/gost/clients.txt"
],
"ChainNodes": [
"http://UPSTREAM_USER:UPSTREAM_PASS@upstream.example.com:10001"
]
},
{
"ServeNodes": [
"https://0.0.0.0:10444?cert=/run/credentials/gost.service/fullchain.pem&key=/run/credentials/gost.service/privkey.pem&secrets=/etc/gost/clients.txt"
],
"ChainNodes": [
"http://UPSTREAM_USER:UPSTREAM_PASS@upstream.example.com:10002"
]
},
{
"ServeNodes": [
"https://0.0.0.0:10445?cert=/run/credentials/gost.service/fullchain.pem&key=/run/credentials/gost.service/privkey.pem&secrets=/etc/gost/clients.txt"
],
"ChainNodes": [
"http://UPSTREAM_USER:UPSTREAM_PASS@upstream.example.com:10003"
]
},
{
"ServeNodes": [
"https://0.0.0.0:10446?cert=/run/credentials/gost.service/fullchain.pem&key=/run/credentials/gost.service/privkey.pem&secrets=/etc/gost/clients.txt"
]
}
]
}
# 每行一个账号,用户名与密码之间用空格分隔
CLIENT_USER CLIENT_PASSWORD
- 上游用户名或密码若包含 URL 保留字符(如
@、:),需做百分号编码(例如@写成%40)。 - 示例中的
http://上游仅适合可信内网或专线环境;如果上游支持 HTTPS 且经公网传输,在 GOST v2 中应改为https://并附加?secure=true以校验证书。
先创建文件并锁定权限,再写入实际配置,最后用 Python 校验 JSON 语法:
sudo touch /etc/gost/gost.json /etc/gost/clients.txt
sudo chown root:gost /etc/gost/gost.json /etc/gost/clients.txt
sudo chmod 0640 /etc/gost/gost.json /etc/gost/clients.txt
sudoedit /etc/gost/gost.json /etc/gost/clients.txt
sudo python3 -m json.tool /etc/gost/gost.json >/dev/null && echo "JSON 语法正常"
配置 systemd 沙箱与凭据隔离
由于监听端口均大于 1024,服务不需要任何 Linux capabilities,可以直接清空 CapabilityBoundingSet,再配合 ProtectSystem=strict 与 LoadCredential 将进程隔离在最小权限边界内:
sudo tee /etc/systemd/system/gost.service >/dev/null << 'EOF'
[Unit]
Description=GOST HTTPS Proxy Service
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=gost
Group=gost
WorkingDirectory=/etc/gost
LoadCredential=fullchain.pem:/etc/letsencrypt/live/proxy.example.com/fullchain.pem
LoadCredential=privkey.pem:/etc/letsencrypt/live/proxy.example.com/privkey.pem
ExecStart=/usr/local/bin/gost -C /etc/gost/gost.json
Restart=on-failure
RestartSec=5s
NoNewPrivileges=true
CapabilityBoundingSet=
PrivateTmp=true
ProtectHome=true
ProtectSystem=strict
[Install]
WantedBy=multi-user.target
EOF
sudo systemd-analyze verify /etc/systemd/system/gost.service
sudo systemctl daemon-reload
sudo systemctl enable --now gost.service
sudo ss -lntp | grep gost
确认四个端口均处于 LISTEN 状态后,模拟一次证书续期验证流程是否畅通(--dry-run 默认不会触发 deploy hook):
sudo certbot renew --cert-name proxy.example.com --dry-run
限制日志空间并完成本地验收
对于 10–20 GB 的小容量系统盘,建议顺手为 systemd-journald 设定持久化日志上限,并清理已归档日志:
sudo mkdir -p /etc/systemd/journald.conf.d
printf "[Journal]\nSystemMaxUse=200M\n" | sudo tee /etc/systemd/journald.conf.d/size.conf >/dev/null
sudo systemctl restart systemd-journald
sudo journalctl --vacuum-size=200M && df -h /
最后回到本地终端,逐端口验证 TLS 握手、鉴权拦截与最终出口 IP:
for port in 10443 10444 10445 10446; do
printf 'Port %s: ' "$port"
curl -fsS --max-time 20 --noproxy '' \
--proxy "https://proxy.example.com:${port}" \
--proxy-user "CLIENT_USER:CLIENT_PASSWORD" \
https://ifconfig.me
printf '\n'
done
curl -i --max-time 20 --noproxy '' \
--proxy "https://proxy.example.com:10443" \
https://ifconfig.me
# 预期返回:HTTP/1.1 407 Proxy Authentication Required
日常维护与故障排查
常用维护命令
sudo systemctl status gost.service
sudo journalctl -u gost.service -n 50 --no-pager
sudo python3 -m json.tool /etc/gost/gost.json >/dev/null && \
sudo systemctl restart gost.service
sudo certbot certificates
常见报错速查
| 现象 / 报错 | 常见原因 | 解决方法 |
|---|---|---|
open .../fullchain.pem: permission denied | gost.json 中误填了 /etc/letsencrypt/live/ 原始路径 | 改为 /run/credentials/gost.service/ 并确认 gost.service 已配置 LoadCredential |
ss -lntp | grep gost 无输出 | gost.json 存在语法错误,或以 root 编辑后权限丢失 | 用 python3 -m json.tool 校验格式,并执行 chown root:gost 修复属主 |
| 客户端连接超时 | 安全组未放行端口,或 Cloudflare 误开启橙云代理 | 检查云厂商防火墙入站规则,确保代理子域为仅 DNS(灰云) |
返回 407 或上游连接失败 | 客户端账密不匹配,或上游密码含 @、: 等未转义字符 | 核对 /etc/gost/clients.txt,并对上游 URL 中的保留字符做百分号编码 |