Nginx反代代理MySQL配置实例

案例:一个朋友要用Nginx代理MySQL(MySQL局域网),不用NAT映射等,好吧,做个笔记。

Nginx版本:1.9.x(持tcp的负载均衡,nginx_tcp_proxy_module(姚伟斌阿里团队也可以实现))

Nginx官方模块: ngx_stream_core_module --with-stream_ssl_module(ssl协议支持,比如MySQL ssl)
官网:http://nginx.org/en/docs/stream/ngx_stream_core_module.html

1、查看现有编译

--user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module

2、重新编译:
--user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module

注意:--with-stream --with-stream_ssl_module

3、配置、检测、重启nginx:
配置:
stream {
upstream mysql {
zone myapp1 64k;
server localhost:3306 weight=1 max_fails=3 fail_timeout=30s;
#server 192.168.1.221:3306 weight=1 max_fails=2 fail_timeout=30s;   
}
server {
        listen 2188;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass mysql;
}
}

启动:
[root@autoCentos67X64 conf]# netstat -atupn|grep nginx
tcp        0      0 0.0.0.0:2188                0.0.0.0:*                  LISTEN      2359/nginx         
[root@autoCentos67X64 conf]#

注意:2188可是Nginx的端口,代理(负载)后端的MySQL。其它玩法大家可自己研究。

下面关于Nginx的文章您也可能喜欢,不妨参考下:

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2017-03/142221.htm