(JE&BE对应)通过在VPS上安装HaProxy和Nginx来对BungeeCord的通信进行反向代理

为了隐藏Minecraft的原始IP,我们可以使用VPS进行反向代理。通过准备多个VPS并通过GSLB等方式进行分散,还可以用作DDoS防护。

从构成上来看,

[BungeeCord - MCServer] <--> [VPS(Proxy)] <--> [クライアント]

のようになります。

因为我们还需要获取连接过来的用户的IP地址,所以我们将使用proxy_protocol来实现。

由于知識水平有限,我在查閱各種文章和文件時進行操作。
我以最低限動作為目標,所以可能會有不穩定的情況。

这次要使用的VPS

WebARENA Indigoの8GB 6vCPUプランのVPSを使用します。
OSはUbuntu 20.04です。
オーバースペックな気もしますが…まぁいいでしょう。
友人からもらったVPSを使っているので…

准备 – BungeeCord / Geyser

将BungeeCord的config.yml中的proxy_protocol更改为true。


将Geyser的config.yml文件中的enable-proxy-protocol和proxy-protocol-whitelisted-ips进行以下更改:

enable-proxy-protocol: true #有効化
proxy-protocol-whitelisted-ips: "VPSのIP" #ホワリス

如果想允许多个VPS进行访问,可以这样设置:proxy-protocol-whitelisted-ips: [“VPS的IP”, “VPS的IP(2)”]。

准备 – VPS方面 – VPS

HaProxyをインストール

$sudo apt install haproxy

Nginxをビルド

在 apt 可用的 Nginx 中不包含 ngx_stream_* 模块。
因此,您必须自己构建。

下载最新版的Nginx。
(截至2023年9月26日,1.24.0是最新版本。)

$wget http://nginx.org/download/nginx-1.24.0.tar.gz
$tar zxvf nginx-1.24.0.tar.gz
$cd nginx-1.24.0

下载需要构建所需的库。

$apt install build-essential libpcre3-dev zlib1g-dev

创建用户。

$ sudo groupadd nginx
$ sudo useradd -g nginx nginx
$ sudo usermod -s /bin/false nginx

ビルドします。

$sudo ./configure \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --pid-path=/var/run/nginx.pid \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --user=nginx \
    --group=nginx \
    --with-stream

$sudo make
$sudo make install

起動し、無事に動作していることを確認

$ sudo systemctl start nginx.service
$ sudo systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
     Active: active (running) since Mon 2023-09-25 20:01:45 JST; 16h ago
       Docs: man:nginx(8)
    Process: 660193 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_pro>
    Process: 660200 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; >
   Main PID: 660205 (nginx)
      Tasks: 3 (limit: 9513)
     Memory: 2.3M
     CGroup: /system.slice/nginx.service
             tq660205 nginx: master process /usr/sbin/nginx -g daemon on; maste>
             tq660207 nginx: worker process
             mq660208 nginx: worker process

TCP(Java实现版本)

Java版(TCP)のリバースプロキシを作っていきます。
エディタを使って、/etc/haproxy/haproxy.cfgのconfigを

#Global
global

    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats


# Gateway Settings
defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5000
    timeout client  50000
    timeout server  50000


# MC Gateway
listen minecraft01
    bind 0.0.0.0:25565
    mode tcp
    option tcplog
    balance roundrobin
    server mc01 <BungeeCord-IP>:<Port> send-proxy-v2

のようにしてください。

とはBungeeCordが動いているサーバーのIP(ドメイン):ポートにしてください。
例: 123.456.78.90:25565、mc.example.com:25566

設定したら、念のために

haproxy -f /etc/haproxy/haproxy.cfg -c

当你执行这一步之后,需要确认配置文件是有效的。

systemctl restart haproxy

重启一下吧。
如果在重启后能够成功连接到VPS的IP地址:端口号,那就算是成功了。

UDP(BE版)

進入製作 BE版(UDP)的反向代理。

使用編輯器,編輯/etc/nginx/nginx.conf中的config。

worker_processes 2;

events {
    worker_connections 1024;
}

stream {
    log_format proxy '$remote_addr [$time_local] '
                     '$protocol $status $bytes_sent $bytes_received '
                     '$session_time "$upstream_addr" '
                     '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

    upstream minecraft_udp_upstreams {
        server <BungeeCord-IP>:<Port>;
    }

    server {
        error_log /var/log/nginx/error.log debug;
        access_log /var/log/nginx/access.log proxy buffer=32k;
        listen 19132 udp;
        proxy_pass minecraft_udp_upstreams;
        proxy_timeout 120s;
        proxy_protocol on;
    }
}

请以这种方式做。

Java版と同様とはBungeeCordが動いているサーバーのIP(ドメイン):ポートにしてください。
例: 123.456.78.90:19132

upstreamの部分でドメインを使用する場合、ngx_upstream_jdomainのようなドメインを解決できるものが必要となるらしいです。

为了确认一下语法,请进行语法检查。

$nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

となっているのを確認したら

$systemctl restart nginx

让我们重新启动。
重新启动后,如果可以成功连接到VPS的IP地址和端口号,则表示成功。

最后

試してみた感じでは特に問題もなく安定しており、期待通りの動作となりました。
皆さんも、是非試してみてはいかがでしょうか。

NginxのUDPでproxy_protocolを使う方法に関する記事がかなり少なく、数時間ぐらい格闘しました。
解決策を見つけられた時の達成感はやっぱり最高

请参考以下文章。

 

bannerAds