VPS 单端口多用途部署总结(Trojan + Nginx SNI 分流)
VPS 单端口多用途部署总结(Trojan + Nginx SNI 分流)
> 适用场景:只有一台 VPS、一个 443 端口,同时需要
> 1. Trojan 翻墙
> 2. 正常网站 / 任何后端项目
> 3. 后续可无限扩展子域名、邮件、CI 等 TLS 服务
---
## 📌 最终架构图(逻辑)
```text
Browser ──443──> Nginx (stream) ──SNI──┬─ troy.yourdomain.com ──> Trojan:9443
└─ *.other.domain ──> Nginx:8443 (https)
-
443 端口由 Nginx 独占,通过
stream模块按 SNI 转发 -
Trojan 容器只暴露 9443 给内部网络
-
普通网站/项目运行在 Nginx 8443(内部),可反代任意后端
✅ 已完成清单
| 组件 | 状态 | 备注 |
| ----------- | ---------------------------------------------- | ----------------------------- |
| Docker 网络 | ✅ proxy bridge 网络 | 容器互通 |
| Trojan 容器 | ✅ 监听 9443 | 配置回落到 nginx:80 |
| Nginx 容器 | ✅ 监听 80/443 | stream 分流 + 内部 8443 https |
| 健康检查 | ✅ curl -fk https://localhost:8443/index.html | 日志干净 |
| 证书 | ✅ 使用 Let's Encrypt yourdomain.com | 路径正确 |
🔧 目录结构(保持即可)
/root/
├── nginx/ # nginx 配置主目录
│ ├── nginx.conf # 含 stream 分流 + 8443 https
│ ├── conf.d/ # 可放其他站点配置
│ └── html/
│ └── index.html # 健康检查用
└── trojan-deploy/
├── docker-compose.yml # 一键启停
├── config.json # Trojan 服务端配置
└── ssl/ # 证书挂载点(实际指向 /etc/letsencrypt)
🚀 一键启停
cd /root/trojan-deploy
docker compose down && docker compose up -d
🎯 Trojan 客户端填写示例
| 字段 | 值 |
| ---------- | ------------------------------ |
| 地址 | yourdomain.com |
| 端口 | 443 |
| 密码 | 见 trojan-deploy/config.json |
| SNI | troy.yourdomain.com |
| 允许不安全 | 关闭 |
🌐 后续部署真实项目的三种姿势
① 直接反代(最简单)
编辑 /root/nginx/conf.d/yourproject.conf(会被自动 include):
server {
listen 8443 ssl;
http2 on;
server_name project.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
location / {
proxy_pass http://your-backend:3000; # Docker 网络内域名或宿主机 IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
重启 nginx 容器生效:
docker exec nginx nginx -s reload
② Docker Compose 一起起
把业务容器也加入 proxy 网络,compose 片段示例:
your-backend:
image: node:18-alpine
working_dir: /app
volumes:
- ./project:/app
command: npm start
networks:
- proxy
然后在 nginx 反代 http://your-backend:3000 即可。
③ 新增子域名(重复利用证书)
-
证书已通配或多域名:
certbot certonly --nginx -d new.yourdomain.com证书会续到同一目录
/etc/letsencrypt/live/yourdomain.com,无需改路径。 -
新增 server 块(仍监听 8443):
server { listen 8443 ssl; http2 on; server_name new.yourdomain.com; ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; location / { ... } } -
重载配置:
docker exec nginx nginx -s reload
🔐 证书自动续期(推荐)
# 系统 systemd 计时器(Ubuntu 22.04+ 已自带)
# 确认钩子是否有效
certbot renew --dry-run
续期成功后钩子会自动 nginx -s reload,无需手动重启容器。
📊 常用运维命令
| 场景 | 命令 |
| --------------- | ------------------------------------------------ |
| 看实时日志 | docker logs -f nginx / docker logs -f trojan |
| 进入容器 | docker exec -it nginx bash |
| 重载 nginx 配置 | docker exec nginx nginx -s reload |
| 更新镜像 | docker compose pull && docker compose up -d |
🎉 结语
你现在拥有:
-
单端口 443 同时跑 Trojan 翻墙 + 任意数量网站/项目
-
新增子域名只需加 server 块,无需再开新端口
-
证书自动续期,一键启停,日志干净
Enjoy your single-port VPS!
有任何新需求(邮件服务器、CI、CDN 回源)都可继续在这条 443 上扩展 🔧
---
*本文由[萧兮的博客](https://www.20010515.xyz)原创发布,欢迎转载,转载务必保留原文链接。*
**萧兮的博客**:[https://www.20010515.xyz](https://www.20010515.xyz) · 原文:[https://www.20010515.xyz/posts/0fa41822-51a8-4fb5-bd93-da96ffdc1a97](https://www.20010515.xyz/posts/0fa41822-51a8-4fb5-bd93-da96ffdc1a97)