Centos7+Nginx负载均衡(Load Balance)配置详情介绍

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

随着互联网信息的爆炸性增长,负载均衡(load balance)已经不再是一个很陌生的话题,顾名思义,负载均衡即是将负载分摊到不同的服务单元,既保证服务的可用性,又保证响应足够快,给用户很好的体验。快速增长的访问量和数据流量催生了各式各样的负载均衡产品,很多专业的负载均衡硬件提供了很好的功能,但却价格不菲,这使得负载均衡软件大受欢迎,nginx就是其中的一个,在linux下有Nginx、LVS、Haproxy等等服务可以提供负载均衡服务,而且Nginx提供了几种分配方式(策略),当然现在主流的公有云(Windows Azure)提供的服务在均衡分配方式基本上都类似于Nginx的轮询(round robin)分配方式和加权轮询(Weight round robin)分配方式,对于其他公有云产品没有具体试验过,所以我们今天主要介绍一下Nginx下的几种分配方式,具体见下:

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 例如:

upstreamserver_pool{
server192.168.5.21weight=10;
server192.168.5.22weight=10;
}

3、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 例如:

upstreamserver_pool{
ip_hash;
server192.168.5.21:80;
server192.168.5.22:80;
}

4、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

upstreamserver_pool{
server192.168.5.21:80;
server192.168.5.22:80;
fair;
}

注意:整个分配方式是通过修改定义负载均衡的server配置中添加的。

我们今天主要介绍我个人认为使用最多的几种方式吧;前面四种。

我们后端使用两台Apache服务作为WEB服务,具体安装就不多介绍了。

192.168.5.21

Yuminstallhttpd

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

查看httpd 版本

rpm-qa|grephttpd

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

接下来我们首先要为apache定义一个 默认的页面,方便区分;我们为了后面的数据统计所以页面内容显示的比较少

Vim/var/www/httml/index.html
</html>
<!DOCTYPEhtml>
<html>
<head>
<title>WelcometoApache</title>
<style>
body{
35em;
margin:0auto;
font-family:Tahoma,Verdana,Arial,sans-serif;
}
</style>
<styletype="text/css">
h1{color:red}
h2{color:blue}
h3{color:green}
h4{color:yellow}
}
</style>
</head><bodybgcolor='7D7DFF'>
<h2>HostName:A-S----->IP:192.168.5.21</h2>
</body>
</html>

保存退出,启动服务

Systemctlstarthttpd

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

然后添加默认的防火墙端口8o

Firewall-cmd--zone=public--add-port='80/tcp'--permanent
或者vim/etc/firewalld/zone/public.xml
添加一下格式
<portportocal='tcp'port='80'>

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

我们测试访问

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

我们准备第二台WEB服务(192.168.5.22)我们按照第一台的方式进行配置,再次就跳过了。

第二台主机的配置:192.168.5.22 主机名 B-S

安装好httpd后,我们将a-s上的index拷贝到b-s服务器上

scpindex.htmlroot@192.168.5.22:/var/www/html/

然后修改index.html文件

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

我们为了后面的测试,我们将两台服务器的显示内容修改一下,为了通过服务进行批量测试。

将两台Apache WEB服务的index.html文件只显示服务器的名称及IP地址。

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

接下来我们就部署nginx,在Centos7上yum安装需要定义yum源

cd/etc/yum.repo
vimepel.repo
添加以下内容
[epel]
name=aliyunepel
baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

Yuminstallnginx
nginx192.168.5.20

Centos7+Nginx负载均衡(Load Balance)配置详情介绍

启动服务

systemctlstartnginx

dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。