Nginx upstream-fair模块 nginx_upstream_check_module后台服务器健康检测模块 安装 负载均衡配置

前言

为了实现Nginx负载均衡,根据统计服务器后端节点响应时间,优先选举相应时间较快的服务器(fair);以及在服务器后端节点挂掉(端口关闭,服务器宕机等)后,能自动屏蔽,选择存活的服务器(check)功能。需要在原生的Nginx上安装fair、check模块。

程序

nginx-1.14.0 nginx程序源码

下载地址:http://nginx.org/download/nginx-1.14.0.tar.gz

nginx-upstream-fair-master fair模块源码

官方github下载地址:https://github.com/gnosek/nginx-upstream-fair
说明:如果从github下载最新版本,在安装到nginx 1.14.0版本时,会报出编译错误。需要对源码做一些修改,修改参照(如果你看到这篇文章时,github主已经修改了该bug,或者你用的是nginx 1.14.0以下版本,请忽视...):https://github.com/gnosek/nginx-upstream-fair/pull/27/commits/ff979a48a0ccb9217437021b5eb9378448c2bd9e
对于比较懒的童鞋,这里提供了已经修改好的源码包:https://files.cnblogs.com/files/ztlsir/nginx-upstream-fair-master.zip

nginx_upstream_check_module-master check模块源码

下载地址:https://github.com/yaoweibin/nginx_upstream_check_module

Nginx负载均衡反向代理方式

  • 轮询(默认)
  • ip_hash
  • weight
  • fair(第三方)
  • url_hash(第三方)。

系统

Ubuntu 14.04

 

目录

  • upstream-fair模块
  • upstream_check_module后台服务器健康检测模块
  • Nginx安装
  • 负载均衡配置
  • 心得

upstream-fair模块

解压 upstream-fair 包

ztlsir@ztlsir-Virtual-Machine:/tmp$ unzip nginx-upstream-fair-master.zip  

 

upstream_check_module后台服务器健康检测模块

解压下载的 upstream_check_module 包

ztlsir@ztlsir-Virtual-Machine:/tmp$ unzip nginx_upstream_check_module-master.zip

给 upstream-fair 扩展 upstream_check_module 的 upstream_fair.patch 补丁。否则,当负载均衡采用fair模式时,fair将会不支持check

ztlsir@ztlsir-Virtual-Machine:/tmp$ cd nginx-upstream-fair-master/ #进入才将解压的upstream-fair源码目录 vz@vz-Virtual-Machine:/tmp/nginx-upstream-fair-master$ patch -p1 < ../nginx_upstream_check_module-master/upstream_fair.patch #打补丁,对patch有疑问的童鞋,自行百度

Nginx安装

解压Nginx 1.14.0安装包

ztlsir@ztlsir-Virtual-Machine:/tmp$ tar zxvf nginx-1.14.0.tar.gz

给Nginx扩展 upstream_check_module 的 check_1.12.1+.patch 补丁。

ztlsir@ztlsir-Virtual-Machine:/tmp$ cd nginx-1.14.0/ #进入才将解压的nginx源码目录 vz@vz-Virtual-Machine:/tmp/nginx-1.14.0$ patch -p1 < ../nginx_upstream_check_module-master/check_1.12.1+.patch #打补丁

安装Nginx

ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-1.14.0$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --add-module=/tmp/nginx_upstream_check_module-master --add-module=/tmp/nginx-upstream-fair-master  #配置,安装目录:/usr/local/nginx
ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-1.14.0$ sudo make && sudo make install  #编译、安装,此处用sudo,因为在"/usr/local/"下创建nginx目录需要root权限

负载均衡配置

修改安装目录的拥有用户

ztlsir@ztlsir-Virtual-Machine:/tmp$ cd /usr/local/  #进入到local目录
ztlsir@ztlsir-Virtual-Machine:/usr/local$ sudo chown -R ztlsir nginx/  #修改nginx安装目录的所有者为ztlsir

修改配置文件

/usr/local/nginx/nginx.conf:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    check_shm_size 10M;
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    proxy_next_upstream error timeout invalid_header;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #引用vhosts文件夹下的配置文件
    include vhosts/*.conf;
 
}

/usr/local/nginx/vhosts的目录文件

vz@vz-Virtual-Machine:/usr/local/nginx/conf/vhosts$ ls
1001.conf  1002.conf  1003.conf

vhosts是自定义的文件夹。在该文件夹下,是根据端口进行负载配置的,此处选取其中一个文件进行展示,其他文件都大同小异。

/usr/local/nginx/vhosts/1001.conf:

    upstream localhost1001 {
            fair;
            server  192.168.120.10:1001;
            server  192.168.120.11:1001;
            check interval=3000 rise=2 fall=5 timeout=1000 type=http;
            #在Web服务配置了默认文档时,可以使用如下配置
            #但当服务没有配置时,需要修改为:check_http_send "GET /temp.html HTTP/1.0\r\n\r\n";
            #其中“/temp.html”表示根目录下的一个temp.html文件
            check_http_send "HEAD / HTTP/1.0\r\n\r\n";
            check_http_expect_alive http_2xx http_3xx;
    }
    server {
        listen       1001;
        server_name  localhost;

        #charset koi8-r;
    
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://localhost1001;
            proxy_redirect  off;
            proxy_set_header Host $host:1001;
            proxy_connect_timeout       100s;
            proxy_read_timeout          150s;
            proxy_send_timeout          100s;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    } 

心得

在实现fair和check模块时,走了很多坑。最初想着直接用淘宝的Tengine就不用安装check模块了,但是在安装上fair模块后,运行Tengine总是报错。就算是修改了fair代码,也还是不行,最后不得已又换回了Nginx。

猜你喜欢

转载自www.cnblogs.com/ztlsir/p/8945043.html
今日推荐