


How nginx implements load balancing and multi-site sharing session
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:
default.aspx
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:
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:
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.
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:
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,
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
We will see Server 1 has started to serve us. Let's click "setsession" again to set a session value.
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:
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
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
Click "setsession",
Click again: "getsession"
In this way we can see that server 2 gives the session value we just set in server 1.
Let’s click “getsession” again.
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

The start and stop commands of Nginx are nginx and nginx -s quit respectively. The start command starts the server directly, while the stop command gracefully shuts down the server, allowing all current requests to be processed. Other available stop signals include stop and reload.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

To solve the "Welcome to nginx!" error, you need to check the virtual host configuration, enable the virtual host, reload Nginx, if the virtual host configuration file cannot be found, create a default page and reload Nginx, then the error message will disappear and the website will be normal show.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

To register for phpMyAdmin, you need to first create a MySQL user and grant permissions to it, then download, install and configure phpMyAdmin, and finally log in to phpMyAdmin to manage the database.

nginx appears when accessing a website. The reasons may be: server maintenance, busy server, browser cache, DNS issues, firewall blocking, website misconfiguration, network connection issues, or the website is down. Try the following solutions: wait for maintenance to end, visit during off-peak hours, clear your browser cache, flush your DNS cache, disable firewall or antivirus software, contact the site administrator, check your network connection, or use a search engine or web archive to find another copy of the site. If the problem persists, please contact the site administrator.

There are five methods for container communication in the Docker environment: shared network, Docker Compose, network proxy, shared volume, and message queue. Depending on your isolation and security needs, choose the most appropriate communication method, such as leveraging Docker Compose to simplify connections or using a network proxy to increase isolation.
