nginx添加nginx-sticky-module模块步骤的实现
nginx-sticky-module模块是nginx实现负载均衡的一种方案,和ip_hash负载均衡算法会有区别的
ip_hash 根据客户端ip将请求分配到不同的服务器上.
sticky 根据服务器个客户端的cookie,客户端再次请求是会带上此cookie,nginx会把有次cookie的请求转发到颁发cookie的服务器上.
安装Sticky
1. 下载sticky
# 创建目录 mkdir /usr/local/nginx/module cd /usr/local/nginx/module #下载sticky wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz tar xf master.tar.gz #解压 tar -zxvf master.tar.gz
2. 编译nginx
# 进入nginx安装目录 cd /usr/local/nginx-1.9.9 ./configure --prefix=/usr/local/nginx-1.9.9 \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/run/nginx.pid \ --error-log-path=/usr/local/nginx/logs/error.log \ --http-log-path=/usr/local/nginx/logs/access.log \ --with-pcre \ --user=nginx \ --group=nginx \ --with-stream \ --with-threads \ --with-file-aio \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --add-module=/usr/local/nginx/module/nginx-sticky-module #在此载入sticky模块 make #更新检测 make upgrade
3. 查看模块是否被载入
cd /usr/local/nginx-1.9.9 ./sbin/nginx -V
如下图表表示添加成功

4. 使用
upstream backend {
sticky name=ngx_cookie expires=6h;
server 192.168.31.240:8080 weight=3 max_fails=3 fail_timeout=10s;
server 192.168.31.241:8080 weight=3 max_fails=3 fail_timeout=10s;
server 192.168.31.242:8080 weight=6 max_fails=3 fail_timeout=10s;
server 192.168.31.243:8080;
server 192.168.31.244:8080 down;
}name: cookie的名称
expire: 有效期
5. nginx 启动 停止 重启命令
/usr/local/nginx-1.9.9/sbin/nginx -s start /usr/local/nginx1.9.9/sbin/nginx -s stop /usr/local/nginx1.9.9/sbin/nginx -s reload
到此这篇关于nginx添加nginx-sticky-module模块步骤的实现的文章就介绍到这了,更多相关nginx-sticky-module模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Nginx 502 bad gateway错误解决的九种方案及原因
一般在访问某些网站或者我们在做本地测试的时候,服务器突然返回502 Bad Gateway Nginx,这种问题相信大家也遇到不少了,下面这篇文章主要给大家介绍了关于Nginx 502 bad gateway错误解决的九种方案及原因,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2022-08-08
nginx出现500 Internal Server Error错误的解决方法
这篇文章主要介绍了nginx出现500 Internal Server Error错误的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或工作有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2024-09-09
Nginx配置 location模块实现路由(反向代理、重定向)功能
本文主要介绍了Nginx配置 location模块实现路由(反向代理、重定向)功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-04-04
Nginx中报错:Permission denied与Connection refused的解决
这篇文章主要给大家介绍了在Nginx中报错:13: Permission denied与111: Connection refused的解决方法,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。2017-04-04


最新评论