设为首页 收藏本站
查看: 1019|回复: 0

[经验分享] nginx+Apache负载均衡及动静分离

[复制链接]

尚未签到

发表于 2018-11-18 13:12:59 | 显示全部楼层 |阅读模式
  使用nginx+Apache负载均衡及动静分离
  介绍
  LB负载均衡集群分两类: LVS (四层)和 nginx或haproxy (七层)
  客户端都是通过访问分发器的VIP来访问网站 在七层中的网站页面有: .php .html .png .jpeg .jsp 等, 有态页面有静态页面。 需要在应用层基于同的应用行分发。
  一:实验拓扑图:
  
DSC0000.png

  二:实验目标
  实战:使用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/
DSC0001.png

  
  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>  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服务器上解析了
  }
  }
  如图:
DSC0002.png

  一下内容注释掉,匹配到php动态类型文件直接在nginx服务器上解析了,再解析给后端服务器:
  如图:
DSC0003.png

  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地址
  如图:
DSC0004.png

  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>  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
  
  (4)生成图片文件:上传如下图片,到“xuegod62网站/var/www/html/目录下:
DSC0005.png

  (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
  
  (4)生成图片文件:--上传如下图片,到“xuegod64网站/var/www/html/目录下:
DSC0006.png

  (5)重启apache服务器
  [root@xuegod64 html]# service httpd restart
  4、测试
  1测试负载均衡及动静分离---静态页面:
  = DSC0007.png DSC0008.png
  2测试动静分离及负载均衡---动态页面:
  
DSC0009.png

DSC00010.png

  
  3测试图片负载均衡
  
DSC00011.png

DSC00012.png

  
  4测试自剔除坏的节点:
  [root@xuegod64 html]# service httpd stop
  http://192.168.1.63/pic.jpg

  
  5、测试性能:
  扩展: 文件打开数过多
  [root@xuegod63html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html #运行正常
  [root@xuegod63html]# ab -n 2000 -c 2000 http://192.168.1.62/index.html #报错
  This is ApacheBench, Version 2.3
  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
  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)


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-636610-1-1.html 上篇帖子: nginx、tomcat、apache 下篇帖子: 程序包管理rpm,yum用法解析及自动化部署apache server实战,让您轻松拥有apache网站!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表