v2ray反向代理

这篇文章发表于 阅读 0

本文主要介绍自己在使用v2ray进行反向代理过程中。安装请参考官网安装,主要实现通过中间服务器访问家里电脑上运行的服务。

原理

  • 假设在主机 A 中有一个网页服务器,这台主机没有公网 IP,无法在公网上直接访问。另有一台主机 B,它可以由公网访问。现在我们需要把 B 作为入口,把流量从 B 转发到 A。

  • 在主机 A 中配置一个 V2Ray,称为bridge,在 B 中也配置一个 V2Ray,称为portal

  • bridge会向portal主动建立连接,此连接的目标地址可以自行设定。portal会收到两种连接,一是由bridge发来的连接,二是公网用户发来的连接。portal会自动将两类连接合并。于是bridge就可以收到公网流量了。

  • bridge在收到公网流量之后,会将其原封不动地发给主机 A 中的网页服务器。当然,这一步需要路由的协作。

  • bridge会根据流量的大小进行动态的负载均衡

也就是说你需要一台能访问公网的服务器,一台家里的电脑。

服务器配置
{ "log": { "error": "/var/log/v2ray/error.log", "loglevel": "info", "access": "/var/log/v2ray/access.log" }, "inbounds": [ // 链接外网的入口 { "tag": "external", "port": 40080, // 外网访问的端口 "protocol": "dokodemo-door", "settings": { "address": "127.0.0.1", "port": 80, // 访问的本地电脑的端口 "network": "tcp" } }, // 用于接受bridge的连接 { "port": 1024, "tag": "interconn", "protocol": "vmess", "settings": { "clients": [ { "id": "27848739-7e62-4138-9fd3-098a63964b6b" } ] } } ], "outbounds": [ { "protocol": "freedom", "settings": {} } ], "routing": { "rules": [ { "type": "field", "inboundTag": [ "external" ], "outboundTag": "portal" }, { "type": "field", "inboundTag": [ "interconn" ], "outboundTag": "portal" } ] }, // 反向代理的配置 "reverse": { "portals": [ { "tag": "portal", "domain": "bridge.ablum.com" } ] } }
客户端配置
{ "log": { "error": "/var/log/v2ray/error.log", "loglevel": "info", "access": "/var/log/v2ray/access.log" }, "inbounds": [], "outbounds": [ { "tag": "out", "protocol": "freedom", "settings": { "redirect": "127.0.0.1:80" } }, { "protocol": "vmess", "settings": { "vnext": [ { "address": " 服务器ip", "port": 1024, "users": [{ "id": "27848739-7e62-4138-9fd3-098a63964b6b" }] } ] }, "tag": "interconn" }, { "tag": "blockout", "protocol": "blackhole", "settings": { "response": { "type": "none" } } } ], "dns": { "servers": [""] }, "routing": { "strategy": "rules", "settings": { "domainStrategy": "IPIfNonMatch", "rules": [ { "type": "field", "inboundTag": ["bridge"], "domain": ["full:bridge.ablum.com"], "outboundTag": "interconn" }, { "type": "field", "inboundTag": ["bridge"], "outboundTag": "out" } ] } }, "transport": {}, "reverse": { "bridges": [ { "tag": "bridge", "domain": "bridge.ablum.com" // 与服务器配置的domain相同,不用真实存在 } ] } }

PS: 官方先启动bridge,再启动portal,实测先启动portal也可以。