Nginx adds nginx-sticky-module module steps

The nginx-sticky-module module is a solution for nginx to achieve load balancing, which is different from the ip_hash load balancing algorithm

  1. ip_hash assigns requests to different servers according to client ip.
  2. sticky According to the cookie of the server and the client, the client will bring this cookie when it requests again, and nginx will forward the request with the cookie to the server that issued the cookie.

Install Sticky

1. Download 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. Compile 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. Check if the module is loaded

cd /usr/local/nginx-1.9.9
./sbin/nginx -V

The following chart indicates that the addition is successful
insert image description here

4. use

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: the name of the cookie
expire: validity period

5. nginx start stop restart command

/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

Guess you like

Origin blog.csdn.net/weixin_42202992/article/details/131021151