Nginx 1.9.0 以上版本增加对tcp协议的支持,默认编译安装是不带此模块的,所以要加上编译参数。
模块参数 --with-stream –-with-stream_ssl_module
例如:
./configure --prefix=/usr/local/nginx --with-stream --with-http_ssl_module --with-stream_ssl_module
make && make install
加入系统服务:
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl restart nginx
修改配置文件:
vim /usr/local/nginx/conf/nginx.conf
在http
的同级添加stream
stream {
upstream mysqls {
hash $remote_addr consistent;
server 192.168.58.143:3306 weight=5 max_fails=3 fail_timeout=30s;
server 192.168.58.142:3306 weight=1 max_fails=3 fail_timeout=30s;
}
server {
listen 9945;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass mysqls;
}
}
http {
...
}