Nginx代理MySQL,Redis,Oracle的配置

把nginx代理mysql,需要添加stream模块  

stream用于TCP/UDP数据流的代理和负载均衡,转发TCP消息。

ngx_stream_core_module模块由1.9.0版提供。 

进入nginx的sbin目录 。./nginx -V 查看是否安装stream模块。

已经安装stream会显示--with-stream ,否则就是没有安装。

$ cd /usr/local/nginx/sbin/
$ ./nginx -V
nginx version: nginx/1.21.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
configure arguments: --with-stream

如果没有安装stream模块,则要安装下。

到nginx的解压目录,进行配置( ./configure  --with-stream),编译(make)。

如果之前没有安装nginx的话,全新安装则时行配置( ./configure  --with-stream),编译(make),安装(make install)

$ cd /usr/local/nginx-1.21.0
$ ./configure   --with-stream
.....
$ make

新编译后的 nginx在 objs里, 先到/usr/local/nginx/sbin/目录 停掉ningx,

备份下nginx 。如果全新安装就不用备份了。

$ cd /usr/local/nginx/sbin/
$ ./nginx -s stop
$ cp nginx nginx.bak

把新的nginx复制过来(全新安装不需要)。

$ cp /usr/local/nginx-1.21.0/objs/nginx /usr/local/nginx/sbin/

修改配置文件

vi /usr/local/nginx/conf/nginx.conf

代理192.168.0.2 和 192.168.0.3 分别用本服务器ip:25674 ,25675访问。配置如下:


#user  nobody;
user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


stream {
# 代理MYSQL 1
    upstream mysql_01{
       hash $remote_addr consistent;
       server 192.168.0.2:3306 weight=5 max_fails=3 fail_timeout=30s;
    }
    server {
       listen 25674; # 数据库服务器监听端口
       proxy_connect_timeout 10s;
       proxy_timeout 300s; # 设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
       proxy_pass mysql_01;
    }
# 代理MYSQL 2
	upstream mysql_02{
       hash $remote_addr consistent;
       server 192.168.0.3:3306 weight=5 max_fails=3 fail_timeout=30s;
    }
    server {
       listen 25675; # 数据库服务器监听端口
       proxy_connect_timeout 10s;
       proxy_timeout 300s; # 设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
       proxy_pass mysql_02;
    }

# 代理Redis
    upstream redis {
        server 10.10.10.10:6379 max_fails=3 fail_timeout=30s;  #redis替换为真实地址
    }
    server {
        listen 8085;   # 对外暴露端口号
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass redis;
    }
# 代理Oracle
    upstream oracle {   
        server 192.168.0.200:1521 weight=1 max_fails=2 fail_timeout=30s;   #原oracle地址
    }
    server {
        listen 3335;# 反向代理后监听的端口,
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass oracle;
    }
}

http {}

启动nginx

$ cd /usr/local/nginx/sbin/
$ ./nginx 

完成。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值