Nginx load Balancing multi-site sharing Session_nginx

Source: Internet
Author: User
Tags sessions nginx load balancing
Common practices for multi-site sharing sessions are:

• Use. NET Automatic status services (asp.net State service);
• Use the. NET session database;
• Use memcached.
• The use of cookies to achieve the sharing of multiple sites (this approach is limited to several sites in the same domain name);
Here we will rehearse the database form to store the session, to achieve a multi-site sharing session.

First we build the site, the following figure:

Default.aspx

There are two button, Setsession is mainly used to assign a value to a session (such as: session["sharevalue"] = "ABCD"),

GetSession is mainly to get a session value.

The specific code is as follows:

There's just so much in the code section ...

The following is to configure the web.config, in fact, is mainly in <system.web>
This node adds MachineKey and sessionstate to these two nodes,
1. The main role of machinekey is:
"According to MSDN's standard statement:" Configure the key to be used to encrypt and decrypt Forms authentication Cookie data and view state data and use it to authenticate out-of-process session state identities. "That means that a lot of asp.net encryption is dependent on the values in the machinekey, such as Forms authentication cookies, viewstate encryption." By default, asp.net configuration is its own dynamic generation, if a single server, of course, but if more than one server load balancing, machinekey also adopt dynamic generation, each server machinekey value inconsistent, resulting in the results of encryption is not consistent, can not share the verification and viewstate, In case of load balancing for multiple servers, 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:

Copy Code code 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; password=js@#$1234 "allowcustomsqldatabase=" true "cookieless=" false "timeout="/> "


Part of the site this is good ... The following is to configure the library .....

Database configuration:
Using the Aspnet_regsql.exe tool
ASP.net version 2.0 Microsoft has provided aspnet_regsql.exe tools to facilitate the configuration of the session database. The tool is located in the system root \microsoft.net\framework\ version number folder on the WEB server.

Use examples:

Aspnet_regsql.exe-s. -U sa-p 123456-ssadd-sstype P
-S parameter:
Represents the database instance name. You can use the "." Represents the native.
-U and-p parameters:
Represents a user name and password.
-E parameter:
You can then select a group in-u–p with-E. –e means that the current system user logs on to the database through Windows authentication, and-u-p logs on to the database using SQL Server users.
-ssadd/–ssremove Parameters:
-ssadd means 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 you store session data in the tempdb database, session data will be lost when you restart SQL Server.

Store session data in the ASPState database, rather than in the tempdb database.
C
Stores session data into 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 setting is: Aspnet_regsql.exe-s. -E-d Awbuisession-ssadd-sstype C

All right. We're done with the basics.
Now we are deploying one of our newly built web sites to IIS. But since we're going to load. At least two copies were deployed.

We have one of the servers in the defaut.aspx "server 1" to "Server 2", the main purpose of this is to make a difference!

Specifically as follows:

The URLs for two websites are:

Server 1:127.0.0.1:8081;

Server 2:127.0.0.1:8080;

Ok. Here we are configuring NIGNX.

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

Make the setting as above:

Ok. Nginx this configuration even OK. Let's start the Nginx.

In the browser, enter the URL we configured in Nginx such as: 127.0.0.1:8090

We will see that server 1 is already starting to serve us, and we'll click "Setsession" to set a session value,

We'll see server 2 starting to work. Then we'll click "Getsesion" to see the session values that were set on server 1, and the results are as follows:

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

ASPStateTempSessions one of the SessionID in this table,

The SessionID includes two parts: 24-bit SessionID and 8-bit appname for different sites, different appname, and in the same situation that 24-bit sessionid can be made under different sites, 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, modify Tempgetappid as follows:

Copy Code code 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 '%s ' ' and '%s '. Please rename the 1st application to resolve the problem. '
1, @appName, @dupApp)
End
End
COMMIT
End
return 0

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

Restart each site. Browse the website again

Dot "Setsession",

and point: "GetSession"

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

Let's point again: "GetSession",

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

Additional point: Session expiration deletion, mainly in SQL Server Agent job completion.

Specific can, check the other relevant 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.