Nginx load balancing session sharing problem

1. What is the session remains?

After the user is simply visit the website, and then close the browser, the next time when you open the site, you will find still logged in, this can be understood as the session remains functional.

2. Why do session remains?

HTTP protocol is stateless, in connection with a master, two execution is not the relationship between the successful request. This poses a problem, then there is no way a user interacts with a continuous web site, such as the electricity supplier and then a website where users put a product into the shopping cart, again after a switch interface added items, both there is no correlation between the additions of commodities request, the browser can not know the user chose those goods.

3, the principle remains the realization of the session?

  • cookie: the general store again cookies in your browser. The main store information (session ID)
  • session: general storage server again. The main information is stored there: (session ID corresponding to the user name, login information, etc.)

4, when a user accesses a dynamic Web site, how is between cookie and session work

  • When a user first requests the server site, the server will generate session_id through the program, and then use the set-cookie way to the browser under session_id, the browser will session ID to the local storage of cookies in
  • When the user requests the server site, the browser will then carry the cookie Headers header information of the site that the browser cookie information carried by the first request to the server when the site issued session_ID
  • After users log on to the website, the server will be stored in the corresponding session_ID (Mysql, Redis, Mencached, File)
  • When the next time the user requests the site, the server program verifies session_ID submitted by the browser
    Here Insert Picture Description

5, using load balancing encounter problems when session remains can be achieved by the following ways

  • session viscosity: viscosity Nginx each session refers to all requests are forwarded to the same user on the same server, and the Nginx IP_hash
  • session replication: that every time the session is changed, created or modified, it is broadcast to the servers in the cluster, the same session on all servers
  • Sharing session: session cache memory to the database, using redis, memcached
  • persistent session: session stored in the database, the same operation as the operation of the data session
Published 28 original articles · won praise 391 · views 50000 +

Guess you like

Origin blog.csdn.net/Dakshesh/article/details/104457592