0x01:插件说明
官网:https://github.com/evanmiller/nginx_upstream_hash
官网:https://github.com/gnosek/nginx-upstream-fair
官网: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
/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
关键编译参数
make
(1) 对于nginx_upstream_hash会出现以下错误
ngx_http_upstream_hash_module 的多重定义
说明Nginx内部包含了一个同名的模块。
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
};
应用这个结构体的代码一并修改。
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
~
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: 基本语法
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
}
upstream mongrel {
fair;
server 127.0.0.1:5000;
server 127.0.0.1:5001;
server 127.0.0.1:5002;
}
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已经很久没更新了。