Nginx + redis-based Asp.net site construction, nginxredis

Source: Internet
Author: User
Tags install openssl

Nginx + redis-based Asp.net site construction, nginxredis

Synopsis

In traditional information systems (such as small-scale ERP/MES systems), distributed deployment of simple application servers and database servers is often used to improve the load capacity of application systems, with the increase in access, in addition to the expansion of hardware and network on the application server layer, it is difficult to cope with the [regular beginning ].

 

Of course, there are many open-source technologies nowadays. Isn't it distributed? Application Server Distributed, database read/write splitting, Cache Server, and authentication server... There are indeed many methods. If you don't need to worry about it, let's talk about Server Load balancer at the application server level today. You can practice the following technologies:Nginx,Of course, it also includes the cache technology: redis.

The preliminary assumption is as follows: load balancing is performed on multiple identical application servers in the LAN through nginx, and each same application shares a cache server [This is simple ]. Pull:

Start building [tossing]

1. Prepare the Operating System

A linux instance, of course, is generally a virtual machine. Here I have installed centos7 and configured the IP address as 192.168.110.100. The machine name is centos.

You can run the asp.net mvc site on a windows Server, for example, windows 10 + iis8, and configure the IP address as 192.168.110.1. The machine name does not matter.

Configure the hosts of the two machines:

windows:C:\Windows\system32\drivers\etc\hosts

192.168.110.100  cluster.com

 

centos: vim /etc/hosts

192.168.110.100  cluster.com 

2. Install Nginx

Generally, you need to install the compiling environment first [do not engage in c anyway, refer to other articles for manual installation]. centos supports yum installation, which is generally yum install. Of course, you need to log on with the root user under su root.

To ensure the connection of virtual machines, run the command: yum install gcc-c ++

I thought that nginx could be directly installed. I didn't expect that there are three dependent libraries to download and install. The procedure is the same:

Download the installation package, decompress the installation package, enter the configuration directory, and execute make and make install respectively.

Of course, this is not the focus:

The downloaded version: Release

> Install pcre
Obtain the pcre compilation installation package, compile
Decompress the pcre-xx.tar.gz package.
Go to the extract directory and execute./configure, make, and make install.

> Install openssl
Obtain the openssl compilation installation package, which is sent to centos at https://www.openssl.org/source/openssl-fips-2.0.10.tar.gz.

Decompress the openssl-xx.tar.gz package.
Go to the extract directory and execute./config, make, and make install.

> Install zlib
Obtain the zlib compilation installation package, and access centos at http://zlib.net/zlib-1.2.11.tar.gz.
Decompress the openssl-xx.tar.gz package.
Go to the extract directory and execute./configure, make, and make install.

> Install nginx
Obtain nginx at http://nginx.org/download/nginx-1.12.2.tar.gz.
Decompress the nginx-xx.tar.gz package.
Go to the extract directory and execute./configure, make, and make install.

 

3. Configure nginx

According to part 1, if nginx has been installed and you have never thought of any other installation experience, many problems may occur. We recommend that you fix it on your own. We can enter the following in the command line:Whereis nginx

If nginx is installed normally, the following error occurs:

[Root @ centos bin] # whereis nginx
Nginx:/usr/local/nginx
[Root @ centos bin] # cd/usr/local/nginx
[Root @ centos nginx] # ls-l
Total usage 0
Drwx ------ 2 nobody root 6 November 2 14:08 client_body_temp
Drwxr-xr-x 2 root 333 November 2 20:56 conf
Drwx ------ 2 nobody root 6 November 2 14:08 fastcgi_temp
Drwxr-xr-x 2 root 40 November 2 11:05 html
Drwxr-xr-x 2 root 58 November 2 20:58 logs
Drwx ------ 2 nobody root 6 November 2 14:08 proxy_temp
Drwxr-xr-x 2 root 19 November 2 11:05 sbin
Drwx ------ 2 nobody root 6 November 2 14:08 scgi_temp
Drwx ------ 2 nobody root 6 November 2 14:08 uwsgi_temp

The configuration file is generally named nginx. conf In the conf folder. In this experiment, the key is to configure the file:

[root@centos nginx]# vim ./conf/nginx.conf

Let's modify the following points [192.168.110.1: 9001 and 9002 sites are under5th o'clockThe deployed application site, which is described in advance here. Why is there the same ip address? The local demonstration will lose one IIS and the port will be different ]:

1 http {2 3 #.... omit some codes 4 # We need the internal application address and port of the server Load balancer, where weight is the weight. Here, 50% accounts for half of 5 upstream cluster.com {6 server 192.168.110.1: 9001 weight = 1; 7 server 192.168.110.1: 9002 weight = 1; 8} 9 10 server {11 listen 80;
# Need to load the site, here is the host set hosts site 12 server_name cluster.com; 13 14 # charset koi8-r; 15 16 # access_log logs/host. access. log main; 17 18 location/{19 root html; 20 index index.html index.htm; 21 proxy_pass http://cluster.com; 22 # Set the Host header and client real address, the Server can obtain the client's real IP23 proxy_set_header X-Forwarded-Host $ host; 24 proxy_set_header X-Forwarded-Server $ host; 25 proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for; 26 proxy_set_header X-Real-IP $ remote_addr; 27} 28 29 #... omit some encoding 30}

The configuration is so simple. We recommend that you disable the centos firewall before starting nginx:

systemctl stop iptalbes.service

Enable nginx and run it in the nginx directory. Check whether the nginx directory is successful:

 

[Root @ centos nginx] #./sbin/nginx

[Root @ centos nginx] # ps-ef | grep nginx
Root 1485 1 0? 00:00:00 nginx: master process./sbin/nginx
Nobody 1486 1485 0? 00:00:00 nginx: worker process
Root 1663 1471 0 00:00:00 pts/0 grep -- color = auto nginx

In this case, we can access cluster.com on a windows computer and we can see the 9001 or 9002 websites:

Don't worry if you can't see the computer you actually accessed. You just need to deploy redis and write several mvc request services.

 

4. install and configure redis

Download the following example with nginx. now I have compared the old redis-3.0.6.tar.gz, https://redis.io/download. Of course, you can download the latest one.

Three axes: extract the file, enter the decompressed directory, and execute make

Go to the Src subdirectory, and you will see some execution files: redis-server \ redis-cli and a redis. conf. We 'd better create a redis directory in/usr/local, and copy several major ones in it.

Of course, there is a file configuration that needs to be modified: whether the background is enabled in redis. cong:

Vim redis. conf # modify daemonize yes # comment out the bind end at the same time to ensure that the LAN can be accessed

Enable redis:

 ./redis-server ./redis.conf
1 [root@centos redis]# redis-cli2 127.0.0.1:6379> set test "helloword"3 OK4 127.0.0.1:6379> get test5 "helloword"6 127.0.0.1:6379> 

This is simple and the test is successful. Here, we emphasize again that the centos firewall is disabled.

5. write and publish the asp.net mvc site

Through the step-by-step installation above, we finally entered the topic. We built a simple asp.net mvc site, which won't be nonsense here. Then we deployed the site to the local iis to deploy the two sites, the ports are respectively 9001 and 9002.

If windows Firewall is enabled, enable inbound and outbound restrictions on ports 9001 and 9002 in the firewall.

At the same time, in order to develop a connection to redis for cache service, you also need to install the api package. The function is here:

Input: Install-Package StackExchange. Redis

Add the following services to HomeController:

1 public class HomeController: Controller 2 {3 public ActionResult Index () 4 {5 return View (); 6} 7 // <summary> 8 // get service request address 9 /// </summary> 10 // <returns> </returns> 11 public JsonResult GetServerInfo () 12 {13 var server = HttpContext. request. url. host + "" + HttpContext. request. url. port; 14 return Json (server, JsonRequestBehavior. allowGet ); 15} 16 /// <summary> 17 // set cache 18 /// </summary> 19 /// <param name = "key"> </param> 20 /// <param name = "value"> </param> 21 // <returns> </returns> 22 public JsonResult SetRedisValue (string key, string value) 23 {24 RedisClient client = new RedisClient ("192.168.110.100", 6379); 25 26 client. setValue (key, value); 27 28 var server = HttpContext. request. url. host + "" + HttpContext. request. url. port; 29 30 return Json ($ "Access server: {server}, set the value of the cache key {key} to {value}", JsonRequestBehavior. allowGet ); 31} 32 // <summary> 33 // read cache 34 // </summary> 35 // <param name = "key"> </param> 36 /// <returns> </returns> 37 public JsonResult GetRedisValue (string key) 38 {39 RedisClient client = new RedisClient ("192.168.110.100", 6379); 40 var v = client. getValue (key); 41 42 var server = HttpContext. request. url. host + "" + HttpContext. request. url. port; 43 44 return Json ($ "Access server: {server}, get cache key {key} value: {v}", JsonRequestBehavior. allowGet); 45}
View Code

6. Test Run

Finally begin [project acceptance]

Step 2: Access the address http://cluster.com/home/getserverinfo, and click the new page on the f5's tab."Cluster.com 9002 ","Cluster.com 9001" indicates that Server Load balancer is successful, and applications on ports 9001 and 9002 are constantly replaced by users.

Step 2, write cache http://cluster.com/Home/SetRedisValue? Key = test & value = helloword, which can be found and will prompt you:"Access Server: cluster.com 9001, set the cache key test value to helloword"

Step 2, request cache http://cluster.com/Home/GetRedisValue? Key = test, you can find that if you refresh multiple times, it will change:

"Access Server: cluster.com 9002. The value of the test cache key is helloword"
"Access Server: cluster.com 9001. The value of the test cache key is helloword"
Note: Our test is successful!

 

7. Post feeling

A review of the entire process is actually just a small experiment. It's just a record of the learning process. Of course there is no synchronization of session Status and database-level distribution.

 

Related Article

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.