Load Balancing of three virtual machines with Nginx in CentOS Environment

Source: Internet
Author: User

Load Balancing of three virtual machines with Nginx in CentOS Environment

Server Load balancer

First, let's take a brief look at what Server Load balancer is. Simply understanding what it means literally can explain that N servers are equally loaded, A server is not idle because of its high load downtime. The premise of Server Load balancer is that multiple servers are required to achieve load balancing, that is, two or more servers.

Test Environment
Three CentOS instances are installed in VMware.

Server a ip Address: 192.168.0.219 (master)

Server B IP Address: 192.168.0.119

IP address of the C server: 192.168.0.109

Deployment ideas
Server A acts as the master server, and the domain name is directly resolved to server A (192.168.0.219). server A performs load balancing on server B (192.168.0.119) and server C (192.168.0.109.

On server A, the upstream command is used to allocate loads.

Vi/etc/nginx/conf. d/default. conf

 


Upstream 192.168.0.219 {
Server 192.168.0.119: 80;
Server 192.168.0.109: 80;
}


Server {
Listen 80;
Server_name 192.168.0.219;
Charset utf8;

Location /{
Proxy_pass http: // 192.168.0.219;
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
}
}

Save and restart nginx

 


On server B and server C,

Vi/etc/nginx/conf. d/default. conf

 


Server {
Listen 80;
Server_name 192.168.0.219;
Index index.html;
Root/usr/share/nginx/html;

}

Save and restart nginx

Test
When you access http: // 192.168.0.219, write a different index.html file under the B 、c server to differentiate which server is used for processing.

Open the browser to access a.com and refresh the results. All requests are allocated to server B (192.168.0.119) and server C (192.168.0.109) by the master server (192.168.5.149, load Balancing is achieved.

Does the master server provide services?
In the preceding example, the Server Load balancer of the master server is applied to other servers. Can the master server itself be added to the server list, in this way, a server is not wasted as a forwarding function, but also involved in the provision of services.

How can this problem be solved? Because port 80 is already used to listen for Server Load balancer processing, the server cannot use port 80 to process access requests for 192.168.0.219. A new one is required.

Then, edit/etc/nginx/conf. d/default. conf on the master server and add the following content:

Server {
Listen 8080;
Server_name 192.168.0.219;
Index index.html;
Root/usr/share/nginx/html;
}

Restart nginx

Then, re-configure the new load balancing.

For more Nginx tutorials, see the following:

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

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.