542179528 发表于 2018-11-18 13:12:59

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时必须先安装相应的编译工具
  #yum -y install gcc gcc-c++ autoconf automake
  #yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
  zlib:nginx提供gzip模块,需要zlib库支持
  openssl:nginx提供ssl功能
  pcre:支持地址重写rewrite功能
  2)安装nginx:
  # tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/
  # cd /usr/local/src/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)编译和安装:
  #make -j 4
  #make install
  4)生成运行nginx的用户:
  # useradd -u 8000 -s /sbin/nologin nginx
  5)启动nginx:
  # /server/nginx-1.8.0/sbin/nginx
  # echo '/server/nginx-1.8.0/sbin/nginx & ' >> /etc/rc.local
  6)测试: http://192.168.1.63/

  
  7)nginx服务日常操作::
  (1)测试配置文件是否正确语法:
  # /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)重新加载配置文件

  # /server/nginx-1.8.0/sbin/nginx -s>  (3)关闭与开启nginx
  # /server/nginx-1.8.0/sbin/nginx -s stop
  # /server/nginx-1.8.0/sbin/nginx -s start #没有start参数
  nginx: invalid option: "-s start"
  7)配置nginx成为分发器,实现动静分离
  # cd /server/nginx-1.8.0/conf #配置文件目录
  # cp nginx.conf nginx.conf.back #备份一下配置文件
  # vim nginx.conf
  # 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服务器配置文件:
  # /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

  # /server/nginx-1.8.0/sbin/nginx -s>  2、配置后端服务器: xuegod62
  (1)配置web服务器:
  # yum install httpd php -y
  (2)生成静态测试文件:
  root@xuegod62 html]#echo 192.168.1.62 > /var/www/html/index.html
  (3)生成动态测试文件:
  #vim /var/www/html/test.php #写如以下内容:
  192.168.1.62-php
  
  (4)生成图片文件:上传如下图片,到“xuegod62网站/var/www/html/目录下:

  (5)启动apache服务器:
  # service httpd restart
  3、配置后端服务器:xuegod64
  (1)配置web服务器:
  # yum install httpd php -y
  (2)生成静态测试文件:
  #echo 192.168.1.64 > /var/www/html/index.html
  (3)生成动态测试文件:
  #vim /var/www/html/test.php #写如以下内容:
  192.168.1.64-php
  
  (4)生成图片文件:--上传如下图片,到“xuegod64网站/var/www/html/目录下:

  (5)重启apache服务器
  # service httpd restart
  4、测试
  (1)测试负载均衡及动静分离---静态页面:
  =
  (2)测试动静分离及负载均衡---动态页面:
  


  
  (3)测试图片负载均衡:
  


  
  (4)测试自动剔除坏的节点:
  # service httpd stop
  http://192.168.1.63/pic.jpg

  
  5、测试性能:
  扩展: 文件打开数过多
  3html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html #运行正常
  3html]# 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文件太多。
  # ulimit -n
  1024#系统默认一个进程最多同时允许打开1024的文件
  # ulimit -n 10240#修改默认允许同事打开10240个文件
  # 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 (mean)
  Time per request:       0.559 (mean, across all concurrent requests)
  Transfer rate:          488.82 received
  Connection Times (ms)
  minmean[+/-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:         16127 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]
查看完整版本: nginx+Apache负载均衡及动静分离