联合使用Apache与Nginx反代实现分流与IPv4转IPv6

作者 by Stuart / 2022-01-06 / 1 评论 / 294 个足迹

前言

废话

前不久将Blog从Oracle挪腾到了Scaleway,Stardust确实特别便宜,但是仅有IPv6地址这一点很伤,国内宽带大面积没有开通IPv6或许只是因为咱钱没加够,Web访问倒是可以通过套CF解决,问题是一旦需要通过SSH连上去处理点东西就会变得非常头秃,只能切换到手机热点。突然想到自己还有一台巴黎的机器同时有IPv4和IPv6,为何不搭建一个桥接来方便仅有IPv4网络时访问纯IPv6资源呢。顺便,还有一台Scaleway星尘,计划挂载OSS当做存储点,也是纯IPv6,正好顺手做个反代分流。

环境

Scaleway 星尘 VPS 巴黎/阿姆斯特丹各一,以下简称服务器A/B,系统为Debian 11,均为纯IPv6服务器;
巴黎独服*1 以下简称服务器F,系统为Debian 9,具有IPv4与IPv6;
DNS服务提供商为CloudFlare;
设置二级域名f-a给服务器A使用,f-b给服务器B使用,以上两个二级域名均设置A记录(即IPv4解析记录)到服务器F;

过程

Apache2

安装Apache2

这里省事,直接使用apt包管理方式

apt-get install apache2

启用反代所需模组

a2enmod proxy proxy_http proxy_ajp proxy_balancer proxy_connect proxy_html lbmethod_byrequests rewrite deflate headers

重启apache服务使模组生效

systemctl restart apache2
配置Apache2

准备修改Apache2的虚拟主机配置,先停止Apache2服务

systemctl stop apache2

到apache配置文件目录

cd /etc/apache2/sites-enabled/

创建第一个配置文件,内容是让Apache将请求访问服务器F上的f-a.stuartlab.net转发到50001端口
(创建之前记得把默认自带的那个配置文件000-default删掉)
(此处个人喜好是使用vim编辑器,其他的如nano,vi,emacs均可)

vim f-a.conf

内容如下

<VirtualHost *:80>
    ServerName f-a.stuartlab.net
    ProxyRequests On
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:50001/
    ProxyPassReverse /  http:// 127.0.0.1:50001/
</VirtualHost>

保存并退出
创建第二个配置文件,内容是让Apache将请求访问服务器F上的f-b.stuartlab.net转发到50002端口

vim f-b.conf

内容如下

<VirtualHost *:80>
    ServerName f-b.stuartlab.net
    ProxyRequests On
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:50002/
    ProxyPassReverse /  http:// 127.0.0.1:40002/
</VirtualHost>

保存并退出
重启Apache2

systemctl restart apache2

Nginx

准备工作

由于这里需要用ngx_stream_core模块,默认打包好的软件包并不带有,所以需要自己下载源码编译安装;
此时最新的源码包为1.21.5,到官网下载源码包

wget http://nginx.org/download/nginx-1.21.5.tar.gz

解压缩所下载的压缩包

tar -zxvf nginx-1.21.5.tar.gz

进入解压获得的文件夹

cd nginx-1.21.5

安装编译所需软件包

apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev
编译安装Nginx

配置Nginx编译参数

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module 

正常情况下应有如下输出

creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/etc/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/etc/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/etc/nginx/logs/nginx.pid"
  nginx error log file: "/etc/nginx/logs/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

编译并安装

make &&make install

使用nginx-v指令查看当前nginx版本输出如下则一切正常(仅限于1.21.5版本)

nginx version: nginx/1.21.5
配置Nginx

编辑nginx配置文件,由之前的配置可知nginx配置文件在/etc/nginx/

cd /etc/nginx/

编辑nginx.conf,删除原有内容,写入如下

worker_processes  1;
events {
    worker_connections  1024;
}
stream {
    upstream backend1 { #France-HTTP
        hash $remote_addr consistent;
        server [服务器A的IPv6地址]:80         max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend1;
    }
    server {
        listen 50001;
        proxy_connect_timeout 10s;
        proxy_timeout 30s;
        proxy_pass backend1;
    }
     upstream backend2 { #Netherland-HTTPl
        hash $remote_addr consistent;
        server [服务器B的IPv6地址]:80    max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend2;
    }
    server {
        listen 50002;
        proxy_connect_timeout 10s;
        proxy_timeout 30s;
        proxy_pass backend2;
    }
     upstream backend3{ #France-SSH
        hash $remote_addr consistent;
        server [服务器A的IPv6地址]:22         max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend3;
    }
    server {
        listen 50003;
        proxy_connect_timeout 10s;
        proxy_timeout 30s;
        proxy_pass backend3;
    }
     upstream backend4 { #Netherland-ssh
        hash $remote_addr consistent;
        server [服务器B的IPv6地址]:22    max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend4;
    }
    server {
        listen 50004;
        proxy_connect_timeout 10s;
        proxy_timeout 30s;
        proxy_pass backend4;
    }
}

保存并退出
重启Nginx服务

nginx -s reload

结果

最终效果为在纯IPv4环境下可直接依靠二级域名分流,访问服务器A和B上的Web内容(具体表现为可直接访问f-a.stuartlab.net与f-b.stuartlab.net,标准的80/443端口,解析到的结果是服务器F的IP地址,内容为服务器A/B所提供的Web);
通过ssh Ausername@服务器F的IP地址:50003可直接在IPv4环境下SSH连接的服务器A,同理Busername@服务器F的IP地址:50004可直接在IPv4环境下SSH连接的服务器B;

END&一点补充

实验时使用的另外的端口号和二级域名,文中提到的域名与端口号怼过去应该是未解析才对,但是确实做了这个实验.jpg;
请把这个当成一份实验报告/过程或者是笔记而不是当作教程;
Stuart Jan,8,2022

Reference&参考资料

https://nginx.org/en/docs/stream/ngx_stream_core_module.html
https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html

独特见解

 评论 1 条