Day 28th: Nginx Extended Load Balancing cluster

Source: Internet
Author: User

Small Q: Aristotle divides man's happiness into three categories: happiness from the outside, happiness from the soul;

And the happiness from the flesh.


What I learned today was beyond expectation, not only because I got up early, but I was very good at noon and didn't rest. But after reading this section of the heart is a little shaky; After looking at a lot of high-availability load Balancing cluster content, suddenly do not know how to take notes, the contents of a piece came up, but dare not to remember, I am afraid that the day can not be remembered, and then gave up a lot of fragmentary content, and so build a special path to study the




A: Introduction

Nginx is a lightweight Web server + reverse proxy server + Email proxy server.

What we used to say is that as a Web server to build LNMP architecture, today is a reverse proxy to build load-balancing, in fact, is a proxy extension;

As a Load Balancer server: Nginx can either directly support Rails and PHP programs externally, or support external service as an HTTP proxy server.


As a large web site, a highly available load cluster is essential, but our access to the site is not possible to enter the domain name access, but LVS is based on TCP/IP, only support access to IPs, and to solve this problem is nginx, he is based on 7 application layer development, support reverse proxy resolution domain name function;

There is a need, that is, a server has a lot of web sites, or a site has multiple directories, when the customer requests a website or a directory, we use a specially designated server to access,

Configuration of this need is not nginx;





II: Preparatory work


Note: This experiment is based on a few previous experiments, otherwise a highly available load Balancer cluster lvs+keepalived needs to be configured from scratch;

Based on our previous experiments, we need to clear the rules first.

Ipvsadm-c

Iptables-t nat-f


Set up a high-availability load Balancer cluster of the machine, we build, continue to use the three operations

and has already installed the Nginx, and has been listening to the 80 port;


director+ Host: 192.168.11.190

Real Server 1:192.168.11.20

Real server+ Preparation: 192.168.11.30

Virtual host vip:192.168.11.100 Because of the DR mode used, so to configure the VIP





Third: domain name instead of IP access

Objective to allow clients to access our website through a domain name, instead of just entering IP access;

Configuration file:

Vim usr/local/nginx/conf/vhosts/1b.conf add content as follows

Upstream Teng {upstream configuring IP modules

Server 192.168.11.20:80; Rs1

Server 192.168.11.30:80; TSt

}


server {

Listen 80; Listening port

server_name www.123.com; Domain name instead of IP


Location/{Authentication mechanism, when accessing 123, go to IP under the Teng module

Proxy_pass http://teng/;

Proxy_set_header Host $host;

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

}

# access_log/home/logs/bbs.access combined;

}


Restart Service:/etc/init.d/nginx restart

Then start the master-slave keepalived Note after Guthrie from , real server script, Nginx service,

Master-Slave port pointing, 80 ports on each machine


Test: Access www.123.com in the browser in this access we also want to configure the Hosts file on Windows

or Curl-xlocalhost www.123.com dir host access can be;




Four: The service corresponds to the specified real server

my needs : When the requested directory is/111 the request is sent to the machine rs1, and when the requested directory is/222/, the request is sent to the machine

RS2, except for directory/111 and directory/222/, other requests are sent to the machine rs2.


Vim usr/local/nginx/conf/vhosts/1b.conf

Upstream QQ

{

Server 192.168.11.20:80;

}

Upstream Teng

{

Server 192.168.11.30; RS2, no port, default 80

}

server {

Listen 80;

server_name 123.com;

location/111/

{

Proxy_pass http://qq/111; It is better to add a/= Directory under

Proxy_set_header Host $host;

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;


}


location/222/

{

Proxy_pass http://teng/222/;

Proxy_set_header Host $host;

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;


}

Location/

{

Proxy_pass http://teng/;

Proxy_set_header Host $host;

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;


}

}

Restart Service:/etc/init.d/nginx restart


Test method: The method is unchanged, mainly you have the idea, you understand why you configure, then how to test that why;


The server in 1.upstream can write multiple, can write multiple upstream;

2. The localtion/222/part can be omitted, because the back location/already contains the/222/, so even if we do not define localtion/222/, it will also be accessed to Teng.



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/74/61/wKioL1Ybsy7gpOH2AAE6UjWbUBg477.jpg "title=" 3.PNG " alt= "Wkiol1ybsy7gpoh2aae6ujwbubg477.jpg"/>






Day 28th: Nginx Extended Load Balancing cluster

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.