Nginx implements load balancing and nginx Load Balancing

Source: Internet
Author: User
Tags nginx server redis server nginx load balancing

Nginx implements load balancing and nginx Load Balancing

Load Balancing with nginx

The functions and advantages of Nginx do not need to be mentioned here. Today, we mainly use the nginx Server Load balancer experiment to record the steps and take them as study notes. You can also give them a reference.

1. Nginx Installation
1.: http://nginx.org/en/download.html
2. decompress the package and enter the command as shown in the cmd window To Go To The nginx directory. Use start nginx.exe to install nginx, as shown in:


After the installation is successful, you will see the “nginx.exe process in the timer task manager.
3. enter 127.0.0.1 in the address bar of the browser. The nginx welcome page is displayed. It indicates that Nginx has been installed successfully.

2. website construction and Configuration
1. Set up two iis sites
Create a new site with only one simple index page. Deploy both sites on the local machine and bind ports 8097 and 8098 respectively.

2. Modify nginx configuration information. nginx configuration information is configured in nginx. conf.
A. Modify the nginx listening port and the listen node value under the http server.
Listen 8096;

B. Add the 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 pieces of information.
# The server cluster name is test.com.
Upstream test.com {
Server 127.0.0.1: 8097;
Server 127.0.0.1: 8098;
}
C. Modify the location node under the http Node
Location /{
Root html;
Index. aspx index.html index.htm; # modify the homepage to index. aspx.
# Test.com corresponds to the cluster name set by upstream.
Proxy_pass http: // test.com;
# 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 the configuration file is modified, restart the nginx service,

3. Running result
Access http: // localhost: 8096/index. aspx
    
We can see that our requests were distributed to 8097 and 8098 sites, indicating that the Server Load balancer was successfully established.
Stop the 8098 site and refresh the page. The request is distributed to the 8097 site, indicating that one of the sites is suspended. As long as one site is good, the system can continue to provide services.

4. session sharing
A. Use the database to save session information. You can view my previous article:Database for multi-site sharing Session
B. 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 allocates it to the fixed server (this has not been tested yet)
Upstream test.com {
Server 127.0.0.1: 8097;
Server 127.0.0.1: 8098;
Ip_hash;
}
C. Set up a Redis server to read sessions from the Redis server.

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.