Detailed explanation of two ways to implement session persistence in Nginx reverse proxy

Detailed explanation of two ways to implement session persistence in Nginx reverse proxy

1. ip_hash:

ip_hash uses a 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 simple and easy to use, but it has the following problems:

  • When the backend server goes down, the session will be lost;
  • Clients from the same LAN will be forwarded to the same backend server, which may cause load imbalance;
  • It is not applicable to CDN networks, and it is not applicable to situations where there is a proxy in the front end.

2. 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 for a group of servers. The difference from ip_hash is that it does not judge the client based on the IP, but based on the cookie. Therefore, the situation in which the client and the front-end proxy from the same LAN in the above ip_hash cause load imbalance can be avoided.

grammar:

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

illustrate:

  • expires: Set the time to keep the cookie in the browser
  • domain: defines the domain of the cookie
  • path: defines the path for the cookie

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementation of multi-port mapping of nginx reverse proxy
  • Nginx reverse proxy forwards port 80 requests to 8080
  • Example of using Nginx reverse proxy to go-fastdfs
  • Nginx reverse proxy configuration removes prefix
  • Detailed explanation of Nginx reverse proxy example

<<:  How to solve the problem of Chinese garbled characters when inserting table data into MySQL

>>:  A brief discussion on how to learn JS step by step

Recommend

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...

JSONP cross-domain simulation Baidu search

Table of contents 1. What is JSONP 2. JSONP cross...

Best Practices Guide for Storing Dates in MySQL

Table of contents Preface Do not use strings to s...

Vue uses echarts to draw an organizational chart

Yesterday, I wrote a blog about the circular prog...

The whole process of IDEA integrating docker to deploy springboot project

Table of contents 1. IDEA downloads the docker pl...

Two ways to build a private GitLab using Docker

The first method: docker installation 1. Pull the...

How to use Volume to transfer files between host and Docker container

I have previously written an article about file t...

Tutorial on deploying nginx+uwsgi in Django project under Centos8

1. Virtual environment virtualenv installation 1....

In-depth analysis of HTML semantics and its related front-end frameworks

About semantics Semantics is the study of the rel...

Specific use of stacking context in CSS

Preface Under the influence of some CSS interacti...

Summary of common Linux distribution mirror source configuration

I have been researching Linux recently and tried ...

A brief discussion on the role and working principle of key in Vue3

What is the function of this key attribute? Let’s...

Tutorial on installing mysql5.7.17 via yum on redhat7

The RHEL/CentOS series of Linux operating systems...

Detailed explanation of html printing related operations and implementation

The principle is to call the window.print() metho...