Explain in detail the two ways of Nginx reverse proxy to achieve session persistence (session)
1. Ip_hash:
Ip_hash uses the source address hashing 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 easy to use, but has the following problems:
When the back-end server goes down, session will be lost; clients from the same LAN will be forwarded to the same back-end server, which may lead to load imbalance; it is not suitable for CDN network and does not apply to the situation where there are agents in the front segment.
2. Sticky_cookie_insert:
Use sticky_cookie_insert to enable session kinship, which causes requests from the same client to be passed to a set of servers on the same server. Unlike ip_hash, it does not judge the client based on IP, but on cookie. Therefore, the load imbalance caused by the clients and front-segment agents from the same local area network 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 cookie in the browser domain: define the domain path of cookie: define the path for cookie
In addition, you can also use the back-end server itself to keep session synchronized through related mechanisms, which will be described in more detail later!
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.