HTTP Persistence
Contents |
HTTP Persistence
Synopsis
The "persistence" module enables websites to forward requests of the same session to the same server. It works together with the load balancing module. When a new request is received, the load balancing module forwards it to server A, and a session is established. After receiving the next request of this session, the persistence module will forward it to server A directly.
The persistence module provides users two ways to mark sessions:
- Inserting HTTP cookie.
- Monitoring existing HTTP cookie in HTTP requests.
- Session-based features to identify the session.
Directives
persistence
Syntax | persistence mode [cookie_name=name] [monitor_cookie=name] [timeout=timeout] |
Context | upstream |
Enables session persistence. There are two modes: insert_cookie (inserting cookie) and session_based. The insert_cookie mode has 2 sub modes: inserting cookie directly and inserting cookei based on monitoring an existing cookie. Timeout is in minute.
Example:
To insert cookie:
upstream pool1 { persistence insert_cookie cookie_name=senginxforward timeout=30; server 192.168.1.101; server 192.168.1.102; }
To listen to existing session cookie:
upstream pool2 { least_conn; persistence insert_cookie cookie_name=senginxforward monitor_cookie=JSESSIONID timeout=30; server 192.168.1.101; server 192.168.1.102; }
Session-based mechanism (1.5.7 and later versions of SEnginx provided):
upstream pool2 { least_conn; persistence session_based; server 192.168.1.101; server 192.168.1.102; } server { session on; ... ... proxy_pass http://pool2; }
Internals
Inserting HTTP Cookie
When receiving a new request, the persistence module will insert to the response a "Set-Cookie" header, which will make the client browser to set a cookie containing server information. This cookie will be contained in the next request sent by the client browser, and the persistence module will forward the request to the previous response server according to this cookie.
Monitoring Existing Cookie
if the server has already set a cookie to identify the session, persistence module will monitor the cookie to provide the persistence functionality.
Session-based mechanism to maintain session
A server SEnginx open session in the case of function, persistence module based on the information recording session in session remains. In this case, persistence timeout and session timeout are same.