nginx除了可以转发http协议以外,还支持对TCP协议的分发,那就是用nginx的upstream组件
在nginx.conf最底部加入
include tcp.d/*.conf;
在同级目录下新建文件夹 tcp.d并在该文件夹内创建test.conf文件
test.conf内容
stream { upstream nginx_proxy { hash $remote_addr consistent; server 192.168.2.245:5123 weight=3; server 192.168.2.246:5123 weight=3; server 192.168.2.250:5123 weight=3; } server { listen 5123; proxy_connect_timeout 10s; proxy_timeout 30s; proxy_pass nginx_proxy; access_log /home/wwwlogs/access.log; error_log /home/wwwlogs/error.log; }}