下文给大家带来怎么样实现Nginx在http的负载均衡,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用编程之家在行业内累计的经验来做一个解答。
首先复习一下LB Cluster负载均衡集群
四层:
LVS
Nginx(stream)
Haproxy(mode_tcp)
七层
Http protocol
Nginx(http,upstream)
Haproxy(mode http)
Httpd/ats/perlbal/pound/…
ngx_stream_proxy_module模块能为http服务做调度,其中stream模块中有
专门的server子命令,不同于其他server,其他server是用于定义虚拟主机的
而stream模块中的server是用来定义组中的一台云服务器的,server可以重复使用多次,
#################################################################
1、实验环境准备,至少准备三台主机,其中一台做为Nginx调度服务器,装备两块网卡
分别在三台主机上配置Nginx,httpd和httpd,并测试可以成功访问页面
[root@localhost Nginx]# curl http://172.18.10.10:80/test1.html
Test Page 1 on UpStream Server 1 (172.18.10.10)
[root@localhost Nginx]# curl http://172.18.10.11:80/test1.html
Test Page 1 on UpStream Server 2 (172.18.10.11)
将172.18.10.10和172.18.10.11做为动态站点(安装httpd+PHP,即ap,listen 80)
将172.18.10.10和172.18.10.11做为静态站点(其中10.11安装Nginx,监听8080,10.10配置虚拟主机,监听80和8080)
3、开始配置操作
[root@localhost ~]# vim /var/www/html/index.PHP
<h3>HTTPD listend on 80 Server1</h3>
<?PHP
PHPinfo();
?>
[root@localhost ~]# scp /var/www/html/index.PHP 172.18.10.11:/var/www/html/
修改Server1为Server2
<h3>HTTPD listend on 80 Server2</h3>
<?PHP
PHPinfo();
?>
4、常识使用谷歌浏览器请求两个地址,看看是否测试页面能够正常显示--------经测试发现能够正常显示
将准备好的Nginx安装包分别scp到两台主机上
[root@localhost ~]# scp Nginx-1.6.2-1.el6.ngx.x86_64.rpm 172.18.10.10:/root/
6、安装Nginx
[root@localhost ~]# yum install Nginx-1.6.2-1.el6.ngx.x86_64.rpm
7、配置静态站点的虚拟服务
172.18.10.10上:
注释DocumentRoot路径
#DocumentRoot "/var/www/html"
添加新的监听端口
#Listen 12.34.56.78:80
Listen 80
Listen 8080
添加虚拟主机,分别监听在80和8080端口上
<VirtualHost *:80>
DocumentRoot /var/www/shope
ServerName www.magedu.com
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot /var/www/html
ServerName imgs.magedu.com
</VirtualHost>
保存退出
创建目录
[root@localhost ~]# mkdir /var/www/shope
将index.PHP移至该目录下
[root@localhost ~]# mv /var/www/html/index.PHP /var/www/shope/
检查语法
[root@localhost ~]# httpd -t
重启httpd服务
[root@localhost ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
在浏览器端分别访问测试80和8080端口
结果正常
172.18.10.11上:
[root@localhost ~]# vim /etc/Nginx/conf.d/default.conf
将虚拟主机监听端口改为8080
listen 80———————------》 listen 8080;
更改root路径
root /usr/share/Nginx/html;—————》root /data/html;
创建虚拟主机目录路径
[root@localhost ~]# mkdir /data/html -pv
mkdir: created directory `/data'
mkdir: created directory `/data/html'
将所有test文件移至/data/html目录下
[root@localhost ~]# mv /var/www/html/test* /data/html/
启动Nginx服务,并且查看是否端口监听
[root@localhost ~]# Nginx
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:8080 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25
访问页面。看看是否能够正常访问
8、配置Nginx调度端的Nginx服务在172.18.200.100上
[root@localhost ~]# vim /etc/Nginx/Nginx.conf
#使用默认的加权轮询算法,进行会发绑定
upstream websrvs {
server 172.18.10.10:80 weight=2 max_fails=2 fail_timeout=2;
server 172.18.10.11:80 weight=3;
}
upstream staticsrvs {
server 172.18.10.10:8080 weight=1;
server 172.18.10.11:8080 weight=1;
}
9、编辑调度的方法
[root@localhost ~]# vim /etc/Nginx/conf.d/default.conf
index index.PHP index.html; ####全局定义,先后顺序
location / {
proxy_pass http://websrvs; ####动态资源加载路径定义
root /usr/share/Nginx/html;
index index.PHP index.html index.htm;
}
location ~* \.(jpg|jpeg|png|gif|html)$ {
proxy_pass http://staticsrvs; #####静态资源加载路径定义
index index.PHP;
}
10、从新加载测试
[root@localhost ~]# Nginx -t
Nginx: the configuration file /etc/Nginx/Nginx.conf Syntax is ok
Nginx: configuration file /etc/Nginx/Nginx.conf test is successful
[root@localhost ~]# Nginx -s reload
打开谷歌浏览器,输入http://172.18.200.100/刷新页面发现会有如下的页面内容来回切换
HTTPD listend on 80 Server2
HTTPD listend on 80 Server1
请求http://172.18.200.100/index.PHP,发现也是如下页面内容来回切换
HTTPD listend on 80 Server2
HTTPD listend on 80 Server1
从其他客户端使用curl来测试
[root@localhost ~]# for ((i=1;i<=10;i++));do curl http://172.18.200.100/index.PHP; done
接下来请求静态文件:http://172.18.200.100/test1.html,不断刷新,发现得到如下内容来回切换
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
从其他客户端使用curl来测试
[root@localhost ~]# for ((i=1;i<=10;i++));do curl http://172.18.200.100/test1.html; done
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
测试结果:实现动态内容的负责均衡
最终实现动静分离,而在静态内容上面我们还可以定义缓存,提升效率。
看了以上关于怎么样实现Nginx在http的负载均衡,如果大家还有什么地方需要了解的可以在编程之家行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,编程之家技术工程师在行业内拥有十几年的经验了。编程之家官网链接www.jb51.cc