把域名指向 VPS,配置支持 WebSocket 的 Nginx 反向代理,并用 certbot 自动签发 Let's Encrypt 证书。附真实命令。
一个 Nginx 挡在所有服务前面:Node 应用、API、仪表盘、游戏面板。再加上会自动续期的免费 Let’s Encrypt 证书,轻松接入 WebSocket。
创建一条 A 记录:`app.example.com → YOUR_SERVER_IP`。等待几分钟,然后在服务器上检查:
getent hosts app.example.com
在 DNS 指向您的服务器之前,certbot 不会签发证书。
apt update && apt install -y nginx certbot python3-certbot-nginx ufw allow 80/tcp ufw allow 443/tcp
cat > /etc/nginx/sites-available/app <<'EOF'
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
EOF
ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/app
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx两行 `Upgrade` 用于启用 WebSocket;较长的读取超时可避免慢速连接(SSE、长轮询、游戏面板)在 60 秒后被切断。请把 `127.0.0.1:3000` 换成您的应用实际监听的端口。
certbot --nginx -d app.example.com --redirect -m [email protected] --agree-tos
certbot 会修改配置、添加 443 块、安装续期定时器并设置 HTTP → HTTPS 跳转。请验证续期是否正常:
certbot renew --dry-run systemctl list-timers | grep certbot
3001 端口上还有另一个应用?复制 server 块,修改 `server_name` 和 `proxy_pass`,再运行一次 certbot。一个 Nginx 可以承载几十个站点:16 欧元的服务器轻松应付。
想要更少的配置?Caddy 两行就能启用 HTTPS:需要精细控制请求头、缓存和超时时,Nginx 更有优势。
nginx -t tail -f /var/log/nginx/error.log curl -I https://app.example.com
502 表示 Nginx 正常,但您的应用已停止或端口不对。404 且页面不对,说明请求被另一个站点块接走了:请检查 `server_name`。
对于普通浏览器访问:需要,因为 Let’s Encrypt 会通过 80 或 443 端口验证您的域名,所以要有独立 IPv4。Buran 的每个服务器套餐都自带一个,任何 VPS 都可以。
是的,并且会通过 certbot 安装的 systemd 定时器每 60 天自动续期。普通网站无需任何付费档。
UDP 流量不行:Nginx 只处理 HTTP。游戏使用自己的端口,请直接在防火墙中开放。
获取带独立 IPv4 的服务器:套餐每月 €12 起。