前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 B VPS 中转 A VPS 流量

使用 B VPS 中转 A VPS 流量

作者头像
yiyun
发布2023-04-12 09:56:29
60.8K0
发布2023-04-12 09:56:29
举报
文章被收录于专栏:yiyun 的专栏yiyun 的专栏

引言 由于某些不可抗力原因, 所处网络环境无法访问 A VPS, 因此 增加 能访问到的 B VPS 来中转流量到 A VPS me --x-> A VPS me <----> B VPS <----> A VPS 非内网穿透需求, A VPS 有公网ip, 可被公网直接发现 和使用某些客户端 配合 Cloudflare Proxy 类似 me(X Client) <----> Cloudflare <----> A VPS <----> google.com me(X Client) <----> 中转机 <----> 落地机 <----> google.com PS: 目前已知 Cloudflare 支持代理: HTTP/HTTPS, WebSocket 和 gRPC(HTTP/2) 但支持的端口有限, 详情请看 Cloudflare 文档 Nginx 流量转发 Nginx是非常强大的四层、七层反向代理软件,功能强大,在互联网上广泛应用。 本节介绍Nginx转发配置。

1. 配置 B VPS /etc/nginx/nginx.conf /etc/nginx/nginx.conf

代码语言:javascript
复制
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

# start: 转发
stream {
    server {
        listen B-VPS-端口号;  # 1-65535 的任意一个数字,无需与 A VPS 的端口号相同
        proxy_pass A-VPS-ip:A-VPS-端口号; # 用 A VPS ip 和端口号替换
    }
}
# end: 转发

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    ....
}
代码语言:javascript
复制
nginx -s reload
代码语言:javascript
复制
# 记得 B VPS 放行此端口 (B-VPS-端口号)
firewall-cmd --zone=public --add-port=8024/tcp --permanent
firewall-cmd --reload

也可以下方这么写 /etc/nginx/nginx.conf

代码语言:javascript
复制
# start: 转发
stream {
    upstream bi_remote_desk {
        # 转发 ip 和端口
        # 用 A VPS ip 和端口号替换
        server 10.10.10.10:3389;
    }
    server {
        # 监听端口
        # 1-65535 的任意一个数字,无需与 A VPS 的端口号相同
        listen 3389; 
        # 转发请求
        proxy_pass bi_remote_desk;  
    }
}
# end: 转发

可以同时配置多个 /etc/nginx/nginx.conf

代码语言:javascript
复制
# start: 转发
stream {
    upstream bi_remote_desk {
        # simple round-robin 转发IP和端口
        server 10.10.10.10:3389;
        #check interval=3000 rise=2 fall=5 timeout=1000;
        #check interval=3000 rise=2 fall=5timeout=1000
        #check interval=3000 rise=2 fall=5timeout=1000
        #check_http_send "GET /HTTP/1.0\r\n\r\n";
        #check_http_expect_alive http_2xxhttp_3xx;
    }
    server {
        listen 3389; ##监听端口
        proxy_pass bi_remote_desk;  #转发请求
    }
    upstream 214_ssh {
        server 10.10.10.10:22;
    }
    server {
        listen 105; ##监听端口
        proxy_pass 214_ssh;  #转发请求
    }
}
# end: 转发

2. 配置 A VPS

仅允许 B VPS ip 连接该服务器(A)的 32 端口, 当然也可以不做这步

代码语言:javascript
复制
firewall-cmd --permanent --add-source=B-VPS-ip/32
fireawll-cmd --reload

firewalld 流量转发 firewalld 是 CentOS7/8 默认的防火墙前端软件,绝大多数主机商提供的镜像都已经安装。 firewalld 转发的好处是 效率高, 直接在内核执行。 TODO: firewalld 流量转发 Q&A

Q: NAT 是什么 ?

参考: Network address translation - Wikipedia From Wikipedia, the free encyclopedia Network address translation between a private network and the Internet

Network address translation (NAT) is a method of mapping an IP address space into another by modifying network address information in the IP header of packets while they are in transit across a traffic routing device.[1] The technique was originally used to bypass the need to assign a new address to every host when a network was moved, or when the upstream Internet service provider was replaced, but could not route the network's address space. It has become a popular and essential tool in conserving global address space in the face of IPv4 address exhaustion. One Internet-routable IP address of a NAT gateway can be used for an entire private network.[2] As network address translation modifies the IP address information in packets, NAT implementations may vary in their specific behavior in various addressing cases and their effect on network traffic. The specifics of NAT behavior are not commonly documented by vendors of equipment containing NAT implementations.[2] 网络地址转换(英语:Network Address Translation,缩写:NAT;又称网络掩蔽、IP掩蔽)在计算机网络中是一种在IP数据包通过路由器防火墙时重写来源IP地址或目的IP地址的技术。这种技术被普遍使用在有多台主机但只通过一个公有IP地址访问互联网私有网络中。它是一个方便且得到了广泛应用的技术。当然,NAT也让主机之间的通信变得复杂,导致了通信效率的降低。 1990年代中期,NAT是作为一种解决IPv4地址短缺以避免保留IP地址困难的方案而流行起来的。网络地址转换在很多国家广泛使用。所以NAT就成了家庭和小型办公室网络连接上的路由器的一个标准特征,因为对他们来说,申请独立的IP地址的代价要高于所带来的效益。 在一个典型的配置中,一个本地网络使用一个专有网络的指定子网(比如192.168.x.x或10.x.x.x)和连在这个网络上的一个路由器。这个路由器占有这个网络地址空间的一个专有地址(比如192.168.0.1),同时它还通过一个或多个因特网服务提供商提供的公有的IP地址(叫做“过载”NAT)连接到因特网上。当信息由本地网络向因特网传递时,源地址从专有地址转换为公用地址。由路由器跟踪每个连接上的基本数据,主要是目的地址和端口。当有回复返回路由器时,它通过输出阶段记录的连接跟踪数据来决定该转发给内部网的哪个主机;如果有多个公用地址可用,当数据包返回时,TCPUDP客户机的端口号可以用来分解数据包。对于因特网上的通信,路由器本身充当源和目的。 流行在网络上的一种看法认为,IPv6的广泛采用将使得NAT不再需要,因为NAT只是一个处理IPv4的地址空间不足的方法。 补充 参考 感谢帮助!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-04-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 配置 B VPS /etc/nginx/nginx.conf /etc/nginx/nginx.conf
  • 2. 配置 A VPS
  • Q: NAT 是什么 ?
相关产品与服务
私有网络
私有网络(Virtual Private Cloud,VPC)是基于腾讯云构建的专属云上网络空间,为您在腾讯云上的资源提供网络服务,不同私有网络间完全逻辑隔离。作为您在云上的专属网络空间,您可以通过软件定义网络的方式管理您的私有网络 VPC,实现 IP 地址、子网、路由表、网络 ACL 、流日志等功能的配置管理。私有网络还支持多种方式连接 Internet,如弹性 IP 、NAT 网关等。同时,您也可以通过 VPN 连接或专线接入连通腾讯云与您本地的数据中心,灵活构建混合云。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档