Nginx reverse proxy implementation of two ways of session retention

  
 

1, ip_hash:

ip_hash uses the source address hash algorithm to always send requests from the same client to the same backend 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:

  • The session will be lost when the backend server is down;
  • Clients from the same LAN will be forwarded to The same back-end server may cause load imbalance;
  • does not apply to CDN networks, not to the case of agents in the previous paragraph.

    Second,sticky_cookie_insert:

    Use sticky_cookie_insert to enable session affinity, which causes requests from the same client to be delivered to the same server on a group of servers. The difference with ip_hash is that it is not based on IP to judge the client, but based on the cookie to judge. Therefore, it is possible to avoid the load imbalance caused by the client and the front agent from the same local area network in the above ip_hash.

    Syntax:

    upstream backend { server backend1.example.com; server backend2.example.com; sticky_cookie_insert srv_id expires=1h domain=xxxx.com path=/;}

    Description :

  • expires: Setting the time to keep cookies in the browser
  • domain: fields defining cookies
  • path: defining paths for cookies

    In addition, you can use the backend server itself to maintain session synchronization through related mechanisms, which will be described in detail later!

  • Copyright © Windows knowledge All Rights Reserved