wireguard-双公网-单内网教程

WireGuard 双公网服务器对接一台内网服务器教程

一、网络结构

本教程适合以下场景:

公网服务器 A:10.0.0.1
内网服务器 B:10.0.0.2
公网服务器 C:10.0.0.3

目标:

公网服务器 A 可以访问内网服务器 B:10.0.0.2
公网服务器 C 可以访问内网服务器 B:10.0.0.2
内网服务器 B 同时连接两台公网服务器 A 和 C
后续可以在公网服务器 A 或 C 上使用 Nginx 反向代理到 10.0.0.2:端口

最终结构:

公网服务器 A  <---- WireGuard ---->  内网服务器 B  <---- WireGuard ---->  公网服务器 C

10.0.0.1                             10.0.0.2                             10.0.0.3

说明:

公网服务器 A 和公网服务器 C 不一定需要互通。
核心目标是:两台公网服务器都可以访问内网服务器 B。

二、服务器规划

服务器 角色 WireGuard IP 说明
公网服务器 A 第一台公网入口 10.0.0.1 监听 UDP 51820
内网服务器 B 内网服务所在机器 10.0.0.2 同时连接 A 和 C
公网服务器 C 第二台公网入口 10.0.0.3 监听 UDP 51820

三、三台服务器安装 WireGuard

三台服务器都执行:

apt update
apt install -y wireguard

创建配置目录:

mkdir -p /etc/wireguard
chmod 700 /etc/wireguard

四、三台服务器分别生成密钥

三台服务器都执行:

cd /etc/wireguard
umask 077
wg genkey | tee private.key | wg pubkey > public.key

查看私钥:

cat /etc/wireguard/private.key

查看公钥:

cat /etc/wireguard/public.key

需要记录下面这些值:

A_PRIVATE_KEY = 公网服务器 A 的私钥
A_PUBLIC_KEY  = 公网服务器 A 的公钥

B_PRIVATE_KEY = 内网服务器 B 的私钥
B_PUBLIC_KEY  = 内网服务器 B 的公钥

C_PRIVATE_KEY = 公网服务器 C 的私钥
C_PUBLIC_KEY  = 公网服务器 C 的公钥

注意:

PrivateKey 是私钥,只能填在本机配置里,不要泄露。
PublicKey 是公钥,可以填到对方服务器的 Peer 配置里。

五、公网服务器 A 配置

公网服务器 A:

WireGuard IP:10.0.0.1
监听端口:51820/udp

编辑配置文件:

nano /etc/wireguard/wg0.conf

写入:

[Interface]
PrivateKey = A_PRIVATE_KEY
Address = 10.0.0.1/24
ListenPort = 51820

[Peer]
PublicKey = B_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32

需要替换:

A_PRIVATE_KEY = 公网服务器 A 自己的私钥
B_PUBLIC_KEY  = 内网服务器 B 的公钥

公网服务器 A 需要在云服务器安全组放行:

UDP 51820

如果系统使用 UFW,可以执行:

ufw allow 51820/udp

六、内网服务器 B 配置

内网服务器 B:

WireGuard IP:10.0.0.2

内网服务器 B 要同时连接两台公网服务器,所以配置里有两个 [Peer]

编辑配置文件:

nano /etc/wireguard/wg0.conf

写入:

[Interface]
PrivateKey = B_PRIVATE_KEY
Address = 10.0.0.2/32

[Peer]
# 公网服务器 A
PublicKey = A_PUBLIC_KEY
Endpoint = A_PUBLIC_IP:51820
AllowedIPs = 10.0.0.1/32
PersistentKeepalive = 25

[Peer]
# 公网服务器 C
PublicKey = C_PUBLIC_KEY
Endpoint = C_PUBLIC_IP:51820
AllowedIPs = 10.0.0.3/32
PersistentKeepalive = 25

需要替换:

B_PRIVATE_KEY = 内网服务器 B 自己的私钥

A_PUBLIC_KEY  = 公网服务器 A 的公钥
A_PUBLIC_IP   = 公网服务器 A 的真实公网 IP

C_PUBLIC_KEY  = 公网服务器 C 的公钥
C_PUBLIC_IP   = 公网服务器 C 的真实公网 IP

示例:

Endpoint = 159.75.xxx.xxx:51820
Endpoint = 198.20.xxx.xxx:51820

注意:

Endpoint 必须是:公网IP:端口
不能只写公网 IP 的一部分。

错误示例:

Endpoint = 159.75
Endpoint = 198.20.13

正确示例:

Endpoint = 159.75.xxx.xxx:51820
Endpoint = 198.20.xxx.xxx:51820

七、公网服务器 C 配置

公网服务器 C:

WireGuard IP:10.0.0.3
监听端口:51820/udp

编辑配置文件:

nano /etc/wireguard/wg0.conf

写入:

[Interface]
PrivateKey = C_PRIVATE_KEY
Address = 10.0.0.3/24
ListenPort = 51820

[Peer]
PublicKey = B_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32

需要替换:

C_PRIVATE_KEY = 公网服务器 C 自己的私钥
B_PUBLIC_KEY  = 内网服务器 B 的公钥

公网服务器 C 也需要在云服务器安全组放行:

UDP 51820

如果系统使用 UFW,可以执行:

ufw allow 51820/udp

八、最终配置汇总

1. 公网服务器 A:10.0.0.1

[Interface]
PrivateKey = A_PRIVATE_KEY
Address = 10.0.0.1/24
ListenPort = 51820

[Peer]
PublicKey = B_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32

2. 内网服务器 B:10.0.0.2

[Interface]
PrivateKey = B_PRIVATE_KEY
Address = 10.0.0.2/32

[Peer]
PublicKey = A_PUBLIC_KEY
Endpoint = A_PUBLIC_IP:51820
AllowedIPs = 10.0.0.1/32
PersistentKeepalive = 25

[Peer]
PublicKey = C_PUBLIC_KEY
Endpoint = C_PUBLIC_IP:51820
AllowedIPs = 10.0.0.3/32
PersistentKeepalive = 25

3. 公网服务器 C:10.0.0.3

[Interface]
PrivateKey = C_PRIVATE_KEY
Address = 10.0.0.3/24
ListenPort = 51820

[Peer]
PublicKey = B_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32

九、启动 WireGuard

三台服务器都执行:

systemctl enable wg-quick@wg0
systemctl restart wg-quick@wg0

查看服务状态:

systemctl status wg-quick@wg0 --no-pager -l

查看 WireGuard 状态:

wg show

查看 WireGuard 网卡:

ip addr show wg0

十、测试连通性

1. 在公网服务器 A 测试访问内网服务器 B

ping 10.0.0.2

如果能 ping 通,说明公网服务器 A 已经可以访问内网服务器 B。


2. 在公网服务器 C 测试访问内网服务器 B

ping 10.0.0.2

如果能 ping 通,说明公网服务器 C 已经可以访问内网服务器 B。


3. 在内网服务器 B 测试访问两台公网服务器

ping 10.0.0.1
ping 10.0.0.3

如果都能 ping 通,说明内网服务器 B 已经同时连接两台公网服务器。


十一、查看握手状态

执行:

wg show

正常会看到类似:

latest handshake: 1 minute ago
transfer: xx KiB received, xx KiB sent

如果某个 Peer 没有 latest handshake,说明这条 WireGuard 连接没有成功。


十二、如果启动失败,使用干净重启方式

如果执行:

systemctl restart wg-quick@wg0

报错:

Job for wg-quick@wg0.service failed because the control process exited with error code.

可以执行:

wg-quick down wg0 2>/dev/null
ip link delete wg0 2>/dev/null
systemctl reset-failed wg-quick@wg0
wg-quick up wg0

如果 wg-quick up wg0 启动成功,再执行:

systemctl enable wg-quick@wg0
systemctl restart wg-quick@wg0
wg show

十三、常见错误排查

1. Endpoint 写错

错误写法:

Endpoint = 159.75
Endpoint = 198.20.13

正确写法:

Endpoint = 159.75.xxx.xxx:51820
Endpoint = 198.20.xxx.xxx:51820

说明:

Endpoint 必须是完整的公网 IP + 端口。
端口一般是 51820。

2. 密钥格式错误

如果看到:

Key is not the correct length or format

说明:

PrivateKey 或 PublicKey 不完整
密钥复制错了
密钥末尾多了空格
密钥少了 =
使用了中文符号

正确密钥一般类似:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=

3. AllowedIPs 路由冲突

内网服务器 B 同时连接两台公网服务器时,不要这样写:

[Peer]
AllowedIPs = 10.0.0.0/24

[Peer]
AllowedIPs = 10.0.0.0/24

这是错误的,可能会导致路由冲突。

正确写法:

[Peer]
AllowedIPs = 10.0.0.1/32

[Peer]
AllowedIPs = 10.0.0.3/32

4. 公网服务器 UDP 51820 没放行

两台公网服务器都需要放行:

UDP 51820

注意:

WireGuard 使用 UDP,不是 TCP。

5. wg0 已经存在

如果看到:

wg0 already exists

执行:

wg-quick down wg0 2>/dev/null
ip link delete wg0 2>/dev/null
wg-quick up wg0

十四、Nginx 反向代理示例

等 WireGuard 通了以后,两台公网服务器都可以反向代理到内网服务器。

假设内网服务器 B 上的 API 服务是:

10.0.0.2:8080

公网服务器 A 或 C 上的 Nginx 可以这样写:

server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://10.0.0.2:8080;

        proxy_http_version 1.1;
        proxy_set_header Connection "";

        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_connect_timeout 5s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }
}

测试 Nginx 配置:

nginx -t

重新加载 Nginx:

systemctl reload nginx

十五、内网服务器服务监听注意事项

如果内网服务器 B 上有服务,例如:

8080 端口

不要只监听:

127.0.0.1:8080

否则公网服务器 A 和 C 通过 WireGuard 访问不到。

建议监听:

0.0.0.0:8080

或者:

10.0.0.2:8080

十六、API 接口使用建议

如果用于 API 接口:

低延迟公网服务器优先作为主入口
高延迟公网服务器可以作为备用入口

例如:

公网服务器 A -> 内网服务器 B:6ms,适合作为主 API 入口
公网服务器 C -> 内网服务器 B:84ms,可以作为备用入口

推荐:

主线路:低延迟公网服务器
备用线路:高延迟公网服务器

不建议:

通过高延迟 WireGuard 链路直连数据库
通过高延迟 WireGuard 链路跑高频 Redis 请求

适合:

Nginx 反向代理到内网 API 服务
低频管理后台
备用线路
容灾线路

十七、最终检查清单

配置完成后,检查下面几项:

1. 公网服务器 A 已放行 UDP 51820
2. 公网服务器 C 已放行 UDP 51820
3. 三台服务器都有 /etc/wireguard/wg0.conf
4. 三台服务器的 PrivateKey 都是自己的私钥
5. Peer 里填写的是对方的 PublicKey
6. 内网服务器 B 有两个 Peer
7. 内网服务器 B 的两个 Peer 的 AllowedIPs 分别是 10.0.0.1/32 和 10.0.0.3/32
8. Endpoint 写法是 公网IP:51820
9. A 可以 ping 通 10.0.0.2
10. C 可以 ping 通 10.0.0.2
11. B 可以 ping 通 10.0.0.1 和 10.0.0.3
12. wg show 可以看到 latest handshake

十八、最简启动命令

三台服务器都可以用:

systemctl enable wg-quick@wg0
systemctl restart wg-quick@wg0
wg show

如果失败,用:

wg-quick down wg0 2>/dev/null
ip link delete wg0 2>/dev/null
systemctl reset-failed wg-quick@wg0
wg-quick up wg0
wg show

十九、最终目标判断

只要下面两个命令都能通,就说明核心目标完成:

在公网服务器 A:

ping 10.0.0.2

在公网服务器 C:

ping 10.0.0.2

如果都能 ping 通,说明:

两台公网服务器都已经可以通过 WireGuard 内网访问内网服务器。

后续你只需要在公网服务器 A 或公网服务器 C 上配置 Nginx,反向代理到:

http://10.0.0.2:端口

AI生成

posted @ 2026-05-07 09:48  可恶的1号富翁  阅读(10)  评论(0)    收藏  举报