Session sharing
We are doing site testing, usually need to save some basic user information, such as login will use the session, when the use of Nginx load balancing, when users browse the site will be assigned to a different server, at this time if the session after login on different servers, A remember the user's login status, but the next time the user request was assigned to b what to do? It is not possible for users to log in again. So to achieve session sharing.
Method
1.ip_hash, this is the Nginx polling method, when the user logs on to the site, will always be on this server there is no session problem
Advantages: no processing is required;
Cons: When a server accesses a large amount of traffic, load balancing is the equivalent of useless
2. Using memcached or Redis cache mode
3. Using database storage, when a server has a large amount of access, this will be a big pressure on the data
4. Asynchronous storage with memcached or Redis + db;
Nginx + Memcached Implementation of the session sharing load balancing, I used the second method;
Own a server, Nginx and memcached in a server;
In the server I deployed the same two sites, the code is the same, one port is 8080, the other is 8090;
1 Private Static ReadOnlyImemcachedmanager _memcachedmanager =NewMemcachedmanager ();2 3 Publicactionresult Index ()4 {5Random rd =NewRandom ();6session["Session"] = Rd. Next (1,100000);7 8 if(!_memcachedmanager.isexit ("Session"))9 {Ten_memcachedmanager.setvalue ("Session", session["Session"]); One } A -Viewbag.message ="the current site is: 8080;session:"+ _memcachedmanager.getvalue<string> ("Session") +""; - the returnView (); -}
Operation Result:
Achieve my goal, to achieve a balanced load at the same time session sharing purposes.
Nginx In addition to the session sharing problems, there is a shared resource issues, such as uploading files, which requires load balancing a problem to solve;
Summarize
Whether it is installed Nginx or memcached, in this process, and did not encounter a deep pit, of course, memcached on the internet there are many examples, some basic installation can be quickly realized, deployed relatively simple after all is just a demo, I do not know what will be in the actual project, there will be a great difference;
This is just a place to study, of course, there are more things to study;
Share
Nginx + Memcached Load balancing for session sharing