Nginx reverse proxy two ways to achieve session retention

1. ip_hash:

ip_hash uses the source address hash algorithm to always send 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 back-end server, which may cause load imbalance;

It does not apply to CDN networks, and does not apply to the situation where there are agents in the previous paragraph.

 

二、sticky_cookie_insert:

Use sticky_cookie_insert to enable session affinity, which will cause requests from the same client to be passed to a group of servers on the same server. The difference from ip_hash is that it does not judge the client based on IP, but based on cookie. Therefore, the load imbalance caused by the client and the previous proxy from the same LAN in the above ip_hash can be avoided.

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 for keeping cookies in the browser

domain: defines the domain of the cookie

path: Define the path for the cookie

Guess you like

Origin blog.csdn.net/weixin_42182501/article/details/101353691