Session sharing in nginx load balancing

         根据之前做好的配置,接下来考虑遇到的数据共享问题。
  因为niginx将原本请求的一台机器分为了2台机器,那么如果当用户信息或者一些临时数据存储在
  session中的话会数据时有时无,因为session存在服务端的,比如第一次在192.168.0.123存了
  session,第二次在192.168.0.321登录的时候发现数据没得了。
  那么我这里总结了几个解决方案,不足的地方希望大神指点下小弟。

First: Store the session data in the cookie

Because the cookie exists to request the user client. Therefore, the data will not appear sometimes and there are no symptoms, if your session saves only the user name,
login status and other small data, it is basically sufficient. User information needs to be encrypted when storing, and decrypted and compared when verifying. The disadvantage is that the
browser cleanup will be cleaned up, and there is a problem with the security of storing important data.

Second: Store the session data in the database

You can create a session table in the database to store session data.
Here, the cookie only saves the login information, and the database is used to store some other information of the currently logged-in user.
In fact, it is a bit like middleware of some companies. If the number of users is huge, a single server can store these data.
The following is the structure of my data table:
Session sharing in nginx load balancing
UserName is the record of which login user name belongs.

Third: nginx's ip_hash

ip_hash load balancing is based on personal understanding: For example, multiple users access the back-end server cluster through nginx. At this time,
because there are different users, the ip is also different. The hash value calculated by the ip+hash algorithm is transmitted to the server, and nginx records this
If the ip and hash value are set, the same ip will still be assigned to this server next time. If the current ip server is down,
the user requests it at this time, and the session has not expired. So is it going to be lost too.
The configuration is relatively simple:
Session sharing in nginx load balancing

I personally feel that the second option is more stable. If there are more ways, I hope the great god can give some pointers.

Guess you like

Origin blog.51cto.com/15034497/2665562