Nginx load balancing, multi-site sharing Session

Source: Internet
Author: User
Tags nginx load balancing
Nginx load balancing, multi-site sharing Session

Common practices for sharing sessions with multiple sites are as follows:

  • Use. net Automatic Status Service (Asp.net State Service );
  • Use the. net Session database;
  • Use Memcached.
  • Use cookies to share multiple sites (this method is only applicable when multiple sites are under the same domain name );

Here we will test how to store sessions in the form of databases to achieve multi-site sharing sessions.

First, we have created a site, such:

 

Default. aspx

There are two buttons. SetSession is mainly used to assign values to a Session (for example, Session ["ShareValue"] = "abcd"

),

GetSession is mainly used to obtain a Session value.

The Code is as follows:

That's all about the code...

 

The following figure shows how to configure Web. config.

 In this nodeAddMachineKey and sessionState nodes,

1. The main functions of adding a machineKey are:

"According to MSDN standards:" The key is configured to encrypt and decrypt Forms authentication Cookie data and view status data, it is used to verify the non-process session status identity." That is to say, many Asp. Net encryption relies on values in machineKey, such as Forms authentication Cookie and ViewState encryption. By default, Asp. net configuration is dynamically generated by yourself. If a single server is okay, but if multiple servers are load balanced, machineKey is also dynamically generated. The machinekey values on each server are inconsistent, as a result, the encrypted results are inconsistent and the verification and ViewState cannot be shared. Therefore, you must configure the same machineKey for Server Load balancer instances on each site. ", You can check other information.

2. Added sessionState to store sessions in the database.

The specific configuration is as follows:

<MachineKey validationKey = "machineKey"

DecryptionKey = "9421E53E196BB56DB11B9C25197A2AD470638EFBC604AC74CD29DBBCF79D6046"

Validation = "SHA1" decryption = "AES"/>

<SessionState mode = "SQLServer" sqlConnectionString = "Data Source = PC-07195; Initial Catalog = AWBUISession; Persist Security Info = True; User ID = jins; password = js @ #$1234 "allowCustomSqlDatabase =" true "cookieless =" false "timeout =" 100 "/>

 

This is good for some websites... The following is how to configure the database .....

 

Database Configuration:

Use aspnet_regsql.exe

After ASP. NET 2.0133, The aspnet_regsql.exe tool is soft enough to easily configure the Session database. This tool is located in the "system root directory \ Microsoft. NET \ Framework \ version" folder on the Web server.

Example:

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

-S parameters:

Indicates the database instance name. You can use "." to indicate the local machine.

-U and-P parameters:

Indicates the user name and password.

-E parameter:

You can select a group in-U-P and-E.-E indicates that the current system user logs on to the database through windows authentication, and-U-P indicates that the SQL Server user logs on to the database.

-Ssadd/-ssremove parameters:

-Ssadd indicates that the Session database is added, and-ssremove indicates that the Session database is removed.

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 SQL Server is restarted.

P

Store session data in the ASPState database instead of the tempdb database.

C

Store session data in a custom database. If the c option is specified, 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

 

Okay. Basically, we have done it ..

Now we deploy the website we just created to IIS. But we need to load it. Deploy at least two copies.

 

 

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

The details are as follows:

 

The URLs of the two websites are:

Server. 0.0.1: 8081;

Server 2: 127. 0.0.1: 8080;

OK. Next we will configure nginx.

 

Find the nginx. conf file in the nginx \ conf configuration file and open it in notepad,

 

Make the following settings:

OK. Nginx configuration is OK. Let's start nginx ..

Enter the URL we configured in nginx in the browser, for example, 127.0.0.1: 8090.

 

We can see that server 1 has started to serve us. Let's click "SetSession" to set a session value,

 

We can see that server 2 is working. Click "GetSesion" to view the session value set in Server 1. The result is as follows:

 

When a Session is stored in the database, Session sharing between server 1 and Service 2 is not performed, mainly in

One SessionID in the ASPStateTempSessions table,

The SessionId includes two parts: the 24-bit SessionID generated by the website and the 8-bit AppName. For different sites, the AppName is different, when the 24-bit SessionID is the same for different sites, you can modify the Stored Procedure TempGetAppID by making sure that the SessionID after AppName is combined, make the SessionID irrelevant to the AppName. Modify the TempGetAppID as follows:

Alter procedure [dbo]. [TempGetAppID]

@ AppName tAppName,

@ AppId int OUTPUT

AS

SET @ appName = 'test' -- LOWER (@ appName) Modify here to make the APPname of multiple sites 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 preceding modification, multiple sites must share the same SessionID.

 

Restart each site. View the website again

Click "SetSession ",

 

Click "GetSession"

In this way, we can see that server 2 provides the session value we set in Server 1 just now.

 

Click "GetSession ",

 

We can see that server 1 and Server 2 return the same result, reaching the "multi-site sharing Session"

 

Note: The Session expired and deleted is mainly completed in the SQL server proxy.

For details, check other related information.

 

 

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.