Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Nginx achieve seven-tier load balancing?

2025-04-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The following brings you how Nginx achieves seven-tier load balancing. I hope it can give you some help in practical application. There are many things involved in load balancing, not many theories, and there are many books on the Internet. Today, we will use the accumulated experience in the industry to do an answer.

Implementing seven-tier load balancing with Nginx

Schedule to different groups of backend CVMs

1. Dynamic and static separation

two。 Partition the website

=

Topological structure

[vip: 20.20.20.20]

[LB1 Nginx] [LB2 Nginx]

192.168.1.2 192.168.1.3

[index] [milis] [videos] [p_w_picpaths] [news]

1.11 1.21 1.31 1.41 1.51

1.12 1.22 1.32 1.42 1.52

1.13 1.23 1.33 1.43 1.53

......

/ web/ web/milis / web/videos / web/p_w_picpaths / web/news

Index.html index.html index.html index.html

I. implementation process

Scheme 1 scheduling according to site partition

Http {

Upstream index {

Server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2

Server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2

Server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2

}

Upstream milis {

Server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2

Server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2

Server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2

}

Upstream videos {

Server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2

Server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2

Server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2

}

Upstream p_w_picpaths {

Server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2

Server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2

Server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2

}

Upstream news {

Server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2

Server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2

Server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2

}

Server {

Location / {

Proxy_pass http://index;

}

Location / news {

Proxy_pass http://news;

}

Location / milis {

Proxy_pass http://milis;

}

Location *\. (wmv | mp4 | rmvb) ${

Proxy_pass http://videos;

}

Location *\. (png | gif | jpg) ${

Proxy_pass http://p_w_picpaths;

}

}

Scheme 2 dispatch according to the separation of movement and movement.

Http {

Upstream htmlservers {

Server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=2

Server 192.168.1.2:80 weight=2 max_fails=2 fail_timeout=2

}

Upstream phpservers {

Server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2

Server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2

}

Server {

Location *\ .html$ {

Proxy_pass http://htmlservers;

}

Location *\ .php$ {

Proxy_pass http://phpservers;

}

}

}

Second, Keepalived implements the scheduler HA.

Note: both master and standby schedulers can achieve normal scheduling.

1. Master / standby scheduler installation software

[root@master ~] # yum-y install keepalived

[root@backup ~] # yum-y install keepalived

2. Keepalived

BACKUP1

[root@uplook ~] # vim / etc/keepalived/keepalived.conf

! Configuration File for keepalived

Global_defs {

Router_id director1 / / Auxiliary changed to director2

}

Vrrp_instance VI_1 {

State BACKUP

Nopreempt

Interface eth0 / / heartbeat interface, try to connect the heartbeat separately

Virtual_router_id 80max / the scheduler for the entire cluster is consistent

Priority 100 / / Auxiliary to 50

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Virtual_ipaddress {

20.20.20.20

}

}

BACKUP2

3. Start KeepAlived (both master and slave start)

[root@uplook ~] # chkconfig keepalived on

[root@uplook ~] # service keepalived start

[root@uplook ~] # ip addr

So far:

Can solve heartbeat failure keepalived

Unable to resolve Nginx service failure

4. Extend the Nginx health check for the scheduler (optional)

Train of thought:

Let Keepalived execute an external script at regular intervals. The function of the script is to turn off the local Keepalived when Nginx fails.

A. Script

[root@master ~] # cat / etc/keepalived/check_nginx_status.sh

#! / bin/bash

/ usr/bin/curl-I http://localhost & > / dev/null

If [$?-ne 0]; then

/ etc/init.d/keepalived stop

Fi

[root@master ~] # chmod adepx / etc/keepalived/check_nginx_status.sh

B. Keepalived uses script

! Configuration File for keepalived

Global_defs {

Router_id director1

}

Vrrp_script check_nginx {

Script "/ etc/keepalived/check_nginx_status.sh"

Interval 5

}

Vrrp_instance VI_1 {

State BACKUP

Interface eth0

Nopreempt

Virtual_router_id 90

Priority 100

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass uplook

}

Virtual_ipaddress {

192.168.1.80

}

Track_script {

Check_nginx

}

}

Note: nginx must be started before keepalived

Schedule to the same set of back-end servers

The website is not split by business / section, and all back-end servers provide the whole site code.

=

Topological structure

[LB Nginx]

20.20.20.20

192.168.1.2

[httpd] [httpd] [httpd]

192.168.1.3 192.168.1.4 192.168.1.5

Implementation process

1. Nginx

Http {

Upstream httpservers {

Server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2

Server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2

Server 192.168.1.5:80 weight=2 max_fails=2 fail_timeout=2

Server 192.168.1.100 server 80 backup;, etc. 3, 4, 5 hang up 100 online

}

Server {

Location / {

Proxy_pass http://httpservers;

Proxy_set_header X-Real-IP $remote_addr

}

}

}

2. Apache LogFormat is optional

LogFormat "% {X-Real-IP} I% l% u% t\"% r\ "% > s% b\"% {Referer} I\ "\"% {User-Agent} I\ "" combined

LogFormat "h% l% u% t\"% r\ "% > s% b" common

LogFormat "% {Referer} I->% U" referer

LogFormat "{User-agent} I" agent

3. Nginx LogFormat

=

After reading the above about how Nginx implements layer-7 load balancing, if there is anything else you need to know, you can find out what you are interested in in the industry information or find our professional and technical engineers who have more than ten years of experience in the industry. Official website link www.yisu.com

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report