[session sharing] If there are multiple machines, how to solve the problem of session sharing

【Origin of the problem】

First of all, we should understand why sharing is needed. If your website is stored on one machine, then this problem does not exist, because the session data is on this machine, but if you use load balancing to distribute requests to different machine? At this time, there is no problem with the session id on the client side, but if the user's two requests are sent to two different machines, and its session data may exist on one of the machines, the situation that the session data cannot be obtained will occur at this time. , so session sharing becomes a problem. 

【Solution】

  1. PHP supports storing session data to a memcache server ,
  2. You can also manually change the directory where the session files are stored to the nfs network file system, so as to achieve cross-machine sharing of files. 
  3. There is also a simple method that can be used when the session information does not change frequently. When the user session is set on machine a, the session data is posted to a cgi of machine b, and the cgi of machine b saves the session data, so that the machine Both a and b will have a copy of the same session data.
  4. In addition to the methods mentioned above, there is another way to solve the problem of session sharing: (in essence, it is not solved by session sharing) Here, taking nginx as an example, the user requests are distributed to different machines, then we only It needs to be fixed, the same user request is distributed to the same machine for processing, that is, this time the user requests the server, then the next time it comes again, the request is also distributed to the same server as the last time. This ensures that the same user will not be unable to obtain session data because the request is distributed to different machines. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325435420&siteId=291194637
Recommended