This paper introduces the VMWare12 virtual machine, Linux (CentOS7) system installation, deployment Nginx1.6.3 Proxy service to do load balancing. The next step is to distribute the request through Nginx to each Web application processing service.
First, Web application development
1. ASP. MVC5 Development
(1) Create a new MVC5 project, create a new controller, in the index method to save the current time to session["MySession"], and write cookies["mycookies" Storage host name and the current time.
123456789101112131415 |
public
ActionResult Index()
{
if
(
this
.HttpContext.Session[
"mysession"
] ==
null
)
{
this
.HttpContext.Session[
"mysession"
] = DateTime.Now.ToString(
"yyyy-MM-dd hh:mm:ss"
);
}
this
.HttpContext.Response.Cookies.Add(
new
HttpCookie(
"mycookies"
)
{
Expires = DateTime.Now.AddDays(1),
Value = HttpContext.Server.MachineName +
"||"
+ DateTime.Now.ToString()
});
return
View();
}
|
(2) Add a second method getsession to the controller, showing the value of the session and the cookie.
123456789101112131415 |
public
ActionResult GetSession()
{
if
(
this
.HttpContext.Session[
"mysession"
] !=
null
)
{
ViewBag.DD =
this
.HttpContext.Session[
"mysession"
].ToString();
ViewBag.SCode =
this
.HttpContext.Session[
"mysession"
].GetHashCode().ToString();
ViewBag.SID =
this
.HttpContext.Session.SessionID;
}
ViewBag.CVAL = System.Web.HttpContext.Current.Request.Cookies[
"mycookies"
].Value;
ViewBag.CID = System.Web.HttpContext.Current.Request.Cookies[
"mycookies"
].Name;
ViewBag.CDO = System.Web.HttpContext.Current.Request.Cookies[
"mycookies"
].Domain;
return
View();
}
|
(3) The session and the cookie information are displayed on the page, the GetSession View code is as follows:
@{ viewbag.title = "GetSession";}
The above implementation session and the cookie read and write, in order to verify the load balance, each request processing is consistent, the next important content, how to do load balancing how to keep the session consistent, for the ASP. NET Technology session principle here do not introduce, online search under a lot.
2. Session Sharing Technology
. NET platform to support several session storage modes:
(1) InProc mode
Session stored in the current site in the same process, modify the Web. config or bin file updates, will cause the session to be lost. This mode is the default mode.
(2) ASPNET State mode
The ASPNET state stores the session in the status service and needs to start the ASP. Aspnet_state.exe to see the process. You also need to configure this mode in Web. config.
(3) SQL Server mode
This mode requires SQL Server configuration-related information, starts the proxy service, database accounts, and tables, and points the Web. config to the database.
(4) Third-party expansion mode
This framework uses this mode to store sessions in other storage, such as: Memcached, Redis Cache, to achieve the purpose of sharing the session. You can extend this abstract class by implementing Sessionstatestoreproviderbase in ASP. This system uses the session stored in the Redis cache, by introducing the Redissessionstateprovider component.
Install-package Microsoft.Web.RedisSessionStateProvider
3, Nginx Service status situation
Enter the command service Nginx status at the CentOS terminal to view the situation and ensure that the service is running properly.
4. Web site Deployment
Deploy two sites for:
Site A: port is 8081,
Site B: Port is 8082,
Second, the function effect shows
(1) The browser accesses the index method, Http://192.168.119.128/demo, as shown below:
(2) Browser access GetSession method, Http://192.168.119.128/demo/getsession, shown as follows:
Through the above verification, the session and the cookies obtained are consistent.
Author: Andon
Source: Http://www.cnblogs.com/Andon_liuNginx distributes the request to each Web application