只有nginx1.9以上版本才支持。
本实验以阿里云ECS的centos7.4环境为实验环境,yum源用的阿里云的源,nginx采用yum安装
附上yum源
[root@LinuxNB ~]# yum makecache fast [root@LinuxNB ~]# yum install -y yum-utils [root@LinuxNB ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@LinuxNB ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/repo/epel-7.repo [root@LinuxNB ~]# yum makecache fast [root@LinuxNB ~]# yum -y install gcc gcc-c++ net-tools wget curl unzip zip dos2unix bash-completion-extras bash-completion rsync
安装nginx
[root@LinuxNB ~]# yum -y install nginx
安装完成之后首先修改主配置文件
[root@LinuxNB ~]# vim /etc/nginx/nginx.conf
在main配置区域添加如下内容(main配置区域就是主配置文件最上面部分,不被任何大括号包含)
load_module /usr/lib64/nginx/modules/ngx_stream_module.so;
如果不知道此模块地址可以全盘搜索,将找出来的路径替换上面路径
[root@proxy nginx]# find / -name "ngx_stream_module.so" /usr/lib64/nginx/modules/ngx_stream_module.so
接着在主配置文件最后添加(此部分和http区域同级别,不被http包含)
stream { log_format proxy '$remote_addr [$time_local] ' '$protocol $status $bytes_sent $bytes_received ' '$session_time "$upstream_addr" ' '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"'; access_log /var/log/nginx/tcp-access.log proxy; open_log_file_cache off; include /etc/nginx/stream.d/*.conf; }
接下来新建 /etc/nginx/stream.d/目录
mkdir -p /etc/nginx/stream.d/ vim /etc/nginx/stream.d/mysql.conf upstream mysql { #hash $remote_addr consistent; #上面这行注释就默认轮训,不注释是远程client_ip 哈希 server 192.168.1.211:3306 max_fails=3 fail_timeout=30s; server 192.168.1.212:3306 max_fails=3 fail_timeout=30s; # 只写一个服务器就实现了端口转发了。 } server { listen 3308; proxy_connect_timeout 30s; proxy_timeout 600s; proxy_pass mysql; }
接下来启动,验证。完毕,最后附上配置文件,请查看附件。
-
« 上一篇:
openresty使用lua构建灰度环境