nginx使用stream反向代理mysql

本文详细介绍了如何使用Nginx配置TCP负载均衡,包括Nginx版本要求、安装步骤、配置文件修改及重启方法。适用于需要进行数据库等TCP服务负载均衡的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、nginx版本至少1.9的版本,版本太低没有stream这个功能。

2、下载nginx

安装nginx,可以参考:https://blog.youkuaiyun.com/shardy0/article/details/88172398

 

./configure --prefix=/www/wdlinux/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/www/tools/pcre-8.42 --with-zlib=/www/tools/zlib-1.2.11 --with-stream

./configure --prefix=/www/wdlinux/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/www/pcre-8.42/ --with-zlib=/www/tools/zlib-1.2.11  --with-stream

Nginx在安装的时候,需要加上一个参数:--with-stream

3. nginx安装完成后,编辑nginx.conf文件,指令:

vim  nginx/conf/nginx.conf

在文件的最下方(位置也可以不是最下方,stream必须和http平级)添加如下内容:

               

cd /www/wdlinux/nginx/conf/

编写tcp.conf文件

vi tcp.conf

    stream {

      upstream cloudsocket {
        hash $remote_addr consistent;

                             server 数据库实际IP:数据库实际端口号 weight=5 max_fails=3 fail_timeout=30s;

                              server 数据库实际IP:数据库实际端口号 weight=5 max_fails=3 fail_timeout=30s;
      }
      server {
        listen 数据库代理端口号
        proxy_connect_timeout 10s;
        proxy_timeout 300s; 
        proxy_pass cloudsocket;
      }
    }

4、重启。

注:需要先停止pkill nginx,然后在启动刚才能生效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值