Nginx系列:几款负载均衡第三方插件的安装与使用

点击上方「蓝字」关注我们

0x01:插件说明

  • nginx_upstream_hash:url_hash是nginx的第三方模块,nginx本身不支持,需要第三方模块。nginx在做负载均衡的时,把转发的URL以hash的形式保存。这样可以保证同一个URL始终分给同一个real server,来提高访问速度。

官网:https://github.com/evanmiller/nginx_upstream_hash
  • nginx-upstream-fair:upstream-fair是比内建的负载均衡更加智能的负载均衡模块,它采用的不是内建负载均衡使用的轮换的均衡算法,而是可以根据页面大小、加载时间长短智能的进行负载均衡。

官网:https://github.com/gnosek/nginx-upstream-fair
  • ngx_http_consistent_hash:通过一致性哈希算法来选择合适的后端节点

官网:https://github.com/replay/ngx_http_consistent_hash
https://www.nginx.com/resources/wiki/modules/consistent_hash/

0x02:安装说明

  • 先分别下载这三款插件

wget https://github.com/gnosek/nginx-upstream-fair/archive/master.zip -O nginx-upstream-fair.zip
wget https://github.com/evanmiller/nginx_upstream_hash/archive/master.zip -O nginx_upstream_hash.zip
wget https://github.com/replay/ngx_http_consistent_hash/archive/master.zip -O ngx_http_consistent_hash.zip
  • 解压对应的压缩包

unzip nginx-upstream-fair-master.zip 
unzip nginx_upstream_hash-master.zip 
unzip ngx_http_consistent_hash-master.zip 
  • 查看旧Nginx的安装配置参数

 /usr/local/nginx/sbin/nginx -V

  • 重新编译添加负载均衡模块

./configure --prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-stream_ssl_preread_module \
--with-http_ssl_module --with-http_image_filter_module \
--add-module=/home/huangjinjin/桌面/fastfds/nginx_upstream_hash-master  \
--add-module=/home/huangjinjin/桌面/fastfds/nginx-upstream-fair-master  \
--add-module=/home/huangjinjin/桌面/fastfds/ngx_http_consistent_hash-master

关键编译参数

  • 安装nginx

make

(1) 对于nginx_upstream_hash会出现以下错误

 ngx_http_upstream_hash_module 的多重定义

说明Nginx内部包含了一个同名的模块。

  • 需要对nginx_upstream_hash进行一定的代码修改,ngx_http_upstream_hash_module定义

ngx_module_t  ngx_http_upstream_hash_module = {
    NGX_MODULE_V1,
    &ngx_http_upstream_hash_module_ctx,    /* module context */
    ngx_http_upstream_hash_commands,       /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};

修改成

ngx_module_t  ngx_http_upstream_hash_module_ext = {
    NGX_MODULE_V1,
    &ngx_http_upstream_hash_module_ctx,    /* module context */
    ngx_http_upstream_hash_commands,       /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};

应用这个结构体的代码一并修改。

  • 修改config配置文件

ngx_addon_name=ngx_http_upstream_hash_module_ext
HTTP_MODULES="$HTTP_MODULES ngx_http_upstream_hash_module_ext"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_upstream_hash_module_ext.c"
have=NGX_HTTP_UPSTREAM_HASH . auto/have
~                                                
  • c文件名字也改下

ngx_http_upstream_hash_module.c 
改成
ngx_http_upstream_hash_module_ext.c 

这样重新configure和make才能成功

(2)对于nginx-upstream-fair会出现以下错误

nginx-upstream-fair/ngx_http_upstream_fair_module.c:543:28: 
error: ‘ngx_http_upstream_srv_conf_t’ has no member named ‘default_port’

参考:http://www.mamicode.com/info-detail-89267.html

到nginx源码目录找到src/http/ngx_http_upstream.h文件ngx_http_upstream_srv_conf_s结构添加in_port_t default_port;

经过错误修复,编译成功后,把objs目录下的nginx文件拷贝到/usr/local/nginx目录即可:

cp ./objs/nginx /usr/local/nginx

0x03: 基本语法

  • nginx_upstream_hash

upstream backend_server {
    server 127.0.0.1:5000;
    server 127.0.0.1:5001;
    server 127.0.0.1:5002;
    hash      $request_uri;
    hash_again  10;  # default 0
}
  • nginx-upstream-fair

upstream mongrel {
    fair;
    server 127.0.0.1:5000;
    server 127.0.0.1:5001;
    server 127.0.0.1:5002;
  }
  • ngx_http_consistent_hash

upstream somestream {
  consistent_hash $request_uri;
  server 127.0.0.1:5000;
  server 127.0.0.1:5001;
  server 127.0.0.1:5002;
}

以下三种任君选择,不过nginx_upstream_hash与nginx-upstream-fair已经很久没更新了

扫码二维码

获取更多精彩

Java乐园

有用!分享+在看☟

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值