Home > Article > Operation and Maintenance > How Nginx reverse proxy implements session persistence
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 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;
Not applicable For CDN networks, it is not suitable for situations where there is an agent in the previous stage.
2. sticky_cookie_insert:
Use sticky_cookie_insert to enable session affinity, which will cause requests from the same client to be blocked. Delivered 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 front-end proxy from the same LAN in the above ip_hash can be avoided.
Syntax:
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 cookies in the browser
domain: defines the domain of the cookie
path: defines the path for the cookie
The above is the detailed content of How Nginx reverse proxy implements session persistence. For more information, please follow other related articles on the PHP Chinese website!