使用nginx apache负载均衡及动静分离 -尊龙游戏旗舰厅官网
使用nginx apache负载均衡及动静分离
介绍
lb负载均衡集群分两类: lvs (四层)和 nginx或haproxy (七层)
客户端都是通过访问分发器的vip来访问网站 在七层中的网站页面有: .php .html .png .jpeg .jsp 等, 有动态页面有静态页面。 需要在应用层基于不同的应用进行分发。
一:实验拓扑图:
二:实验目标
实战:使用apache nginx实现动静分离的负载均衡集群
三:实验环境
主机作用分类 | 主机名 | ip地址 | 安装软件 |
nginx,代理服务器 | xuegod63.cn | 192.168.1.63 | nginx-1.8.0.tar.gz |
apache,静态页面处理 | xuegod62.cn | 192.168.1.62 | http |
apache,静态页面处理 | xuegod64.cn | 192.168.1.64 | http |
apache,动态页面处理 | xuegod62.cn | 192.168.1.62 | http |
apache,动态页面处理 | xuegod64.cn | 192.168.1.64 | http |
apache,图片处理 | xuegod62.cn | 192.168.1.62 | http |
apache,图片处理 | xuegod64.cn | 192.168.1.64 | http |
四:实验代码
1、配置分发器xuegod63(代理服务器)
1)安装nginx时必须先安装相应的编译工具
[root@xuegod63 ~]#yum -y install gcc gcc-c autoconf automake
[root@xuegod63 ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
zlib: nginx提供gzip模块,需要zlib库支持
openssl:nginx提供ssl功能
pcre:支持地址重写rewrite功能
2)安装nginx:
[root@xuegod63 ~]# tar -zxvf nginx-1.8.0.tar.gz -c /usr/local/src/
[root@xuegod63 ~]# cd /usr/local/src/nginx-1.8.0/
[root@xuegod63 nginx-1.8.0]# ./configure --prefix=/server/nginx-1.8.0 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
参数解释:
--with-http_dav_module 启用ngx_http_dav_module支持(增加put,delete,mkcol:创建集合,copy和move方法)默认情冴下为关闭,需编译开启
--with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
--with-http_addition_module 启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
--with-http_sub_module 启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
--with-http_flv_module 启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_mp4_module 启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)
3)编译和安装:
[root@xuegod63 nginx-1.8.0]#make -j 4
[root@xuegod63 nginx-1.8.0]#make install
4)生成运行nginx的用户:
[root@xuegod63 nginx-1.8.0]# useradd -u 8000 -s /sbin/nologin nginx
5)启动nginx:
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx
[root@xuegod63 nginx-1.8.0]# echo '/server/nginx-1.8.0/sbin/nginx & ' >> /etc/rc.local
6)测试: http://192.168.1.63/
7)nginx服务日常操作: :
(1)测试配置文件是否正确语法:
[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -t
nginx: the configuration file /server/nginx-1.8.0/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx-1.8.0/conf/nginx.conf test is successful
(2)重新加载配置文件
[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -s reload
(3)关闭与开启nginx
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s stop
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s start #没有start参数
nginx: invalid option: "-s start"
7)配置nginx成为分发器,实现动静分离
[root@xuegod63 conf]# cd /server/nginx-1.8.0/conf #配置文件目录
[root@xuegod63 conf]# cp nginx.conf nginx.conf.back #备份一下配置文件
[root@xuegod63 conf]# vim nginx.conf
[root@xuegod63 nginx-1.8.0]# vim /server/nginx-1.8.0/conf/nginx.conf #指定启动nginx用户
改:# user nobody;
为:user nginx nginx;
改:
43 location / {
44 root html;
45 index index.html index.htm; #在location / { 。。。} 中添加以下内容 #定义分发策略
index index.html index.htm;
if ($request_uri ~* \.html$){
proxy_pass http://htmlservers; #匹配到htm静态类型访问的都会转到html服务池中
}
if ($request_uri ~* \.php$){
proxy_pass http://phpservers; #匹配到php动态类型文件直接在nginx服务器上解析了
}
}
如图:
把一下内容注释掉,匹配到php动态类型文件直接在nginx服务器上解析了,不再解析给后端服务器:
如图:
8)在配置文件nginx.conf的最后一行}前,添加以下内容:
upstream htmlservers { #定义静态文件负载均衡服务器组名称
server 192.168.1.62:80;
server 192.168.1.64:80;
}
upstream phpservers{ #定义动态文件负载均衡服务器组名称
server 192.168.1.62:80;
server 192.168.1.64:80;
}
upstream picservers { #定义图片文件负载均衡服务器组名称
server 192.168.1.62:80;
server 192.168.1.64:80;
}
#后期工作中,根据工作中的需要,配置成具体业务的ip地址
如图:
9)重新加载nginx服务器配置文件:
[root@xuegod63 conf]# /server/nginx-1.8.0/sbin/nginx -t
nginx: the configuration file /server/nginx-1.8.0/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx-1.8.0/conf/nginx.conf test is successful
[root@xuegod63 conf]# /server/nginx-1.8.0/sbin/nginx -s reload
2、配置后端服务器: xuegod62
(1)配置web服务器:
[root@xuegod62 html]# yum install httpd php -y
(2)生成静态测试文件:
root@xuegod62 html]#echo 192.168.1.62 > /var/www/html/index.html
(3)生成动态测试文件:
[root@xuegod62 html]#vim /var/www/html/test.php #写如以下内容:
192.168.1.62-php
phpinfo();
?>
(4)生成图片文件: 上传如下图片,到“xuegod62网站/var/www/html/目录下:
(5)启动apache服务器:
[root@xuegod62 html]# service httpd restart
3、配置后端服务器:xuegod64
(1)配置web服务器:
[root@xuegod64 html]# yum install httpd php -y
(2)生成静态测试文件:
[root@xuegod64 html]#echo 192.168.1.64 > /var/www/html/index.html
(3)生成动态测试文件:
[root@xuegod64 html]#vim /var/www/html/test.php #写如以下内容:
192.168.1.64-php
phpinfo();
?>
(4)生成图片文件: --上传如下图片,到“xuegod64网站/var/www/html/目录下:
(5)重启apache服务器
[root@xuegod64 html]# service httpd restart
4、测试
(1)测试负载均衡及动静分离---静态页面:
=
(2)测试动静分离及负载均衡---动态页面:
(3)测试图片负载均衡:
(4)测试自动剔除坏的节点:
[root@xuegod64 html]# service httpd stop
http://192.168.1.63/pic.jpg
5、测试性能:
扩展: 文件打开数过多
[root@xuegod63 html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html #运行正常
[root@xuegod63 html]# ab -n 2000 -c 2000 http://192.168.1.62/index.html #报错
this is apachebench, version 2.3 <$revision: 655654 $>
尊龙游戏旗舰厅官网 copyright 1996 adam twiss, zeus technology ltd, http://www.zeustech.net/
licensed to the apache software foundation, http://www.apache.org/
benchmarking 192.168.1.62 (be patient)
socket: too many open files (24) # 测试时,一次打开的socket文件太多。
[root@xuegod63 ~]# ulimit -n
1024#系统默认一个进程最多同时允许打开1024的文件
[root@xuegod63 ~]# ulimit -n 10240 #修改默认允许同事打开10240个文件
[root@xuegod63 ~]# ab -n 2000 -c 2000 http://192.168.1.62/index.html
this is apachebench, version 2.3 <$revision: 655654 $>
尊龙游戏旗舰厅官网 copyright 1996 adam twiss, zeus technology ltd, http://www.zeustech.net/
licensed to the apache software foundation, http://www.apache.org/
benchmarking 192.168.1.62 (be patient)
completed 200 requests
completed 400 requests
.......
completed 1800 requests
completed 2000 requests
finished 2000 requests
server software: apache/2.2.15
server hostname: 192.168.1.62
server port: 80
document path: /index.html
document length: 13 bytes
concurrency level: 2000
time taken for tests: 1.119 seconds
complete requests: 2000
failed requests: 0
write errors: 0
total transferred: 560000 bytes
html transferred: 26000 bytes
requests per second: 1787.69 [#/sec] (mean)
time per request: 1118.765 [ms] (mean)
time per request: 0.559 [ms] (mean, across all concurrent requests)
transfer rate: 488.82 [kbytes/sec] received
connection times (ms)
min mean[ /-sd] median max
connect: 0 56 216.7 1 1062
processing: 4 71 161.9 24 670
waiting: 4 70 161.9 24 670
total: 16 127 271.1 26 1087
percentage of the requests served within a certain time (ms)
50% 26
66% 26
75% 27
80% 57
90% 717
95% 727
98% 1085
99% 1086
100% 1087 (longest request)
总结
以上是尊龙游戏旗舰厅官网为你收集整理的使用nginx apache负载均衡及动静分离的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: c 判断一个数字是否为质数
- 下一篇: elk 学习笔记