Interpretation of the use of sticky third-party modules in Nginx

Table of contents

basic introduction 

Installation of Sticky

Configure using sticky

Notes


 

We all know that Nginx has a load balancing function. Pure ip_hash, like accessing ip in the LAN, will cause ip tilt. The cookie_hash server issues a cookie to the client. Requests with specific cookies will be assigned to its publisher. The cookie needs to be browsed. server support.

basic introduction 

Sticky is a load balancing solution based on cookies. It maintains the session between the client and the back-end server based on cookies. Under certain conditions, it can ensure that the same client accesses the same back-end server. When the request comes, the server sends a cookie and says: Bring it with you next time and come to me directly.

Sticky is a module of nginx. It is an nginx load balancing solution based on cookies. By distributing and identifying cookies, the same client's requests fall on the same server. The default identification name is route.

  • 1. The client initiates an access request for the first time. After nginx receives it, it finds that there is no cookie in the request header, and then distributes the request to the back-end server in a polling manner.
  • 2. After the backend server processes the request, it returns the response data to nginx.
  • 3. At this time, nginx generates a cookie with route and returns it to the client. The value of route corresponds to the back-end server. It may be plain text or hash values ​​such as md5 and sha1.
  • 4. The client receives the request and saves the cookie with route.
  • 5. When the client sends a request next time, it will bring the route, and nginx will forward it to the corresponding back-end server based on the route value in the received cookie.

If the browser does not support cookies, sticky will not take effect and the entire module is implemented using cookies.

Installation of Sticky

download link

nginx-goodies / nginx-sticky-module-ng / Downloads — Bitbucketicon-default.png?t=N7T8https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/downloads/

  • 1) After downloading, put it into the server to decompress, and remember the decompression location.
  • 2) Enter the nginx installation file
  • 3) Configure nginx

 ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--add-module=/tmp/sticky-module/nginx-goodies-nginx-sticky-module-ng

  • The red part is modified to the path after you decompressed it. 
  •  Finally, restart Nginx

Make may report an error when compiling.

Modification: Find the directory where sticky was decompressed, enter the modified file vim ngx_http_sticky_misc.h, and add the following header file

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

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

Configure using sticky

upstream iphashserver {

        sticky;
        server www.test.com:8001;
        server www.test.com:8002;

    }
 

Specifically, additional parameters can be added as follows: 

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback] [secure] [httponly];

Parameter information description:

[name=route] Set the cookie name used to record the session 
[domain=.foo.bar] Set the domain name for the cookie 
[path=/] Set the URL path for the cookie, the default root directory 
[expires=1h] Set the survival of the cookie Period, not set by default. It will be invalid when the browser is closed. It needs to be a value greater than 1 second 
[hash=index|md5|sha1] Set whether the server identification in the cookie is in plain text or use md5 value. The default is md5 
[no_fallback] Set this item, when sticky's backend machine hangs up, nginx returns 502 (Bad Gateway or Proxy Error) instead of forwarding to other servers. It is not recommended to set [ 
secure] to enable secure cookies. HTTPS support is required 
[httponly] to allow cookies. Not leaked through JS, never used

Notes

  • .Requests from the same client may land on different back-end servers if the client initiates multiple requests at the same time. Since these requests do not carry cookies, the server will randomly select the backend server and return different cookies. When the last of these requests returns, the client's cookie will be stabilized, and the value will be based on the last cookie returned.
  • The cookie does not necessarily take effect because the cookie is initially issued by the server. If the client disables cookies, the cookie will not take effect.
  • The client may not bring cookies. When the Android client sends a request, it generally does not bring all cookies. It is necessary to clearly specify which cookies will be brought. If you want to use sticky for load balancing, please add cookies to Android development.
  • The cookie name should not have the same name as the cookie used by the business. Sticky's default cookie name is route, which can be changed to any value.
  • The first request sent by the client does not include cookies. The cookie issued by the server will only take effect on the client's next request.
  • Nginx sticky module cannot be used with ip_hash at the same time

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/132844816