How Nginx Reverse Proxy Implements Session Persistence

11-09-2023

I. ip_hash:

Ip_hash uses the source address hashing algorithm, and always sends requests from the same client to the same back-end server unless the server is unavailable.

Ip_hash syntax:

upstream backend { ip_hash; server backend1.example.com ; server backend2.example.com ; server backend3.example.com down; server backend4.example.com; }

Ip_hash is simple and easy to use, but it has the following problems:

When the back-end server goes down, the session will be lost;

Clients from the same LAN will be forwarded to the same backend server, which may lead to load imbalance;

Not applicable to cdn network, not applicable to the situation that there is an agent in the front section.

Second, sticky_cookie_insert:

Use sticky_cookie_insert to enable session affinity, which will cause requests from the same client to be delivered to a group of servers on the same server. Different from ip_hash, it judges the client not based on ip, but based on cookie. Therefore, it can avoid the load imbalance caused by clients and front-end agents from the same local area network in the above ip_hash.

Grammar:

upstream backend { server backend1.example.com ; server backend2.example.com ; sticky_cookie_insert srv_id expires=1h domain=toxingwang.com path=/; }

Description:

Expires: Set the time to keep cookie in the browser.

Domain: defines the domain of the cookie.

Path: define the path for the cookie.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us