Requirements: Use Nginx to build a reverse proxy, responsible for scheduling all requests, the backend is developed by ASP.NET MVC, and deployed to multiple different servers, forming a backend clusterSession information cannot be shared, resulting in some requests not being processed correctly。 The following problems will arise:
ASP.NET The website can be referred to as follows:
Commonly used solutions are as follows:
Use the database to store the SESSION
Since each server needs to use the same session, we can store the session in the same database, every time we access, we go to the database to check whether there is this session or whether this session has expired, and then we can synchronize the session of multiple servers;
Merit:Using this method is simple, convenient, and easy to get started;
Shortcoming:Using the database to synchronize sessions will increase the IO of the database and increase the burden on the database. At the same time, each access needs to intercept requests and query the database, resulting in an additional layer of access and wasted database session time.
Use a caching mechanism such as Memcache or Redis to store the SESSION
Using distributed caching mechanisms such as memcache or redis to store session data is a popular solution for load balancing and synchronous sessions in many large-scale projects. Its principle is that the project uses the memcache or redis cache in the same place, when the user logs in, the session will be stored in the cache, and then no matter which server of the project is accessed, the session cache will be obtained from the same place, so that session synchronization is easily realized;
Merit:Using cache to synchronize sessions will not increase the burden on the database, nor do you need to manually judge whether the session exists or expires, eliminating some business logic.
Shortcoming:memcache or redis divides memory into many specifications of storage blocks, and there are blocks with sizes, which also determines that memcache or redis cannot fully utilize memory, which will produce memory fragmentation, and if the storage blocks are insufficient, memory overflow will also occur.
Leverage the ip_hash pattern in Nginx
This technique, also known as session keeping, is the ip_hash technology in nginx that allows you toRequests from a certain IP address are pinned to the same backend application server, so that a client and a backend under this IP can establish a stable session.
(But there is also a disadvantage, if the operator's network is more volatile and unstable,Egress IP is dynamicYes, there will be problems with this method. )
The test method is to open two sites based on docker,You need to create a new index.html file under the /data/testsite/a and /data/testsite/b directories, respectively, the command is as follows:
Access through a browser as shown below:
If you create a new nginx container based on Docker, without using ip_hash technology, the request can be sent to different backend servers, as shown in the figure below:
Using ip_hash technology, create a new /data/testsite/nginx.conf file with the following configuration:
docker startup command is as follows:
(Cookie-based session keeping, which can be consulted in the sticky module, omitted)
Nginx configuration:The hyperlink login is visible.
(End)
|