Nginx load balancing, using database storage session, to achieve multi-site sharing session[-turn]

Source: Internet
Author: User
Tags nginx load balancing

Multi-site Sharing session common practice is:

1. Using the. NET Automatic State Service (ASP);

2. Use the session database of. NET;

3, use memcached.

4, the use of cookies to achieve the sharing between multiple sites (this way is limited to a few sites are in the same domain name of the case);

Here we will rehearse the database to store the session, to achieve multi-site sharing session.

First we build a site, such as:

Default.aspx

There are two buttons, setsession is mainly used to assign values to a Session (e.g. session["sharevalue"] = "ABCD"

) ,

The main getsession is to get a Session value.

The specific code is as follows:

So much for the code section ...

The following is to configure the Web. config, in fact, mainly in <system.web>

This node adds the two nodes of MachineKey and sessionstate,

1. The main functions of the machinekey are:

"As per MSDN Standard:" The key is configured so that it can be used to encrypt and decrypt Forms authentication Cookie data and view state data and use it to authenticate the out-of-process session state identity. "This means that many of the encryption in ASP is dependent on the value inside the machinekey, such as the Forms authentication Cookie, ViewState encryption. By default, The configuration of the ASP is self-generated, if a single server is certainly not a problem, but if more than one server load balancing, machinekey also adopt dynamic generation, each server machinekey value inconsistent, resulting in encryption results are not consistent, can not share authentication and viewstate, the For multiple server load balancing scenarios, be sure to configure the same machinekey at each site. , you can check other information.

2. Adding sessionstate is mainly to keep the Session in the database.

The specific configuration is as follows:

<machinekey validationkey= " 86b6275ba31d3d713e41388692fca68f7d20269411345aa1c17a7386dacc9c46e7ce5f97f556f3cf0a07159659e2706b77731779d2da4b53bc47bffd4 Fd48a54 "

decryptionkey= "9421e53e196bb56db11b9c25197a2ad470638efbc604ac74cd29dbbcf79d6046"

validation= "SHA1" decryption= "AES"/>

<sessionstate mode= "SQL Server" sqlconnectionstring= "Data source=pc-07195;initial catalog=awbuisession; Persist Security info=true; User Id=jins; [Email protected]#$1234 "allowcustomsqldatabase=" true "cookieless=" false "timeout=" "/>"

Site section This is good ... The following is to configure the database .....

Database configuration:

Using the Aspnet_regsql.exe tool

After the ASP. NET 2.0 version, Microsoft provides the Aspnet_regsql.exe tool to easily configure the session database. The tool is located in the system root \microsoft.net\framework\ version number folder on the WEB server.

Examples of Use:

Aspnet_regsql.exe-s. -U sa-p 123456-ssadd-sstype P

-S parameter:

Represents the database instance name. You can use "." Represents the native.

-U and-p parameters:

Represents a user name and password.

-E parameter:

You can select a group in-u–p and-e again. –e represents logging in to the database with the current system user through Windows authentication, and-u-p is logging on to the database using SQL Server users.

-ssadd/–ssremove Parameters:

-ssadd indicates that the session database is added, and-ssremove represents the session database removal.

Sstype parameter Description:

T Store session data in the SQL Server tempdb database. This is the default setting. If session data is stored in the tempdb database, session data is lost when you restart SQL Server.
P The session data is stored in the ASPState database instead of stored in the tempdb database.
C Store session data in a custom database. If you specify the C option, you must also use the-D option to include the name of the custom database.

My settings are: Aspnet_regsql.exe-s. -E-d Awbuisession-ssadd-sstype C

All right. Basically, we're done.

Now we are deploying one of our newly built sites into IIS. But since we're going to load. Deploy at least two copies as well.

We changed one of the servers in the defaut.aspx "server 1" to "server 2", so the main goal is to make a difference!

Specific as follows:

The URLs for the two sites are:

Server 1:127.0.0.1:8081;

Server 2:127.0.0.1:8080;

Ok. Here we are the configuration Nignx.

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

Make the settings as above:

Ok. Nginx This configuration even if ok. Let's start the Nginx.

Enter the URL we configured in Nginx in the browser such as: 127.0.0.1:8090

We'll see that server 1 has started to serve us, and we'll click on "Setsession" to set a session value,

We'll see server 2 start working. Then we'll click on "Getsesion" to see the session value set in server 1, the result is as follows:

In this case, the main thing is to store a session in the database did not do server 1 and Service 2 session sharing, mainly in

ASPStateTempSessions one of the SessionID in this table,

The SessionID includes two parts: the site generated 24-bit sessionid and 8-bit appname for different sites, its appname different, in the ability to make 24-bit sessionid under different sites under the same circumstances, To ensure that after the combination plus appname SessionID the same, you can modify the stored procedure tempgetappid, so that its SessionID and appname Independent, modified Tempgetappid as follows:

ALTER PROCEDURE [dbo]. [Tempgetappid]

@appName Tappname,

@appId int OUTPUT

As

SET @appName = ' Test '--lower (@appName) modified here, so that 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 '. Rename the 1st application to resolve the problem. ',

1, @appName, @dupApp)

END

END

COMMIT

END

RETURN 0

After the above modification, the following to implement multiple sites to share the same sessionid.

Restart each site. Browse the website again

Point "Setsession",

Another point: "GetSession"

So we can see that server 2 gives us the session value we just set in server 1.

Let's point out: "GetSession",

You can see that server 1 and server 2 return the same results, reaching the "multi-site sharing Session"

Add a point: Session expires delete, primarily in SQL Server Agent job completion.

Specific can, check the other relevant information.

Nginx load balancing, using database storage session, to achieve multi-site sharing session[-turn]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.