Home > Operation and Maintenance > Nginx > How Nginx reverse proxy implements session persistence

How Nginx reverse proxy implements session persistence

WBOY
Release: 2023-05-20 19:25:20
forward
1617 people have browsed it

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;
}
Copy after login

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=/;
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template