Load balancing solution for nginx and iis in windows

Source: Internet
Author: User
Tags file upload hash iis redis zip nginx server port number redis server

Nginx installation

Nginx is a lightweight Web server/reverse proxy server and email (IMAP/POP3) proxy server, which is released under a BSD-like protocol. Developed by Russian programmer Igor Sysoev, Rambler is used by Russia's large entry-level website and search engine Rambler (Russian: Russian. It is characterized by a small amount of memory and high concurrency. In fact, nginx's concurrency is indeed good in the same type of web servers. Users who use nginx websites in mainland China include: baidu, Sina, Netease, and Tencent.

The nginx version of the latest version is 1.9.3. I downloaded the Windows version. Generally, it is installed in the linux system in actual scenarios. This is not described here because the linux system is currently exploring. Official Download Address: nginx-1.9.3.zip blog download address: nginx-1.9.3.zip. After the download is complete, nginx is started after nginx.exe is decompressed. nginx is displayed in the process after nginx is started.


To implement server load balancer, you need to modify the configuration information of conf/nginx. conf and restart the nginx service after modifying the configuration information. This can be achieved through the nginx-s reload command. Here we use a batch operation provided by Ants.



Place the nginx.batfile in the same folder as nginx.exe and run it directly. All files used in this article will be provided at the end of this article.

 


Website construction and configuration

1. Set up two iis sites

There is only one simple index page under the site to output the current server information. Because I do not have two machines, I have deployed both sites on the local machine and bound ports 8082 and 9000 respectively.

Protected void Page_Load (object sender, EventArgs e)
        {
Label0.Text = "request start time:" + DateTime. Now. ToString ("yyyy-MM-dd HH: mm: ss ");
Label1.Text = "Server name:" + Server. MachineName; // Server name
Label2.Text = "server IP address:" + Request. ServerVariables ["LOCAL_ADDR"]; // server IP address
Label3.Text = "HTTP access port:" + Request. ServerVariables ["SERVER_PORT"]; // HTTP access port"
Label4.Text = ".. NET engine version: "+ ". net clr "+ Environment. version. major + ". "+ Environment. version. minor + ". "+ Environment. version. build + ". "+ Environment. version. revision ;//.. NET engine version
Label5.Text = "server OS version:" + Environment. OSVersion. ToString (); // server OS version
Label6.Text = "server IIS version:" + Request. ServerVariables ["SERVER_SOFTWARE"]; // server IIS version
Label7.Text = "server domain name:" + Request. ServerVariables ["SERVER_NAME"]; // server domain name
Label8.Text = "absolute path of the virtual directory:" + Request. ServerVariables ["APPL_RHYSICAL_PATH"]; // absolute path of the virtual directory
Label9.Text = "absolute path of the execution File:" + Request. ServerVariables ["PATH_TRANSLATED"]; // absolute path of the execution file
Label10.Text = "total number of sessions in the virtual directory:" + Session. Contents. Count. ToString (); // total number of sessions in the virtual directory
Label11.Text = "total number of applications in the virtual directory:" + Application. Contents. Count. ToString (); // total number of applications in the virtual directory
Label12.Text = "domain name host:" + Request. ServerVariables ["HTTP_HOST"]; // domain name host
Label13.Text = "server region language:" + Request. ServerVariables ["HTTP_ACCEPT_LANGUAGE"]; // server region language
Label14.Text = "user information:" + Request. ServerVariables ["HTTP_USER_AGENT"];
Label14.Text = "CPU count:" + Environment. GetEnvironmentVariable ("NUMBER_OF_PROCESSORS"); // number of CPUs
Label15.Text = "CPU type:" + Environment. GetEnvironmentVariable ("PROCESSOR_IDENTIFIER"); // CPU type
Label16.Text = "Request source address:" + Request. Headers ["X-Real-IP"];
        }


2. Modify nginx configuration information

Modify the nginx listening port and modify the listen node value under the http server. Because port 80 of the local machine is occupied, I changed to listening to Port 8083.

Listen 8083;

Add an upstream (server cluster) under the http node. The server sets the information of the Cluster server. Here I have set up two sites and configured two information items.

# The server cluster name is Jq_one.
Upstream Jq_one {
Server 127.0.0.1: 9000;
Server 127.0.0.1: 8082;
}

Modify the location node under the http node

Location /{
Root html;
Index. aspx index.html index.htm; # Modify the homepage to index. aspx.
# Jq_one corresponds to the cluster name set by upstream.
Proxy_pass http: // Jq_one;
# Set the host header and client real address so that the server can obtain the real IP address of the client
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
}

After modifying the configuration file, remember to restart the nginx service. The Complete configuration file information is as follows:



3. Running result


Visit http: // 127.0.0.1: 8083/index. aspx, visit multiple times, focus on the red part.



We can see that our request was distributed to 8082 sites and 9000 sites, and the first was the second 8082 site. The results show that we have successfully established the server load balancer. Try to close the 9000 site and refresh the page to find that the output http port is always 8082. That is to say, if one site is down and another site is okay, we can still serve it.

Problem analysis

Although we have set up a server load balancer site, the following problems still exist.

1. If the site uses a session and requests are evenly distributed to two sites, there must be a session sharing problem. How can this problem be solved?

Use a database to save session information
Use nginx to allocate requests from the same ip address to a fixed server. Modify the settings as follows. Ip_hash calculates the hash value corresponding to the ip address and then assigns it to the fixed server.

Upstream Jq_one {
Server 127.0.0.1: 8082;
Server 127.0.0.1: 9000;
Ip_hash;
}

Build a Redis server and read sessions from the Redis server. Later articles will introduce the use of distributed cache Redis

2. how can an administrator update a site file? Now there are only two servers. You can manually update the file to two servers. If there are 10 servers, manual operations are not feasible.

You can use the GoodSync file synchronization program to update multiple server sites. This automatically detects new file modifications and synchronizes them to other servers. Rsync can be used in linux

3. The file upload function in the site will distribute files to different servers. How can the file sharing problem be solved.

Use the file server to store all files on the server, and read and write file operations on the server. There will also be a problem. The file server has a read/write limit.

4. The server configurations of the load are different. Some high-or low-load servers can handle more requests.

Here, SLB has several algorithm rotation methods, hash method, least connection method, least missing method, fastest response method, and encryption method. Here we can use the authorization method to allocate requests.

Upstream Jq_one {
Server 127.0.0.1: 8082 weight = 4;
Server 127.0.0.1: 9000 weight = 1;
Ip_hash;
}

You can use weight to set the weight of each server to the requested site. The higher the value, the more requests are allocated.

5. Can I obtain the actual IP address of the user's request in the code because the request is forwarded by nginx?

The answer is yes. Set the following request header information on the localtion node:

# Set the host header and client real address so that the server can obtain the real IP address of the client
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

The Real IP address can be obtained through Request. Headers ["X-Real-ip"] in the code.

6. nginx implements static file (image, js, css) caching

Add a new localtion under the server node
# Static resource cache settings
Location ~ \. (Jpg | png | jpeg | bmp | gif | swf | css) $
        {
Expires 30d;
Root/nginx-1.9.3/html; # root: # static file address, set here under/nginx-1.9.3/html
Break;
        }

This is the index page code <li> </li>




Summary

Through nginx, we have implemented a simple load balancing, which is much more complicated than this. For example, if the nginx server goes down, our site will go down directly. Correct, the keepalived component is used to set up multiple nginx services to provide services. This article is only the beginning of the distributed system. Redis Cache will be released in the future, and the article on database implementation of distributed architecture will be coming soon! Hope you can get the guidance of the blog Park distributed Daniel.


Another articleHow to build a server load balancer using Nginx in combination with iis in windowsIt is also worth sharing.

Because the project has encountered a large number of image storage problems, although we still have a lot of images (currently up or down 1 TB, it is estimated that the growth rate is 1.3 times per year ), when I think about how to effectively store a large number of images, I find some materials and see that some people use Nginx to build servers for the purpose of learning, I personally experienced the installation process of nginx in the window and used iis to build a server load balancer. The environment is as follows:


To demonstrate the effect, I installed a virtual machine on my own computer.

Computer A: Nginx is installed on this computer and IIS is also configured. In order not to conflict with Port 80 of Nginx, it is necessary to modify the port number to work with virtual machine B, which is equivalent to two servers for load balancing.

Virtual Machine B: simulate a server, create IIS, and use port 80 on the port number.

The following describes the resources used in the installation process.

Virtual machine resources:

Thunder download address: http://6.jsdx3.crsky.com/software1/VMwareworkstation-v9.0.1.zip

VM user guide material: http://open-source.blog.163.com/blog/static/1267734512010714103659611/

Windows Image resources: http://www.jb51.net/ OS /windows/Win2003/1904.html

Nginx resources:

Chinese nginx: http://www.ostools.net/apidocs/apidoc? Api = nginx-zh

Install Nginx

1. After downloading, decompress the file to your specified directory, not necessarily under C: \. I put it myself.


It is also possible, that is, at startup, you need the CMD command to locate to the D: \ program \ nginx directory.

Note: I downloaded this version of the nginx-1.2.1, unzip, change the file name nginx-1.2.1 to nginx

2. Configure the conf/nginx. conf file in the directory before the installation.


Then modify


Change 80 to any port number, and change it to 8090 here.

3. Open the CMD command line tool and go to the nginx installation directory. Here I am D: \ program \ nginx


Enter nginx


Press Enter. If no prompt is displayed, the installation is successful. Enter 127.0.0.1: 8090 in the browser.


It takes only one small step to succeed.

The command to disable Nginx is: nginx? S stop

4. Configure the server. Remember to restore the modified conf/nginx. conf file to avoid confusion during subsequent settings.

As mentioned above, I have installed Nginx on the 192.168.21.1 computer, so configure IIS above and set up a test site www.nginxtest.com with Port 801.


An index.html page is placed under this site.

5. Configure IIS on the VM. The only difference is that you can set the port number to 80.

6. Modify the nginx configuration file conf/nginx. conf and the result is:


Here is a detailed description:

A Region. The upstream here is added and must be added before the server {} node configuration.

This indicates that two machines are used for load balancing. Www.nginxtest.com is a test site configured by myself.

In Area B, the nginx listening port is configured here. Port 80 is configured by default and the IP address of the Nginx server is used here.

Region C: the domain name to be proxy

After the configuration is complete, start nginx again. Here there are two NGINX


Enter the address in the browser: www.nginxtest.com

If you refresh multiple times, the following two cases will occur, and the two-day service load balancing effect will be achieved.


Refresh several times, which will appear alternately. Success!


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.