Using Nginx Sticky Module
This tutorial will walk you through configuring Nginx + Nginx
Sticky Module as a load balancer to direct client requests to a group of
PowerServer Web APIs. You will have to configure Nginx + Nginx Sticky
Module as a load balancer and use the sticky cookie to support session
persistence. With sticky cookies, the requests from the same user
session are always directed to the same PowerServer Web APIs.
Step 1: Download the source code of Nginx and Nginx Sticky Module
separately.
-
Download the source code of Nginx from https://nginx.org.
-
Download the source code of Nginx Sticky Module from https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src/master/.
Step 2: Re-compile Nginx to include the Nginx Sticky
Module.
|
1 2 3 |
./configure ... --add-module=/absolute/path/to/nginx-sticky-module-ng make make install |
Step 3: Check if any syntax error in the Nginx configuration file,
and then restart Nginx for the changes to take effect.
|
1 |
nginx -t |
|
1 |
systemctl restart nginx |
Step 4: Configure Nginx to direct requests to the PowerServer Web
APIs group using the sticky cookie load-balancing method.
-
Open the nginx.conf file in a text editor (nginx.conf is
located in /etc/nginx/ in Linux). -
Under the “server” block that defines the virtual server, add
another “server” block and “upstream” block that define the server
group.-
The “upstream” directive defines the PowerServer Web APIs
group.In the following example, the “upstream” block consists of
two server configurations; it could consists of more.The “upstream” block also consists of the “sticky”
directive which defines that the sticky-cookie load-balancing
method will be used when determining which server in the group
the request will be directed to. -
The “listen” directive specifies the port number for the
requests. The Web API URL should point to this port
number. -
The “proxy_pass” directive forwards the request to the
server group defined in the “upstream” directive, therefore, it
should match with the upstream name.
The following configuration defines a PowerServer Web APIs
group named webapi which consists
of three servers: https://172.16.100.34:6000/,
https://172.16.100.35:6000/, and https://172.16.100.36:6000/ and
requests made to the Web API URL: https://<server>:8090/ will
be redirected to the PowerServer Web APIs group.123456789101112131415161718upstream webapi{server 172.16.100.34:6000;server 172.16.100.35:6000;server 172.16.100.36:6000;sticky name=route hash=sha1 expires=1h;}server {listen 8090;server_name localhost;location / {proxy_pass https://webapi;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $remote_addr;}} -