Home >Operation and Maintenance >Nginx >How nginx implements load balancing and multi-site sharing session

How nginx implements load balancing and multi-site sharing session

王林
王林forward
2023-05-17 08:31:131248browse

Common ways to share sessions across multiple sites include:
•Use .net automatic state service (asp.net state service);
•Use .net session database;
•Use memcached.
•Use cookies to achieve sharing between multiple sites (this method is only limited to the case where several sites are in the same domain name);
Here we will practice storing sessions in the form of a database to achieve Multi-site shared session.

First we build the site, as shown below:

How nginx implements load balancing and multi-site sharing session

default.aspx

How nginx implements load balancing and multi-site sharing session

There are two buttons, setsession is mainly used to assign a value to a session (such as: session["sharevalue"] = "abcd"),

getsession is mainly used to obtain a session value.

The specific code is as follows:

How nginx implements load balancing and multi-site sharing session

That’s all the code part...

The next step is to configure the web.config. In fact, The main thing is to add the two nodes machinekey and sessionstate to the node
.
1. The main function of adding machinekey is:
"According to msdn's standard statement: "Add the key to Configure it so that it is used to encrypt and decrypt forms authentication cookie data and view state data, and to validate out-of-process session state identities. "That is to say, many encryptions of asp.net depend on the values ​​​​in machinekey, such as the encryption of forms authentication cookie and viewstate. By default, the configuration of asp.net is dynamically generated by itself. If a single server does not Problem, but if multiple servers are load balanced, machinekey is also dynamically generated. The machinekey value on each server is inconsistent, resulting in inconsistent encryption results. Verification and viewstate cannot be shared, so for multiple server load balancing In this case, the same machinekey must be configured on each site." You can check other information for details.
2. Adding sessionstate mainly allows the session to be saved in the database.
The specific configuration is as follows:

Copy code The code is as follows:


decryptionkey="9421e53e196bb56db11b9c25197a2ad470638efbc604ac74cd29dbbcf79d6046"
validation="sha1" decryption="aes"/>



The website part is just fine. . . The following is to configure the database...

Database configuration:
Use the aspnet_regsql.exe tool
After asp.net version 2.0, Microsoft provides the aspnet_regsql.exe tool to easily configure the session database. This tool is located in the "system root directory\microsoft.net\framework\version number" folder on the web server.

Usage example:

aspnet_regsql.exe -s . -u sa - p 123456 -ssadd -sstype p
-s parameter:
represents the database instance name. You can use "." to represent the local machine.
-u and -p parameters:
represents the user name and password.
-e parameter:
You can choose a group between -u –p and -e. –e means to log in to the database through windows authentication as the current system user, and -u -p means to log in to the database as the sqlserver user.
-ssadd / –ssremove Parameters:
-ssadd means adding the session database, -ssremove means removing the session database.
sstype Parameter description:
t
Store session data in sql server tempdb in the database. This is the default setting. If session data is stored in a tempdb database, the session data will be lost when sql server is restarted.

Store session data in the aspstate database instead of the tempdb database.
c
Store session data into a custom database. If you specify the c option, you must also include the name of the custom database using the -d option.
My settings are: aspnet_regsql.exe -s . - e -d awbuisession -ssadd -sstype c

Okay. We've got the basics covered. .
Now we deploy a website we just built to iis. But since we want to load. At least two copies should be deployed.

How nginx implements load balancing and multi-site sharing session

We changed "Server 1" in defaut.aspx in one of the servers to "Server 2". The main purpose of doing this is to make a difference!

The details are as follows:

How nginx implements load balancing and multi-site sharing session

The URLs of the two websites are:

server 1: 127.0.0.1:8081;

server 2:127.0.0.1:8080;

ok. Next we are configuring nignx.

First find the nginx.conf file in the nginx\conf configuration file, open it with Notepad,

How nginx implements load balancing and multi-site sharing session

Make the above settings:

ok. If nginx is configured like this, it will be ok. Let’s start nginx..

Enter the url we configured in nginx in the browser, such as: 127.0.0.1:8090

How nginx implements load balancing and multi-site sharing session

We will see Server 1 has started to serve us. Let's click "setsession" again to set a session value.

How nginx implements load balancing and multi-site sharing session

We will see server 2 start to work. At this time, we click "getsesion" again to see the session value just set on server 1. The result is as follows:

How nginx implements load balancing and multi-site sharing session

When this happens, the main reason is to store a session value in the database The session between server 1 and service 2 is not shared during the session, mainly because of a sessionid in the

How nginx implements load balancing and multi-site sharing session

aspstatetempsessions table,

the sessionid includes Two parts: the 24-bit sessionid and 8-bit appname generated by the website. For different sites, the appname is different. If the 24-bit sessionid can be made the same on different sites, it must be ensured that the sessionid after combining and adding the appname is the same. You can modify the stored procedure tempgetappid so that the sessionid obtained has nothing to do with appname. Modify tempgetappid as follows:

Copy code The code is as follows:


alter procedure [dbo].[tempgetappid]
@appname tappname,
@appid int output
as
set @appname = 'test' --lower(@appname) Modify this so that the appname of multiple sites is a fixed value.
set @appid = null
select @appid = appid
from [awbuisession].dbo.aspstatetempapplications
where appname = @appname
if @appid is null begin
begin tran
select @appid = appid
from [awbuisession].dbo.aspstatetempapplications with (tablockx)
where appname = @appname
if @appid is null
begin
exec gethashcode @appname, @appid output
insert [awbuisession].dbo.aspstatetempapplications
values
(@appid, @appname)
if @@error = 2627
begin
declare @dupapp tappname
select @dupapp = rtrim(appname)
from [awbuisession].dbo.aspstatetempapplications
where appid = @appid
raiserror('sql session state fatal error: hash-code collision between applications ''%s '' and ''%s''. please rename the 1st application to resolve the problem.',
18, 1, @appname, @dupapp)
end
end
commit
end
return 0


After the above modifications, it is necessary to realize that multiple sites share the same sessionid.

Restart each site. Browse the website again

How nginx implements load balancing and multi-site sharing session

Click "setsession",

How nginx implements load balancing and multi-site sharing session

Click again: "getsession"

How nginx implements load balancing and multi-site sharing session

In this way we can see that server 2 gives the session value we just set in server 1.

Let’s click “getsession” again.

How nginx implements load balancing and multi-site sharing session

You can see that Server 1 and Server 2 return the same results, achieving “multi-site sharing” session”

An additional point: session expiration is deleted, mainly when the job in the sql server agent is completed.

How nginx implements load balancing and multi-site sharing session

The above is the detailed content of How nginx implements load balancing and multi-site sharing session. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete