追加安装sticky模块
下载地址:http://code.google.com/p/nginx-sticky-module/downloads/list
或者:
wget https://code.google.com/p/nginx-sticky-module/downloads/detail?name=nginx-sticky-module-1.1.tar.gz
解压:
tar -xzvf nginx-sticky-module-1.1.tar.gz
进入nginx-1.9.3目录
编译时出现错误:
cc1: warnings being treated as errors
ngx_http_sticky_module.c: In function ‘ngx_http_get_sticky_peer’:
/ngx_http_sticky_module.c:333: 警告:赋值时将整数赋给指针,未作类型转换
ake[1]: *** [objs/addon/nginx-sticky-module-1.1/ngx_http_sticky_module.o] 错误 1
根据资料 把ngx_http_sticky_misc.c 的281行修改如下
原digest->len = ngx_sock_ntop(in,digest
->data, len, 1);
改后digest->len = ngx_sock_ntop(in,sizeof(struct sockaddr_in),digest
->data, len, 1);
对nginx-sticky-module-1.1/ngx_http_sticky_module.c文件也进行修改(主要是1.9.x版本会出现这问题)
修改两个地方,如下图:
第6行添加:
#include
第340行左右修改(iphp->rrp.current = iphp->selected_peer;)为:
#if defined(nginx_version) && nginx_version >= 1009000
iphp->rrp.current = peer;
#else
iphp->rrp.current = iphp->selected_peer;
#endif
可参考如下两个链接:
https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/commits/51efa124a4330e194ef651e597a6038a6f7979dc
https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/issues/18/wont-compile-on-nginx-190
在nginx-1.9.3目录,重新添加模块,编译
./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/lab/re/pcre-8.36 \
--with-zlib=/lab/re/zlib-1.2.8 \
--with-openssl=/lab/re/openssl-1.0.2d \
--with-http_stub_status_module \
--with-http_realip_module \
--add-module=/lab/re/nginx-sticky-module-1.1
make
(不要make install,要不然就相当于重新安装了。)
复制编译后的二进制文件到目录(拷贝前把服务停掉)
cp /usr/local/nginx/nginx /usr/local/nginx/nginx.bak
cp /lab/re/nginx-1.9.3/objs/nginx /usr/local/nginx/nginx
/etc/init.d/nginx stop
/etc/init.d/nginx start
netstat -ano|grep 80
修改配置nginx文件,使插件sticky生效
vi /usr/local/nginx/nginx.conf
在upstream中添加sticky;
http {
upstream myproject{
#添加sticky模块后加入此配置
sticky;
#被代理的服务
server 192.168.1.100:8081;
server 192.168.1.101:8080;
}
server {
#nginx监听的端口
listen 80;
server_name localhost;
location / {
#代理
proxy_pass http://myproject;
}
}
}
测试配置文件是否OK
/usr/local/nginx/nginx -t
重载配置文件
/usr/local/nginx/nginx -s reload