2021-03-15 15:59:11 +00:00
|
|
|
|
# 反向代理
|
|
|
|
|
|
|
|
|
|
反向代理可以把服务器端的流量向客户端转发,即逆向流量转发。
|
|
|
|
|
|
|
|
|
|
反向代理的大致工作原理如下:
|
|
|
|
|
|
2021-03-26 06:01:07 +00:00
|
|
|
|
- 假设在主机 A 中有一个网页服务器,这台主机没有公网 IP,无法在公网上直接访问。另有一台主机 B,它可以由公网访问。现在我们需要把 B 作为入口,把流量从 B 转发到 A。
|
|
|
|
|
- 在主机 A 中配置 Xray,称为`bridge`,在 B 中也配置 Xray,称为 `portal`。
|
|
|
|
|
- `bridge` 会向 `portal` 主动建立连接,此连接的目标地址可以自行设定。`portal` 会收到两种连接,一是由 `bridge` 发来的连接,二是公网用户发来的连接。`portal` 会自动将两类连接合并。于是 `bridge` 就可以收到公网流量了。
|
|
|
|
|
- `bridge` 在收到公网流量之后,会将其原封不动地发给主机 A 中的网页服务器。当然,这一步需要路由的协作。
|
|
|
|
|
- `bridge` 会根据流量的大小进行动态的负载均衡。
|
2021-03-15 15:59:11 +00:00
|
|
|
|
|
|
|
|
|
::: tip
|
2021-03-26 06:01:07 +00:00
|
|
|
|
反向代理默认已开启 [Mux](../../development/protocols/muxcool/),请不要在其用到的 outbound 上再次开启 Mux。
|
2021-03-15 15:59:11 +00:00
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
::: warning
|
|
|
|
|
反向代理功能尚处于测试阶段,可能会有一些问题。
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
## ReverseObject
|
|
|
|
|
|
|
|
|
|
`ReverseObject` 对应配置文件的 `reverse` 项。
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"reverse": {
|
|
|
|
|
"bridges": [
|
|
|
|
|
{
|
|
|
|
|
"tag": "bridge",
|
|
|
|
|
"domain": "test.xray.com"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"portals": [
|
|
|
|
|
{
|
|
|
|
|
"tag": "portal",
|
|
|
|
|
"domain": "test.xray.com"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> `bridges`: \[[BridgeObject](#bridgeobject)\]
|
|
|
|
|
|
|
|
|
|
数组,每一项表示一个 `bridge`。每个 `bridge` 的配置是一个 [BridgeObject](#bridgeobject)。
|
|
|
|
|
|
|
|
|
|
> `portals`: \[[PortalObject](#portalobject)\]
|
|
|
|
|
|
|
|
|
|
数组,每一项表示一个 `portal`。每个 `portal` 的配置是一个 [PortalObject](#bridgeobject)。
|
|
|
|
|
|
|
|
|
|
### BridgeObject
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"tag": "bridge",
|
|
|
|
|
"domain": "test.xray.com"
|
2021-03-15 15:59:11 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> `tag`: string
|
|
|
|
|
|
2021-03-18 15:32:36 +00:00
|
|
|
|
所有由 `bridge` 发出的连接,都会带有这个标识。可以在 [路由配置](./routing.md) 中使用 `inboundTag` 进行识别。
|
2021-03-15 15:59:11 +00:00
|
|
|
|
|
|
|
|
|
> `domain`: string
|
|
|
|
|
|
|
|
|
|
指定一个域名,`bridge` 向 `portal` 建立的连接,都会使用这个域名进行发送。
|
|
|
|
|
这个域名只作为 `bridge` 和 `portal` 的通信用途,不必真实存在。
|
|
|
|
|
|
|
|
|
|
### PortalObject
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"tag": "portal",
|
|
|
|
|
"domain": "test.xray.com"
|
2021-03-15 15:59:11 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> `tag`: string
|
|
|
|
|
|
2021-03-26 06:01:07 +00:00
|
|
|
|
`portal` 的标识。在 [路由配置](./routing.md) 中使用 `outboundTag` 将流量转发到这个 `portal`。
|
2021-03-15 15:59:11 +00:00
|
|
|
|
|
|
|
|
|
> `domain`: string
|
|
|
|
|
|
|
|
|
|
一个域名。当 `portal` 接收到流量时,如果流量的目标域名是此域名,则 `portal` 认为当前连接上 `bridge` 发来的通信连接。而其它流量则会被当成需要转发的流量。`portal` 所做的工作就是把这两类连接进行识别并拼接。
|
|
|
|
|
|
|
|
|
|
::: tip
|
|
|
|
|
一个 Xray 既可以作为 `bridge`,也可以作为 `portal`,也可以同时两者,以适用于不同的场景需要。
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
## 完整配置样例
|
|
|
|
|
|
|
|
|
|
::: tip
|
|
|
|
|
在运行过程中,建议先启用 `bridge`,再启用 `portal`。
|
|
|
|
|
:::
|
|
|
|
|
|
2021-03-26 06:01:07 +00:00
|
|
|
|
### bridge 配置
|
2021-03-15 15:59:11 +00:00
|
|
|
|
|
2021-03-20 15:25:07 +00:00
|
|
|
|
`bridge` 通常需要两个 outbound,一个用于连接 `portal`,另一个用于发送实际的流量。也就是说,你需要用路由区分两种流量。
|
2021-03-15 15:59:11 +00:00
|
|
|
|
|
|
|
|
|
反向代理配置:
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"bridges": [
|
|
|
|
|
{
|
|
|
|
|
"tag": "bridge",
|
|
|
|
|
"domain": "test.xray.com"
|
|
|
|
|
}
|
|
|
|
|
]
|
2021-03-15 15:59:11 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
outbound:
|
|
|
|
|
|
|
|
|
|
```json
|
2021-04-06 11:05:35 +00:00
|
|
|
|
{
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"tag": "out",
|
|
|
|
|
"protocol": "freedom",
|
|
|
|
|
"settings": {
|
|
|
|
|
"redirect": "127.0.0.1:80" // 将所有流量转发到网页服务器
|
|
|
|
|
}
|
2021-04-06 11:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```json
|
2021-03-15 15:59:11 +00:00
|
|
|
|
{
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"protocol": "vmess",
|
|
|
|
|
"settings": {
|
|
|
|
|
"vnext": [
|
|
|
|
|
{
|
|
|
|
|
"address": "portal 的 IP 地址",
|
|
|
|
|
"port": 1024,
|
|
|
|
|
"users": [
|
|
|
|
|
{
|
|
|
|
|
"id": "5783a3e7-e373-51cd-8642-c83782b807c5"
|
|
|
|
|
}
|
2021-03-15 15:59:11 +00:00
|
|
|
|
]
|
2021-03-26 06:01:07 +00:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"tag": "interconn"
|
2021-04-06 11:05:35 +00:00
|
|
|
|
}
|
2021-03-15 15:59:11 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
路由配置:
|
|
|
|
|
|
|
|
|
|
```json
|
2021-04-06 11:05:35 +00:00
|
|
|
|
{
|
|
|
|
|
"rules": [
|
|
|
|
|
{
|
|
|
|
|
"type": "field",
|
|
|
|
|
"inboundTag": ["bridge"],
|
|
|
|
|
"domain": ["full:test.xray.com"],
|
|
|
|
|
"outboundTag": "interconn"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "field",
|
|
|
|
|
"inboundTag": ["bridge"],
|
|
|
|
|
"outboundTag": "out"
|
|
|
|
|
}
|
|
|
|
|
]
|
2021-03-15 15:59:11 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-26 06:01:07 +00:00
|
|
|
|
### portal 配置
|
2021-03-15 15:59:11 +00:00
|
|
|
|
|
2021-03-20 15:25:07 +00:00
|
|
|
|
`portal` 通常需要两个 inbound,一个用于接收 `bridge` 的连接,另一个用于接收实际的流量。同时你也需要用路由区分两种流量。
|
2021-03-15 15:59:11 +00:00
|
|
|
|
|
|
|
|
|
反向代理配置:
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"portals": [
|
|
|
|
|
{
|
|
|
|
|
"tag": "portal",
|
|
|
|
|
"domain": "test.xray.com" // 必须和 bridge 的配置一样
|
|
|
|
|
}
|
|
|
|
|
]
|
2021-03-15 15:59:11 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
inbound:
|
|
|
|
|
|
|
|
|
|
```json
|
2021-04-06 11:05:35 +00:00
|
|
|
|
{
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"tag": "external",
|
2021-04-06 11:05:35 +00:00
|
|
|
|
"port": 80,
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"protocol": "dokodemo-door",
|
|
|
|
|
"settings": {
|
|
|
|
|
"address": "127.0.0.1",
|
|
|
|
|
"port": 80,
|
|
|
|
|
"network": "tcp"
|
|
|
|
|
}
|
2021-04-06 11:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```json
|
2021-03-15 15:59:11 +00:00
|
|
|
|
{
|
2021-04-06 11:05:35 +00:00
|
|
|
|
"port": 1024,
|
2021-03-26 06:01:07 +00:00
|
|
|
|
"tag": "interconn",
|
|
|
|
|
"protocol": "vmess",
|
|
|
|
|
"settings": {
|
|
|
|
|
"clients": [
|
|
|
|
|
{
|
|
|
|
|
"id": "5783a3e7-e373-51cd-8642-c83782b807c5"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
2021-04-06 11:05:35 +00:00
|
|
|
|
}
|
2021-03-15 15:59:11 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
路由配置:
|
|
|
|
|
|
|
|
|
|
```json
|
2021-04-06 11:05:35 +00:00
|
|
|
|
{
|
|
|
|
|
"rules": [
|
|
|
|
|
{
|
|
|
|
|
"type": "field",
|
|
|
|
|
"inboundTag": ["external"],
|
|
|
|
|
"outboundTag": "portal"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "field",
|
|
|
|
|
"inboundTag": ["interconn"],
|
|
|
|
|
"outboundTag": "portal"
|
|
|
|
|
}
|
|
|
|
|
]
|
2021-03-15 15:59:11 +00:00
|
|
|
|
}
|
|
|
|
|
```
|