nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发、代理或者负载均衡等

stream模块的用法和http模块差不多,语法基本一致,支持server,hash, listen, proxy_pass等指令,

配置实例如下:

worker_processes auto;
error_log logs/error.stream.log info;
events {
    worker_connections  1024;
}
stream {

    upstream cloudsocket {
       hash $remote_addr consistent;
      # $binary_remote_addr;
       server 172.19.20.7:3306 weight=5 max_fails=3 fail_timeout=300s;
    }


    server {
       listen 3306;#数据库服务器监听端口
       server_name mysql1.tools.hrtop.net;
       proxy_connect_timeout 120s;
       proxy_send_timeout 120;
    proxy_read_timeout 120;
    proxy_buffer_size 256k;
    proxy_buffers 8 128k;
       proxy_pass cloudsocket;
    }
}