首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx安装负载均衡配置 fair check扩展

Nginx安装负载均衡配置 fair check扩展

作者头像
拓荒者
发布2019-03-11 14:25:00
1.7K0
发布2019-03-11 14:25:00
举报
文章被收录于专栏:运维经验分享运维经验分享

前言

本文主要是针对Nginx安装、负载均衡配置,以及fair智能选举、check后端节点检查扩展功能如何扩展,进行讲解说明。

fair模块: upstream-fair,“公平的”Nginx 负载均衡模块,增强了Nginx 提供的round-robin负载均衡算法,可以跟踪后端服务器的负载来分发请求。

chek模块:nginx_upstream_check_module,更专业的负载均衡器内节点的健康检查。这个由淘宝技术团队开发的 nginx 模块 nginx_upstream_check_module,通过它可以用来检测后端 realserver 的健康状态。如果后端 realserver 不可用,则所有的请求就不会转发到该节点上。

目录

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

前期准备

程序

为了方便安装,所有的源码下载,都放在 /tmp/ 目录下。

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-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源码目录 
ztlsir@ztlsir-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源码目录 
ztlsir@ztlsir-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权限
复制代码
复制代码

在配置和安装时,可能会出现pcre、ssl、complier、zlib等相关错误,可以参考:Linux Nginx安装以及可能出现错误

负载均衡配置

修改安装目录的拥有用户

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的目录文件

ztlsir@ztlsir-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;
        }
    }
复制代码
复制代码

 在如上配置中,负载了两个后端节点: 192.168.120.10:1001 和192.168.120.11:1001 ,负载选举模式采用 fair 

 check 配置了健康检查的基本规则。 interval=3000 每隔3秒(3000毫秒)检测一次, rise=2 请求2次正常则标记 realserver状态为up, fall=5 检测 5 次都失败则标记 realserver的状态为down, timeout=1000 超时时间为1秒,超时即失败。以下为详细说明:

复制代码
复制代码
Syntax: check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
Default: 如果没有配置参数,默认值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
Context: upstream

Check指令参数意义:
    - interval:向后端发送的健康检查包的间隔。
    - fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。
    - rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。
    - timeout: 后端健康请求的超时时间。
    - default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。
    - type:健康检查包的类型,现在支持以下多种类型
        - tcp:简单的tcp连接,如果连接成功,就说明后端正常。
        - ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包。
        - http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。
        - mysql: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活。
        - ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。
    - port: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-1.4.0。
复制代码
复制代码

 check_http_send 配置了健康检查包发送的内容。 "HEAD / HTTP/1.0\r\n\r\n" 表示采用 HEAD 方法,访问路径为 / 程序根目录(默认文档)。以下为详细说明:

复制代码
复制代码
Syntax: check_http_send http_packet
Default: "GET / HTTP/1.0\r\n\r\n"
Context: upstream

    -http_packet:当采用长连接进行健康检查时,需在该指令中添加keep-alive请求头,如:"HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。 同时,在采用"GET"方法的情况下,请求uri的size不宜过大,确保可以在1个interval内传输完成,否则会被健康检查模块视为后端服务器或网络异常
复制代码
复制代码

 check_http_expect_alive 指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。以下为详细说明:

Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
Default: http_2xx | http_3xx
Context: upstream

关于 check 模块的详细说明,可以参照:ngx_http_upstream_check_module

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/07/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 目录
  • 前期准备
    • 程序
      • nginx-1.14.0 nginx程序源码
      • nginx-upstream-fair-master fair模块源码
      • nginx_upstream_check_module-master check模块源码
    • Nginx负载均衡反向代理选举方式
      • 系统
      • upstream-fair模块
      • upstream_check_module后台服务器健康检测模块
      • Nginx安装
      • 负载均衡配置
        • 修改配置文件
        相关产品与服务
        负载均衡
        负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档