Skip to content

下载

https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/1.2.6.tar.gz

上传解压

tar xzvf nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz

安装依赖

yum install -y openssl-devel

重新编译 Nginx

cd ~/nginx-1.18.0 ./configure --prefix=/usr/local/nginx --add-module=/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d make

如果报错

打开

/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/ngx_http_sticky_misc.c

在 12 行添加

#include <openssl/sha.h> #include <openssl/md5.h>

备份

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

替换

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

升级检测

make upgrade

检查程序中是否包含新模块

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

nginx
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream httpds {
	    sticky;
        server 192.168.44.102;
        server 192.168.44.103;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://httpds;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

就算后端没有下发 cookie 也能够进行会话保持,打开 cookie 发现多了 route 字段, 值为 00831d824e4b292f87b7823d194edaea,这就是 sticky 下发的 cookie。

nginx
upstream httpget {
    # 可以配置名字和过期时间
    sticky name=route expires=6h;

    server 192.168.44.102;
    server 192.168.44.103;
}