[Four] Nginx load balancing configuration

I. Overview

  After Nginx1.9, he began to support four load balancing, need to introduce additional modules

Second, the configuration

2.1 Environment Preparation

#测试环境 CentOS7
#Nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信。 
#stream模块默认不安装的,需要手动添加参数:–with-stream,官方下载地址:#download,根据自己系统版本选择nginx1.9或以上版本
./configure --add-module=../yaoweibin-nginx_tcp_proxy_module-b8a3028

2.2 Installation and Configuration

1) Download Nginx

wget http://nginx.org/download/nginx-1.9.10.tar.gz
#作用:实现反向代理、负载负载库

2) Download the plug nginx_tcp_proxy_module

wget https://github.com/yaoweibin/nginx_tcp_proxy_module/tarball/master
tar -zxvf master
#nginx 支持TCP转发和负载均衡的支持

3) Compile Nginx

#编译Nginx
#1.解压nginx文件
tar -zxvf nginx-1.9.10.tar.gz
#2.进入到Nginx目录
cd nginx-1.9.10
#3.下载tcp.patch最新补丁
patch -p1 <  ../yaoweibin-nginx_tcp_proxy_module-b8a3028/tcp.patch
#如果报错
-bash: patch: 未找到命令 执行 yum -y install patch 安装即可。
#4.编译Nginx
./configure --add-module=../yaoweibin-nginx_tcp_proxy_module-121c026

#5.
make && make install 
#如果报错
    In file included from ../nginx_tcp_proxy_module-master/ngx_tcp.h:32,
                     from ../nginx_tcp_proxy_module-master/ngx_tcp.c:5:
    ../nginx_tcp_proxy_module-master/ngx_tcp_upstream.h:144: error: expected specifier-qualifier-list before 'ngx_resolver_addr_t'
    make[1]: *** [objs/addon/nginx_tcp_proxy_module-master/ngx_tcp.o] Error 1
    make[1]: Leaving directory `/opt/apps_install/nginx-1.9.9'
    make: *** [build] Error 2
    
# 修改第三方模块包里的头文件,ngx_tcp_upstream.h 144 行将ngx_resolver_addr_t 改为 ngx_addr_t
cd /usr/local/yaoweibin-nginx_tcp_proxy_module-121c026 
vi  ngx_tcp_upstream.h
:set number #使vi编辑器显示行数


 


#5.继续 make && make install

4) Modify the configuration file Nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
    
### 修改为TCP模块
tcp {
   
   ### 定义多个上游服务器
   upstream  haoworld{
      ### 定义TCP模块上游服务器
      server 192.168.0.104:80001;
      server 192.168.0.104:80002;
   }
    server {
        listen       9999;
        server_name  192.168.140.133;
        ### 反向代理upstream
        proxy_pass haoworld;
    }
}

5) Test

Before the test shut down linux firewall
need TCP / UDP testing tools

Guess you like

Origin www.cnblogs.com/haoworld/p/nginx-si-ceng-fu-zai-jun-heng-pei-zhi.html