Skip to content

Commit e0628a7

Browse files
rp-helloFangliding
andauthored
优化反向代理说明文档 (#649)
* Update reverse.md * correct sth --------- Co-authored-by: 风扇滑翔翼 <[email protected]>
1 parent 5bc9395 commit e0628a7

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

docs/config/reverse.md

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22

33
反向代理可以把服务器端的流量向客户端转发,即逆向流量转发。
44

5+
其底层协议为 Mux.cool, 作为一个多路复用协议,它还具有类似 QUIC 的性质,客户端和服务端是对等的,两边都可以新建一个子连接。一般情况情况下只有客户端打开,服务端打开子连接被这里用于发送反向代理请求。
6+
57
反向代理的大致工作原理如下:
68

79
- 假设在主机 A 中有一个网页服务器,这台主机没有公网 IP,无法在公网上直接访问。另有一台主机 B,它可以由公网访问。现在我们需要把 B 作为入口,把流量从 B 转发到 A。
8-
- 在主机 A 中配置 Xray,称为`bridge`,在 B 中也配置 Xray,称为 `portal`
9-
- `bridge` 会向 `portal` 主动建立连接,此连接的目标地址可以自行设定。`portal` 会收到两种连接,一是由 `bridge` 发来的连接,二是公网用户发来的连接。`portal` 会自动将两类连接合并。于是 `bridge` 就可以收到公网流量了。
10-
- `bridge` 在收到公网流量之后,会将其原封不动地发给主机 A 中的网页服务器。当然,这一步需要路由的协作。
10+
- 在主机 B 中配置 Xray,接收外部请求,所以称为 `portal` (门户)。
11+
- 在主机 A 中配置 Xray,负责将B的转发和网页服务器桥接起来,称为`bridge`
12+
13+
- `bridge`
14+
- `bridge` 会向 `portal` 主动建立连接以注册反向通道,此连接的目标地址(domain)可以自行设定。
15+
- `bridge` 在收到`portal`转发过来的公网流量之后,会将其原封不动地发给主机 A 中的网页服务器。当然,这一步需要路由模块的配置。
16+
- `bridge` 收到响应后,也会将响应原封不动地返回给`portal`
17+
18+
- `portal`
19+
- `portal` 收到请求且domain匹配,则说明是由 `bridge` 发来的响应数据,这条连接会用于建立反向通道。
20+
- `portal` 收到请求,domain不匹配,则说明是公网用户发来的连接,这种连接数据会转发给bridge.
21+
1122
- `bridge` 会根据流量的大小进行动态的负载均衡。
1223

1324
::: tip
14-
反向代理默认已开启 [Mux](../../development/protocols/muxcool/),请不要在其用到的 outbound 上再次开启 Mux。
25+
如上所述,反向代理默认已开启 [Mux](../../development/protocols/muxcool/),请不要在其用到的 outbound 上再次开启 Mux。
1526
:::
1627

1728
::: warning
@@ -22,19 +33,19 @@
2233

2334
`ReverseObject` 对应配置文件的 `reverse` 项。
2435

25-
```json
36+
```jsonc
2637
{
2738
"reverse": {
2839
"bridges": [
2940
{
3041
"tag": "bridge",
31-
"domain": "test.xray.com"
42+
"domain": "reverse-proxy.xray.internal"
3243
}
3344
],
3445
"portals": [
3546
{
3647
"tag": "portal",
37-
"domain": "test.xray.com"
48+
"domain": "reverse-proxy.xray.internal"
3849
}
3950
]
4051
}
@@ -51,10 +62,10 @@
5162

5263
### BridgeObject
5364

54-
```json
65+
```jsonc
5566
{
5667
"tag": "bridge",
57-
"domain": "test.xray.com"
68+
"domain": "reverse-proxy.xray.internal"
5869
}
5970
```
6071

@@ -64,15 +75,15 @@
6475

6576
> `domain`: string
6677
67-
指定一个域名,`bridge``portal` 建立的连接,都会使用这个域名进行发送
78+
指定一个域名,`bridge``portal` 建立的连接,都会借助这个域名进行发送
6879
这个域名只作为 `bridge``portal` 的通信用途,不必真实存在。
6980

7081
### PortalObject
7182

72-
```json
83+
```jsonc
7384
{
7485
"tag": "portal",
75-
"domain": "test.xray.com"
86+
"domain": "reverse-proxy.xray.internal"
7687
}
7788
```
7889

@@ -82,7 +93,7 @@
8293

8394
> `domain`: string
8495
85-
一个域名。当 `portal` 接收到流量时,如果流量的目标域名是此域名,则 `portal` 认为当前连接上 `bridge` 发来的通信连接。而其它流量则会被当成需要转发的流量。`portal` 所做的工作就是把这两类连接进行识别并拼接
96+
一个域名。当 `portal` 接收到流量时,如果流量的目标域名是此域名,则 `portal` 认为当前连接上是 `bridge` 发来的通信连接。而其它流量则会被当成需要转发的流量。`portal` 所做的工作就是把这两类连接进行识别并做对应的转发
8697

8798
::: tip
8899
一个 Xray 既可以作为 `bridge`,也可以作为 `portal`,也可以同时两者,以适用于不同的场景需要。
@@ -100,31 +111,33 @@
100111

101112
反向代理配置:
102113

103-
```json
104-
{
114+
```jsonc
115+
"reverse": {
105116
"bridges": [
106117
{
107118
"tag": "bridge",
108-
"domain": "test.xray.com"
119+
"domain": "reverse-proxy.xray.internal"
109120
}
110121
]
111122
}
112123
```
113124

114125
outbound:
115126

116-
```json
127+
```jsonc
117128
{
129+
// 转发到网页服务器
118130
"tag": "out",
119131
"protocol": "freedom",
120132
"settings": {
121-
"redirect": "127.0.0.1:80" // 将所有流量转发到网页服务器
133+
"redirect": "127.0.0.1:80"
122134
}
123135
}
124136
```
125137

126-
```json
138+
```jsonc
127139
{
140+
// 连接到 portal
128141
"protocol": "vmess",
129142
"settings": {
130143
"vnext": [
@@ -145,16 +158,20 @@ outbound:
145158

146159
路由配置:
147160

148-
```json
161+
```jsonc
149162
{
150163
"rules": [
151164
{
165+
// bridge 发出的请求,且域名为配置的域名,那么说明这是尝试向 portal 建立反向隧道的请求,
166+
// 则路由到 interconn,即连接到 portal
152167
"type": "field",
153168
"inboundTag": ["bridge"],
154-
"domain": ["full:test.xray.com"],
169+
"domain": ["full:reverse-proxy.xray.internal"],
155170
"outboundTag": "interconn"
156171
},
157172
{
173+
// 从 portal 过来的流量,也会从 bridge 出来,但是不带上面的domain
174+
// 则路由到 out,即转发给网页服务器
158175
"type": "field",
159176
"inboundTag": ["bridge"],
160177
"outboundTag": "out"
@@ -169,21 +186,22 @@ outbound:
169186

170187
反向代理配置:
171188

172-
```json
173-
{
189+
```jsonc
190+
"reverse": {
174191
"portals": [
175192
{
176193
"tag": "portal",
177-
"domain": "test.xray.com" // 必须和 bridge 的配置一样
194+
"domain": "reverse-proxy.xray.internal" // 必须和 bridge 的配置一样
178195
}
179196
]
180197
}
181198
```
182199

183200
inbound:
184201

185-
```json
202+
```jsonc
186203
{
204+
// 直接接收来自公网的请求
187205
"tag": "external",
188206
"port": 80,
189207
"protocol": "dokodemo-door",
@@ -195,10 +213,11 @@ inbound:
195213
}
196214
```
197215

198-
```json
216+
```jsonc
199217
{
200-
"port": 1024,
218+
// 接收来自 bridge 尝试建立反向隧道的请求
201219
"tag": "interconn",
220+
"port": 1024,
202221
"protocol": "vmess",
203222
"settings": {
204223
"clients": [
@@ -212,15 +231,20 @@ inbound:
212231

213232
路由配置:
214233

215-
```json
234+
```jsonc
216235
{
217236
"rules": [
218237
{
238+
// 如果入站是 external,说明是来自公网的请求,
239+
// 则路由到 portal, 最终会转发给 bridge
219240
"type": "field",
220241
"inboundTag": ["external"],
221242
"outboundTag": "portal"
222243
},
223244
{
245+
// 如果来自 interconn 入站,说明是来自 bridge 的尝试建立反向隧道请求,
246+
// 则路由到 portal, 最终会转发给对应的公网客户端
247+
// 注意:这里进入的请求会带上了前文配置的domain,所以 portal 能够区分两种被路由到 portal 的请求
224248
"type": "field",
225249
"inboundTag": ["interconn"],
226250
"outboundTag": "portal"

0 commit comments

Comments
 (0)