nginx和apache关于负载均衡的比较

来源:互联网 发布:淘宝怎么取消短信验证 编辑:程序博客网 时间:2024/05/04 13:55

安装apache2.2.17

gzip -d httpd--****.tar.gz
tar xvf httpd---***.tar
apache的安装参数
./configure --with-included-apr --prefix=/home/lhh/apache2 --enable-so --enable-mods-shared=proxy

apache的prefork参数

<IfModule mpm_prefork_module>
    StartServers                 800
    MinSpareServers          800
    MaxSpareServers         1000
    ServerLimit                   1500
    MaxClients                    1500
    MaxRequestsPerChild   1000
</IfModule>

代理配置

ProxyPass /test balancer://mycluster stickysession=JSESSIONID|jsessionid scolonpathdelim=On
<Proxy balancer://mycluster>
        BalancerMember http://192.168.128.136:8180/test
        BalancerMember http://192.168.128.136:8280/test
        BalancerMember http://192.168.128.139:8180/test
</Proxy>

安装nginx

./configure --prefix=/home/zxg/nginx --with-http_stub_status_module --with-http_ssl_module --with-cc-opt='-O2'

nginx的参数设置

use epoll;#仅用于linux

代理配置

    #add by zhao,xingguo
    upstream balance-tomcat  {
            server   192.168.128.136:8180;
            server   192.168.128.136:8280;
            server   192.168.128.139:8180;
    }
      server
      {
              listen  8085;
              server_name  192.168.128.139;

              location / {
                       proxy_redirect     off;
                       proxy_pass         http://balance-tomcat;
              }
      }

说明:

1.apache和nginx都作为代理服务器,代理三台相同的tomcat服务器

2.每台tomcat服务器最大性能大概为970request/s(代码里有Thread.sleep(200l);模拟业务)

3.四次测试结果如下:

apache和nginx的反向代理的比较,单位request/s
nginx  800--8000  3248  2409  2774  2135  2410 
apache 600--8000  2431  1645  2198  1288  1909

由此分析可以看出

1.nginx性能高,占用cpu,内存少

2.apache的进程开的太多了,真烦。

3.高并发下nginx有完美的表现

4.apache有会话保持功能,尽管nginx有ip_hash但是不能代替session stick

5.尽量的用nginx和apache同时代理后端的服务器,

nginx对外部的webservice(性能高),apache对外部的web访问(session stick)。