nginx代理转发数据库连接

# 安装 nc(netcat)
yum install -y nc

# 验证安装成功
nc --version  # 输出 nc 版本即说明安装成功


nc -zv 127.0.0.1 8086
Ncat: Version 7.92 ( https://nmap.org/ncat )
Ncat: Connected to 127.0.0.1:8086.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.
代表连接成功

步骤 1:检查 Nginx 是否支持 TCP 代理
执行以下命令,查看 Nginx 编译模块:
bash
运行
nginx -V 2>&1 | grep --color=auto stream
如果输出包含 --with-stream,说明已支持;若没有,需重新编译 Nginx(附简易编译步骤)。


(可选)重新编译 Nginx 增加 stream 模块
bash
运行
# 1. 下载对应版本的 Nginx 源码(以 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

# 2. 查看原有编译参数(复制下来)
nginx -V 2>&1 | grep configure | sed 's/--prefix=.*nginx/--prefix=\/usr\/local\/nginx/'

# 3. 重新编译(追加 --with-stream,替换原有参数)
./configure 原有编译参数 --with-stream
make && make install

# 4. 验证
nginx -V 2>&1 | grep stream

步骤 2:配置 Nginx TCP 代理
Nginx 的 TCP 代理配置在 stream 块中(与 http 块同级),而非 http 内部。
1. 编辑 Nginx 主配置文件
默认配置文件路径:/usr/local/nginx/conf/nginx.conf(或 /etc/nginx/nginx.conf)
bash
运行
vim /usr/local/nginx/conf/nginx.conf
2. 添加 stream 代理配置
在 http 块外部添加 stream 块,完整示例如下:
nginx
# 全局配置
user  root;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

# TCP 代理核心配置(与 http 同级)
stream {
    # 连接超时配置(可选,根据需要调整)
    proxy_connect_timeout 10s;
    proxy_timeout 300s;  # 数据库连接超时(建议设大些)
    proxy_buffer_size 4k;

    # 定义后端数据库服务器组(单节点)
    upstream kingbase_db {
        server 1.1.1.1:54321;  # Kingbase 数据库地址+端口
    }

    # 监听 8086 端口,代理到数据库
    server {
        listen 8086;  # Nginx 监听的端口(本地 DBeaver 连接此端口)
        proxy_pass kingbase_db;  # 转发到数据库组
    }
}

# 原有 http 配置(无需修改)
http {
    include       mime.types;
    default_type  application/octet-stream;
    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;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
步骤 3:验证配置并重启 Nginx
bash
运行
# 1. 检查配置语法是否正确
nginx -t

# 2. 重启 Nginx(生效配置)
nginx -s reload

# (若重启失败,用强制重启)
systemctl restart nginx  # 或 /usr/local/nginx/sbin/nginx -s stop && /usr/local/nginx/sbin/nginx


或配置
stream {
#配置这一条
    include /mnt/www/server/panel/vhost/nginx/tcp/*.conf;
}

步骤 1:创建 TCP 代理子配置文件
你的 stream 块中包含了 /mnt/www/server/panel/vhost/nginx/tcp/*.conf,因此需要在该目录下新建数据库代理配置文件:
bash
运行
# 1. 确认目录存在(不存在则创建)
mkdir -p /mnt/www/server/panel/vhost/nginx/tcp/

# 2. 新建 Kingbase 代理配置文件(命名建议:kingbase_8086.conf)
vim /mnt/www/server/panel/vhost/nginx/tcp/kingbase_8086.conf
步骤 2:写入代理配置内容
在新建的 kingbase_8086.conf 中添加以下内容(适配你已有的 stream 日志格式,无需重复定义):
nginx
# Kingbase V8 数据库 TCP 代理(8086 端口)
upstream kingbase_db {
    server 1.1.1.1:54321;  # 目标数据库 IP+端口
}

server {
    listen 8086;                # Nginx 监听的代理端口
    proxy_connect_timeout 10s;  # 连接数据库超时时间
    proxy_timeout 300s;         # 数据库长连接超时(避免被断开)
    proxy_buffer_size 4k;       # 缓冲区大小
    proxy_pass kingbase_db;     # 转发到数据库节点
}

 

posted @ 2025-12-19 11:13  全琪俊  阅读(2)  评论(0)    收藏  举报