VPS 运维操作手册(统一端口与分流方案)

本手册整理了 VPS 的统一端口分流架构、组件配置位置、常用操作与故障排查步骤,目标是让团队成员或 AI 协作者快速理解并复用这套部署模式。


架构总览

目标

  • 对外只暴露 443,通过 SNI 将流量分发到不同后端。
  • HTTP 服务统一监听 8443 终止 TLS,避免与代理抢占 443
  • 代理(XrayR / sing-box)仅监听本地回环口 127.0.0.1,由 443 分流进入。
  • 防火墙默认仅放行 80/tcp443/tcp+udpSSH (22/2233),以及可选的 33333/udp(Hysteria2)。

端口路由表

对外入口 路由依据 (SNI) 内部目标 场景说明
443/tcp tz.202221.xyz 127.0.0.1:8443 网站(证书在 8443)
443/tcp www.amazon.com 127.0.0.1:44333 XrayR / VLESS-REALITY
443/tcp www.paypal.com 127.0.0.1:60503 sing-box / VLESS-REALITY #1
443/tcp www.cloudflare.com 127.0.0.1:22222 sing-box / VLESS-REALITY #2
33333/udp 0.0.0.0:33333127.0.0.1 Hysteria2(如开放需放行)
80/tcp openresty http 80→443 跳转 / ACME 验证

stream 分流只读取 SNI,不终止 TLS,因此上游(8443 等)需要自己配置证书。


组件与文件位置

OpenResty / Nginx(运行于容器)

  • 主配置:/usr/local/openresty/nginx/conf/nginx.conf
  • SNI 分流:/usr/local/openresty/nginx/conf/stream.conf
  • 网站配置:/usr/local/openresty/nginx/conf/conf.d/*.conf

使用 1Panel 可直接在「网站/反代」界面修改;命令行方式可通过 pgrep -xo openresty 获取主 PID,并以 /proc/<PID>/root 为容器根查看内部路径。

代理程序

  • XrayR:/etc/XrayR/config.yml(确保 ControllerConfig.ListenIP: 127.0.0.1
  • sing-box:/etc/sing-box/conf/*.json(确保 "listen": "127.0.0.1"

证书示例(Cloudflare Origin CA)

  • 证书:/etc/ssl/cf-origin/tz.202221.xyz.crt
  • 私钥:/etc/ssl/cf-origin/tz.202221.xyz.key(权限建议 600

标准配置复用

1. 在 nginx.conf 全局引入 stream.conf

1
2
3
4
5
6
7
# /usr/local/openresty/nginx/conf/nginx.conf
include /usr/local/openresty/nginx/conf/stream.conf;

http {
include /usr/local/openresty/nginx/conf/conf.d/*.conf;
# 其他 http 配置...
}

2. stream.conf — 443 分流器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# /usr/local/openresty/nginx/conf/stream.conf
stream {
map $ssl_preread_server_name $backend {
tz.202221.xyz web;
www.amazon.com xrayr;
www.paypal.com sb1;
www.cloudflare.com sb2;
default web;
}

upstream web { server 127.0.0.1:8443; }
upstream xrayr { server 127.0.0.1:44333; }
upstream sb1 { server 127.0.0.1:60503; }
upstream sb2 { server 127.0.0.1:22222; }

server {
listen 443;
proxy_pass $backend;
ssl_preread on;
}
}

3. 网站终止 TLS(监听 8443)

1
2
3
4
5
6
7
8
9
10
11
# /usr/local/openresty/nginx/conf/conf.d/tz.202221.xyz.conf
server {
listen 8443 ssl http2;
server_name tz.202221.xyz;

ssl_certificate /etc/ssl/cf-origin/tz.202221.xyz.crt;
ssl_certificate_key /etc/ssl/cf-origin/tz.202221.xyz.key;

include conf.d/snippets/security.conf;
# location / {...}
}

若不使用 Cloudflare,可换成 Let’s Encrypt 证书路径。

4. 常见站点配置模板

  • http://https:// 跳转放在 80 端口配置中。
  • 静态站可直接 root /path/to/site;
  • 动态站需结合后端(PHP-FPM、Node.js 等)设定 proxy_pass

代理节点接入规范

XrayR(VLESS-REALITY 示例)

1
2
3
4
5
6
7
8
9
10
11
12
13
# /etc/XrayR/config.yml
ControllerConfig:
ListenIP: 127.0.0.1
PanelType: "V2board"
# 其他面板配置...
InboundConfig:
VlessOptions:
ListenIP: 127.0.0.1
ListenPort: 44333
ServerName: www.amazon.com
RealityPrivateKey: "..."
ShortIds:
- "0123456789abcdef"

更新完配置后,执行:

1
2
sudo systemctl restart XrayR
sudo systemctl status XrayR --no-pager

sing-box(多节点示例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"inbounds": [
{
"type": "vless",
"tag": "sb1",
"listen": "127.0.0.1",
"listen_port": 60503,
"users": [ { "uuid": "...", "flow": "xtls-rprx-vision" } ],
"tls": {
"enabled": true,
"server_name": "www.paypal.com",
"reality": {
"enabled": true,
"private_key": "...",
"short_id": "abcdef0123456789"
}
}
}
]
}
1
2
sudo systemctl restart sing-box
sudo journalctl -u sing-box -n 50 --no-pager

防火墙与端口开放

UFW 基线

1
2
3
4
5
6
7
8
9
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 443/udp
sudo ufw allow 22/tcp # 或 2233
sudo ufw allow 33333/udp # 若需对外开放 Hysteria2
sudo ufw enable
sudo ufw status verbose

HY2 端口提示

  • Hysteria2 默认 udp/33333;若仅流量回源,可限制源地址或使用 IP 白名单。

日常巡检

  1. 检查 Nginx/OpenResty 状态
1
2
sudo systemctl status openresty --no-pager
sudo nginx -t # 在容器内执行
  1. 确认流量分流
1
2
sudo ss -ltnp | grep 443
sudo tail -f /usr/local/openresty/nginx/logs/error.log
  1. 验证代理监听
1
2
sudo ss -ltnp | grep 127.0.0.1:44333
sudo journalctl -u XrayR -n 50 --no-pager
  1. 证书续期
  • Cloudflare Origin:手动更新。
  • Let’s Encrypt(acme.sh):acme.sh --cron 或系统定时任务。

故障排查流程

1. 网站无法访问

  • curl -vk --resolve tz.202221.xyz:443:服务器IP https://tz.202221.xyz/
  • 检查 8443 站点日志:tail -f /usr/local/openresty/nginx/logs/access.log
  • 确认证书路径与权限。

2. 代理不可用

  • 查看 stream 日志是否分流到对应 upstream。
  • 检查 XrayR / sing-box 日志与状态。
  • 确认客户端 SNI 与 Reality 配置一致。

3. 端口冲突

1
2
sudo lsof -i :443
sudo lsof -i :8443

若发现非预期进程占用,需要调整或停止后重载 Nginx。

4. 防火墙阻断

1
2
sudo ufw status numbered
sudo ufw delete <编号>

常见运维操作

  • 添加新网站:复制 8443 模板 → 修改 server_name 与根目录 → 重新加载 OpenResty。
1
sudo systemctl reload openresty
  • 新增代理节点:在 stream.conf 添加 SNI → 配置对应代理程序监听本地端口 → 重载 OpenResty → 重启代理。

  • 自动化脚本建议:

1
2
sudo /root/scripts/nginx-cert-check.sh
sudo /root/scripts/backup-config.sh

可将脚本放入 cron.dailysystemd timer,形成定期检查。


1Panel 与 SSH 的协作建议

  • 1Panel 适合直观管理网站、证书、Docker 容器。
  • 复杂编辑或批量操作建议使用 SSH,并保留变更记录(如 Git / 脚本)。
  • 如需从容器内查看文件,可使用:
1
nsenter -t $(pgrep -xo openresty) -m sh

附录:向协作者说明的核心要点

  1. 统一入口:所有对外服务经过 443 端口的 SNI 分流,内部服务请监听回环地址。
  2. 证书管理:443 层不终止 TLS,请确保后端服务自备证书(Cloudflare Origin 或 Let’s Encrypt)。
  3. 防火墙策略:端口放行最小化,新增服务需说明开放原因与安全措施。
  4. 日志位置
    • OpenResty:/usr/local/openresty/nginx/logs/
    • XrayR:/var/log/XrayR/
    • sing-box:/var/log/sing-box/
  5. 变更流程:修改配置后,先 nginx -t / sing-box check -c 等语法检查,再重载服务。

将本文分享给 AI 或团队成员,可让他们快速了解 VPS 架构与操作约定,避免端口混乱和配置冲突。