Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize load balancing and Multi-site sharing Session by nginx

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to achieve load balancing multi-site sharing Session by nginx". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve load balancing multi-site sharing Session" can help you solve your doubts.

Common practices of multi-site sharing session are:

Use the. Net automated status service (asp.net state service)

Session database using .net

Use memcached.

Use cookie to achieve sharing among multiple sites (only if several sites are in the same domain name)

Here we will demonstrate how to store session in the form of a database to achieve multi-site sharing of session.

First of all, let's build the site, as shown below:

Default.aspx

There are two button, and setsession is mainly used to assign a session (for example: session ["sharevalue"] = "abcd")

The main purpose of getsession is to get a session value.

The specific code is as follows:

That's all the code part is all about.

The following is the need to configure web.config, in fact, mainly in

Machinekey and sessionstate are added to this node.

1. The main functions of adding machinekey are:

According to the standard msdn saying, "the key is configured to be used to encrypt and decrypt forms authentication cookie data and view state data, and to validate the out-of-process session state identity." In other words, a lot of asp.net encryption depends on the values in machinekey, such as forms authentication cookie, viewstate encryption. By default, the configuration of asp.net is generated dynamically. Of course, there is no problem for a single server, but if multiple servers are load balanced, machinekey is also dynamically generated, and the machinekey values on each server are inconsistent, resulting in inconsistent encrypted results, so verification and viewstate cannot be shared. Therefore, in the case of load balancing among multiple servers, the same machinekey must be configured on each site. " You can check other materials for details.

two。 The main purpose of adding sessionstate is to keep session in the database.

The specific configuration is as follows:

The copy code is as follows:

The part of the website is fine. Here is the configuration of the database. ..

Database configuration:

Use the aspnet_regsql.exe tool

After asp.net 2.0, Microsoft provides aspnet_regsql.exe tools to easily configure session database. The tool is located in the system root\ microsoft.net\ framework\ version 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 local machine.

-u and-p parameters:

Represents the user name and password.

-e parameter:

You can choose between-u-p and-e. -e indicates that the current system user logs in to the database through windows authentication, while-u-p uses the sqlserver user to log in to the database.

-ssadd /-ssremove parameter:

-ssadd means to add session database, and-ssremove means to remove session database.

Sstype parameter description:

T

Store the session data in the sql server tempdb database. This is the default setting. If the session data is stored in the 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 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 setting is: aspnet_regsql.exe-s. -e-d awbuisession-ssadd-sstype c

Okay. We've already taken care of the basics.

Now let's deploy a website we just built to iis. But since we have to load. At least two copies of the deployment.

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

The details are as follows:

The url of the two websites are:

Server 1:127.0.0.1:8081

Server 2:127.0.0.1:8080

Ok . Next we will configure nignx.

First find the file nginx.conf in the nginx\ conf configuration file and open it in notepad

Make the settings as above:

Ok . Nginx is ok when configured in this way. Let's start nginx..

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

We will see that server 1 is already serving us. Let's click "setsession" to set a session value.

We will see server 2 working. At this point, let's click "getsesion" to see the session value just set on server 1, and the result is as follows:

This is mainly due to the failure to share session between server 1 and service 2 when storing a session in the database, mainly in

A sessionid in the aspstatetempsessions table

The sessionid includes two parts: the 24-bit sessionid generated by the website and the 8-bit appname have different appname for different sites. If you can make the 24-bit sessionid the same under different sites, to ensure that the sessionid after the combination with appname is the same, you can modify the stored procedure tempgetappid so that the resulting sessionid is independent of appname. Modify the tempgetappid as follows:

The copy 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 for 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'% slots' and'% slots. Please rename the 1st application to resolve the problem.'

18, 1, @ appname, @ dupapp)

End

End

Commit

End

Return 0

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

Restart the stations. Browse the website again.

Click "setsession"

Click again: "getsession"

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

Let's order again: "getsession"

You can see that server 1 and server 2 return the same result, achieving "multi-site sharing session"

An additional point: session expired deletion, mainly in the sql server agent job completion.

After reading this, the article "how to achieve load balancing and multi-site sharing of Session with nginx" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to learn more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report