1、nginx版本至少1.9的版本,版本太低没有stream这个功能。
2、下载nginx
安装nginx,可以参考:https://blog.csdn.net/shardy0/article/details/88172398
./configure --prefix=/www/wdlinux/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/www/tools/pcre-8.42 --with-zlib=/www/tools/zlib-1.2.11 --with-stream
./configure --prefix=/www/wdlinux/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/www/pcre-8.42/ --with-zlib=/www/tools/zlib-1.2.11 --with-stream
Nginx在安装的时候,需要加上一个参数:--with-stream
3. nginx安装完成后,编辑nginx.conf文件,指令:
vim nginx/conf/nginx.conf
在文件的最下方(位置也可以不是最下方,stream必须和http平级)添加如下内容:
cd /www/wdlinux/nginx/conf/
编写tcp.conf文件
vi tcp.conf
stream {
upstream cloudsocket {
hash $remote_addr consistent;
server 数据库实际IP:数据库实际端口号 weight=5 max_fails=3 fail_timeout=30s;
server 数据库实际IP:数据库实际端口号 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 数据库代理端口号;
proxy_connect_timeout 10s;
proxy_timeout 300s;
proxy_pass cloudsocket;
}
}
4、重启。
注:需要先停止pkill nginx,然后在启动刚才能生效。