您的位置:首页 > 理论基础 > 计算机网络

实验:部署实现nginx在http上的动静分离和负载均衡

2017-05-13 12:09 766 查看
首先复习一下LB Cluster负载均衡集群
四层:
LVS
Nginx(stream)
Haproxy(mode_tcp)
七层
Http protocol
Nginx(http,upstream)
Haproxy(mode http)
Httpd/ats/perlbal/pound/…

接下来如何实现nginx在http的负载均衡

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)

2、实验目的。实现nginx对静态内容和动态内容的负载均衡
3、开始配置操作
在172.18.10.10下编辑php页面
[root@localhost ~]# vim /var/www/html/index.php
<h1>HTTPD listend on 80 Server1</h1>
<?php
phpinfo();
?>
将实验页面发至172.18.10.11的页面文件存放路径下
[root@localhost ~]# scp /var/www/html/index.php 172.18.10.11:/var/www/html/
修改Server1为Server2
<h1>HTTPD listend on 80 Server2</h1>
<?php
phpinfo();
?>

4、常识使用谷歌浏览器请求两个地址,看看是否测试页面能够正常显示--------经测试发现能够正常显示
5、配置静态站点的nginx
将准备好的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)
测试结果:实现动态内容的负责均衡
最终实现动静分离,而在静态内容上面我们还可以定义缓存,提升效率。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息