dun 和网站共存:nginx 占着 443,让 Reality 也走 443
作者:Joey · 发布:2026-11-28 · 分类:节点搭建 · 原文:https://joeyblog.net/dun-nginx-443.html
VPS 上已经跑着 nginx 网站,还能装 dun 吗?默认就能共存。想让 Reality 也走 443,用 nginx stream 按 SNI 分流,四步改完,三个坑和还原方法全部实测。
评论区和 GitHub 上问得最多的一个问题:机器上已经跑着网站了,还能不能装 dun,会不会把网站搞坏。
先说结论:能共存,而且默认就能。dun 发现 443 被占,会自己换一个随机端口,网站一点不受影响。这篇在一台真机器上从头走一遍:默认共存是什么样,想让节点也走 443 要怎么改,中间会踩哪几个坑,最后怎么还原。
项目地址:https://github.com/byJoey/dun
教程原文:https://github.com/byJoey/dun/blob/main/docs/nginx-443.md
视频版:
下面的终端输出都是在搬瓦工洛杉矶的测试机上真实跑出来的,系统 Ubuntu 24.04,nginx 1.24,dun 1.1.5。访客用的是新加坡另一台机器。服务器 IP 写成 203.0.113.10,访客 IP 写成 198.51.100.20,域名写成 example.com,密钥打了码。
默认就能共存
装 dun 之前,80 和 443 都是 nginx 的,上面跑着一个博客,证书是 Let’s Encrypt 签的:
root@vps:~# ss -ltnp | grep -E ':(80|443) '
LISTEN 0.0.0.0:80 nginx
LISTEN 0.0.0.0:443 nginx
LISTEN [::]:80 nginx
LISTEN [::]:443 nginx
一键安装,菜单选 1 只装 Reality:
bash <(curl -fsSL https://github.com/byJoey/dun/releases/latest/download/install.sh)
它先查端口,发现 443 被 nginx 占着,自动换了一个随机端口:
请选择: 1
· 安装 VLESS + Vision + Reality(偷自己)
! 443 被 nginx 占用
Reality 监听端口 [36804]:
凭据类型 [1]: 2
✓ 解析 2gjb4b.ncv5u.example.com → 203.0.113.10(仅 DNS)
✓ 证书已签发
✓ Reality 偷自己 安装完成
凭据用的是 API Token,整个安装 27 秒。装完从新加坡看,网站照常,访客 IP 也对;节点在 36804 上,打开是伪装登录页:
root@client:~# curl -s https://myblog.example.com/ip
你的 IP: 198.51.100.20
root@client:~# curl -sI https://2gjb4b.ncv5u.example.com:36804/ | head -1
HTTP/2 200
dun 的文件、服务、sing-box 都是自己单独一份,和网站互不干扰。要注意的就两件事:端口别撞;节点用一个新的子域名,别覆盖网站原来那条解析。
两个小遗憾
这样共存有两个小遗憾。第一,节点链接里带着这个端口:
vless://••••••••-••••-••••-••••-••••••••••••@203.0.113.10:36804?encryption=none&flow=xtls-…
第二,就是 issue 里有人问的:直接打开节点域名,什么都出不来。不带端口就是走 443,443 门口只有 nginx,它不认识节点域名,递出来的是博客的证书,对不上:
root@client:~# curl -sv https://2gjb4b.ncv5u.example.com/ 2>&1 | grep -E "subject:|SSL: no alternative"
* subject: CN=myblog.example.com
* SSL: no alternative certificate subject name matches target host name '2gjb4b.ncv5u.example.com'
伪装站其实一直在 Reality 自己的端口上,带上 :36804 就能打开。
这不影响使用,客户端连 36804 照样上网。不在意端口的话,到这里就可以了。 在意的话,让节点也走 443 有两个好处:443 是最普通的 HTTPS 端口,更像一个正常网站;打开节点域名也能看到一个证书正规的网站,这正是偷自己想要的效果。
dun 不会自动改你的 nginx。每个人的配置千差万别,有的还是面板生成的,面板一重新生成改动就没了;改坏了,挂的是你的网站。所以下面每一步都是手动的,看得到,也能还原。
思路:在 443 门口看一眼 SNI
TLS 握手的第一个包里,明文写着要访问的域名,也就是 SNI。nginx 的 stream 模块配上 ssl_preread,可以只看这一眼,不解密,然后把整条连接原封不动转出去。Reality 收到的握手和客户端直连时一模一样。

客户端 ──443──▶ nginx stream
├─ SNI 是节点域名 ──▶ 127.0.0.1:8444 ──▶ Reality
└─ 其他 ───────────▶ 127.0.0.1:8443 ──▶ 你原来的网站
网站从 443 挪到本机的 8443,对外还是 443。中间多了一跳,网站看到的来源会变成 127.0.0.1,所以要用 proxy_protocol 把访客的真实 IP 带过去。去 Reality 的那一跳再经过 8444 把这个头摘掉,因为 sing-box 不认它。8443、8444 只是例子,换成机器上空着的端口都行。
第 0 步:备份,记下两样东西
cp -a /etc/nginx /etc/nginx.bak-dun
dun links | grep vless
sni= 后面是节点域名。Reality 的监听端口在 /etc/dun/state.json 里 "reality" 那段的 "port",这台是 36804。
第 1 步:stream 模块
root@vps:~# nginx -V 2>&1 | tr ' ' '\n' | grep -E -- '^--with-stream(=dynamic)?$'
--with-stream=dynamic
- 输出
--with-stream:编进去了,跳过 - 输出
--with-stream=dynamic:要另外装。Debian、Ubuntu 用apt install libnginx-mod-stream,CentOS、Rocky、Alma 用dnf install nginx-mod-stream,Alpine 用apk add nginx-mod-stream - 什么都没有:这个 nginx 不带 stream,没法这样做,Reality 就留在随机端口
这台是 dynamic。演示时我故意先跳过这一步,后面会看到漏了是什么报错。
第 2 步:网站挪到 8443
找出所有监听 443 的地方:
root@vps:~# grep -rn 'listen.*443' /etc/nginx/
/etc/nginx/sites-available/myblog:10: listen 443 ssl http2;
/etc/nginx/sites-available/myblog:11: listen [::]:443 ssl http2;
/etc/nginx/sites-available/default:27: # listen 443 ssl default_server;
/etc/nginx/sites-available/default:28: # listen [::]:443 ssl default_server;
sites-enabled 里一般是软链接,grep -r 不跟进去,所以直接搜整个 /etc/nginx/。每个 server 里 TCP 的 443 都这样改:
# 改前
listen 443 ssl http2;
listen [::]:443 ssl http2;
# 改后
listen 127.0.0.1:8443 ssl http2 proxy_protocol;
- IPv6 那行删掉,对外的 v4、v6 都由下一步的 stream 接
- nginx 1.25.1 以前,
http2只能写在 listen 上,要跟着抄过去;default_server、reuseport也照抄 listen 443 quic这类 HTTP/3 的行不用改,那是 UDP,不冲突
改完:
root@vps:~# grep -n listen /etc/nginx/sites-enabled/myblog
2: listen 80;
3: listen [::]:80;
10: listen 127.0.0.1:8443 ssl http2 proxy_protocol;
然后新建 /etc/nginx/conf.d/dun-443.conf(Alpine 放 /etc/nginx/http.d/),写三条:
set_real_ip_from 127.0.0.1;
real_ip_header proxy_protocol;
port_in_redirect off;
前两条把访客的真实 IP 还原回 $remote_addr,第三条让 nginx 自己发跳转时别带上 8443。为什么不能省,下面实测给你看。
第 3 步:在 443 上分流
新建 /etc/nginx/dun-stream.conf,节点域名和 Reality 端口换成你自己的:
stream {
map $ssl_preread_server_name $dun_upstream {
2gjb4b.ncv5u.example.com 127.0.0.1:8444;
default 127.0.0.1:8443;
}
server {
listen 443;
listen [::]:443;
ssl_preread on;
proxy_protocol on;
proxy_pass $dun_upstream;
}
# 去 Reality 的这一跳把 proxy_protocol 头摘掉
server {
listen 127.0.0.1:8444 proxy_protocol;
proxy_pass 127.0.0.1:36804;
}
}
stream 要和 http 平级,不能放进 conf.d,那里面的文件是在 http 里引入的。在 /etc/nginx/nginx.conf 末尾加一句:
root@vps:~# tail -1 /etc/nginx/nginx.conf
include /etc/nginx/dun-stream.conf;
没有 IPv6 的机器,把 listen [::]:443; 删掉。
坑 ①:unknown directive "stream"
检查一下:
root@vps:~# nginx -t
2026/11/28 12:10:41 [emerg] 12425#12425: unknown directive "stream" in /etc/nginx/dun-stream.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
这是最常见的报错:模块没装,或者装了没加载。nginx -t 不过就别 reload,nginx 会继续用旧配置跑着,网站不受影响。把模块装上:
root@vps:~# apt install -y libnginx-mod-stream 2>&1 | tail -3
Setting up libnginx-mod-stream (1.24.0-2ubuntu7.21) ...
Processing triggers for nginx (1.24.0-2ubuntu7.21) ...
Triggering nginx reload ...
root@vps:~# ls /etc/nginx/modules-enabled/
50-mod-stream.conf
root@vps:~# nginx -t && systemctl reload nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@vps:~# ss -ltnp | grep nginx
LISTEN 0.0.0.0:80 nginx
LISTEN 127.0.0.1:8443 nginx
LISTEN 127.0.0.1:8444 nginx
LISTEN 0.0.0.0:443 nginx
LISTEN [::]:80 nginx
LISTEN [::]:443 nginx
443、8443、8444 都是 nginx 在听了。Debian、Ubuntu 要是还原过 /etc/nginx,modules-enabled 里的链接会丢,再 apt install 也不补,手动补一个:
ln -s /usr/share/nginx/modules-available/mod-stream.conf /etc/nginx/modules-enabled/50-mod-stream.conf
坑 ② ③:真实 IP 和跳转带端口
如果第 2 步那三条没加,网站能打开,但是:
root@client:~# curl -s -o /dev/null -w "%{http_code}\n" https://myblog.example.com/
200
root@client:~# curl -s https://myblog.example.com/ip
你的 IP: 127.0.0.1
root@client:~# curl -sI https://myblog.example.com/docs | grep -i location
location: https://myblog.example.com:8443/docs/
访客 IP 全变成 127.0.0.1,日志、限流、fail2ban 都看成本机;打开目录时 nginx 自己发的跳转把 8443 带了出去,访客被跳到一个打不开的端口。加上那三条、reload 之后:
root@client:~# curl -s https://myblog.example.com/ip
你的 IP: 198.51.100.20
root@client:~# curl -sI https://myblog.example.com/docs | grep -i location
location: https://myblog.example.com/docs/
访问日志里记的也是真实 IP,HTTP/2 也还在:
root@vps:~# grep curl /var/log/nginx/access.log | tail -1
198.51.100.20 - - [28/Nov/2026:12:13:49 +0000] "GET / HTTP/2.0" 200 245 "-" "curl/8.5.0"
再直接打开节点域名,这回走的是 443,返回伪装登录页,证书是 dun 签的通配符证书:
root@client:~# curl -sI https://2gjb4b.ncv5u.example.com/ | head -1
HTTP/2 200
root@client:~# curl -sv https://2gjb4b.ncv5u.example.com/ 2>&1 | grep -E "subject:|SSL certificate verify"
* subject: CN=*.ncv5u.example.com
* SSL certificate verify ok.
第 4 步:让 dun 的链接写 443
打开 /etc/dun/state.json,在 "reality" 那段把 "pub_port" 改成 443,"port" 不要动,它是 Reality 真正监听的端口:
root@vps:~# sed -i 's/"pub_port": 36804/"pub_port": 443/' /etc/dun/state.json
root@vps:~# grep -n port /etc/dun/state.json
4: "port_mode": "direct",
19: "port": 36804,
20: "pub_port": 443,
root@vps:~# dun apply
再看链接,Reality 的端口是 443,订阅地址也不带端口了:
【订阅】v2rayN / Shadowrocket / NekoBox / 小火箭
https://2gjb4b.ncv5u.example.com/••••••••••••••••/sub
【订阅】Clash Verge / FlClash / mihomo
https://2gjb4b.ncv5u.example.com/••••••••••••••••/clash
【Reality 偷自己】直连,速度最好
vless://••••••••-••••-••••-••••-••••••••••••@203.0.113.10:443?encryption=none&flow=xtls-rp…
客户端重新导入一次订阅。旧的订阅地址还能用,因为 Reality 的随机端口还开着。我在新加坡那台用 mihomo 加载新订阅试了一下:
root@client:~# curl -s -o clash.yaml "$(cat sub.url)" && grep -A3 Reality clash.yaml
- name: "vps-Reality"
type: vless
server: 203.0.113.10
port: 443
root@client:~# for i in 1 2 3; do curl -s -x 127.0.0.1:21290 -o /dev/null -w "%{http_code} %{time_total}s\n" https://www.gstatic.com/generate_204; done
204 0.674479s
204 0.679457s
204 0.664398s
root@client:~# curl -s -x 127.0.0.1:21290 https://api.ipify.org; echo
203.0.113.10
三次 204,出口就是洛杉矶这台。网站和节点都在 443 上了。
1.1.5 不再占 80
做这份教程时还发现了 dun 自己的一个问题:1.1.4 及以前,80 空着的时候 dun 会占住 80 做跳转。机器一重启,dun 要是抢在 nginx 前面,nginx 绑不上 80 就起不来,网站直接挂掉。1.1.5 已经修了,dun 完全不碰 80,还在用旧版本的执行 dun upgrade。
改完把这台重启了一次:
root@vps:~# systemctl is-active nginx dun dun-sb
active
active
active
root@vps:~# ss -ltnp | grep -E ':(80|443) ' | awk '{print $4, $6}' | cut -c1-60
0.0.0.0:443 users:(("nginx",pid=487,fd=8),("nginx",pid=486,f
0.0.0.0:80 users:(("nginx",pid=487,fd=5),("nginx",pid=486,fd
[::]:443 users:(("nginx",pid=487,fd=9),("nginx",pid=486,fd=9
[::]:80 users:(("nginx",pid=487,fd=6),("nginx",pid=486,fd=6)
还要注意
- AnyTLS 和 Reality 用的是同一个节点域名,nginx 分不出来,所以 AnyTLS 只能留在自己的端口
- 用面板管理 nginx 的,面板重新生成配置时可能把第 2 步改回去,改之前先看面板支不支持自定义监听
- 程序或配置里用到
$server_port的,现在拿到的是 8443。常见的是proxy_set_header X-Forwarded-Port $server_port,改成写死 443 - 网站前面本来就套了 CDN、自己配过
real_ip_header的,第 2 步那三条要和原来的合并,同一层只能有一个real_ip_header - 以后在 dun 里改了 Reality 端口,
dun-stream.conf里的端口要跟着改,pub_port也要再改回 443
还原
root@vps:~# rm -rf /etc/nginx && mv /etc/nginx.bak-dun /etc/nginx
root@vps:~# nginx -t && systemctl restart nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@vps:~# sed -i 's/"pub_port": 443/"pub_port": 36804/' /etc/dun/state.json && dun apply
root@vps:~# ss -ltnp | grep -E ':(443|36804) ' | awk '{print $4, $6}' | cut -c1-50
0.0.0.0:443 users:(("nginx",pid=627,fd=7),("nginx"
[::]:443 users:(("nginx",pid=627,fd=8),("nginx",pi
*:36804 users:(("sing-box",pid=419,fd=7))
还原后的 /etc/nginx 和最开始的备份 diff -r 一个字节都不差,节点回到 36804,照常能用:
root@client:~# curl -s https://myblog.example.com/ip
你的 IP: 198.51.100.20
root@client:~# curl -sI https://2gjb4b.ncv5u.example.com:36804/ | head -1
HTTP/2 200
客户端重新导入一次订阅就行。
小结
- 不在意端口:用默认的,dun 自己避开,什么都不用改
- 在意:四步,装 stream 模块,网站挪到 8443,443 上按 SNI 分流,
pub_port改成 443 - 三个坑:模块没加载、真实 IP、跳转带端口
有问题直接去 GitHub 提 issue。
