nginx代理mysql

#需要用到四层负载。首先确定你的nginx支持四层负载。确定开启stream模块
[root@test-node5 conf.d]# nginx -V
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-stream_ssl_module

[root@test-node5 conf.d]# egrep -v "^$|^\s*#"  /usr/local/nginx/conf/nginx.conf 
worker_processes  1;
events {
    worker_connections  1024;
}
include conf.d/*.conf;
http {
    include       mime.types;
    default_type  application/octet-stream;
    include vhost/*.conf;
    sendfile        on;
    keepalive_timeout  65;
}
#stream要配在http同级。也可以用include。在外面单独配置。推荐使用第二种。方便管理配置文件
[root@test-node5 conf]# cat conf.d/mysql-test.conf 
stream {
	upstream mysql {
		server 192.168.0.12:3306 max_fails=3 fail_timeout=30s; 
	}
	server {
		listen 8085;
		proxy_connect_timeout 2s;  #后端服务器连接的超时时间_发起握手等候响应超时时间(默认60秒)
		proxy_timeout 900s;  #接收后端响应内容超时
		proxy_pass mysql;
	}
}
[root@test-node5 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@test-node5 conf]# nginx -s reload
#完成

posted @ 2023-02-25 14:44  安生丶  阅读(411)  评论(0编辑  收藏  举报