暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

HAProxy vs Nginx

零君聊软件 2020-03-26
1455

HAProxy和Nginx都是比较优秀的reverse proxy(反向代理),但它们之间的区别还是挺大的。本文对两者做一个简单的对比。


功能比较

HAProxy和Nginx二者的定位还是有些区别的。Nginx其实是一个web server,而且支持内容缓存机制。虽然Nginx也是一个reverse proxy(load balancer),却是通过两个module(如下)支持的,分别支持7层和4层代理。

    ngx_http_proxy_module
    ngx_stream_proxy_module


    而HAProxy则是一个纯粹的reverse proxy(load balancer),同样支持4层和7层。单纯就LB来说,HAProxy支持ACL(Access Control Lists),比Nginx功能更强大。因为HAProxy追求零拷贝(zero copy)的转发,代码层面的优化做到了极致,所以HAProxy的转发性能更高。

    进程模型
    Nginx采用master-worker的进程模型。master进程只有一个,主要作用是加载配置并管理worker进程。worker进程作用主要是处理请求。


    HAProxy也支持多进程,但并不是推荐的配置方式。HAProxy 1.8之后开始支持多线程模型,而且也是推荐的方式。所以HAProxy和Nginx一样,都可以有效地利用多核。


    二次开发

    Nginx完全是模块化的扩展机制,开发定制的模块相对比较容易。而HAProxy模块化就差很多了。虽然HAProxy也提供了一些企业级的模块,但开发者要自己开发定制的模块,难度还是挺大的。HAProxy代码层面追求极致的优化,所以有很多小技巧,理解起来不是那么容易。 


    从社区活跃度来说,Nginx比HAProxy要活跃很多,而且Nginx的star/fork数量明显高于HAProxy。


    Monitoring

    Nginx开源版只提供了7个状态信息,具体如下:

      Active connections: 当前活跃的连接数
      accepts: 接受的用户连接数
      handled: 当前处理的连接总数。通常与accepts相同,除非达到了资源上限
      requests: 用户请求总数
      Reading: nginx正在读取请求头的连接数
      Writing: nginx正在写的连接数
      Waiting: 正在等待请求的空闲连接s数

      具体可参考:

      http://nginx.org/en/docs/http/ngx_http_stub_status_module.html


      如果要查看更多的metrics信息,那对不起,请使用收费版nginxplus(nginx+)。


      HAProxy则提供了将近100个metrics,具体参考:

      https://cbonte.github.io/haproxy-dconv/2.1/management.html#9

      而且HAProxy还提供了一个非常实用的监控页面,如下:


      HAProxy也可以与grafana集成,用grafana来浏览HAProxy的metrics,具体参考:

        https://hub.docker.com/r/prom/haproxy-exporter/
        https://grafana.com/grafana/dashboards/2428


        收费?

        Nginx和HAProxy都是开源的项目,都是用C语言开发的。但他们又都有各自的收费版本,Nginx的商业版就是Nginxplus (或者写成Nginx+)。


        HAProxy分社区版和企业版。社区版免费,企业版则是收费的。具体参考:

        https://www.haproxy.com/products/community-vs-enterprise-edition/


        但是HAProxy常用的重要功能基本都包含在社区版中,例如sticky session、DNS resolve、High avalibility等。而开源版的nginx则不包含这些功能。所以如果单纯要部署Load balancer,一般情况下,HAProxy社区版就足够了。


        总之,如果你需要一个LB,并对价钱敏感,那么就远离Nginx!


        Documentation

        Nginx的官方并没有对开源版(Nginx)和收费版(Nginx+)分别提供不同的文档,他们的文档入口相同,如下:

        http://nginx.org/en/docs/


        但是在文档中会对某些commercial feature做特别说明。点击上面页面中的“Admin's Guide”会打开Nginx+的文档链接。虽然Nginx+的文档主要是讲的收费的部分,但也会适当兼顾到开源版。


        HAProxy对社区版和企业版有不同的文档,分别如下。

          Community version:
          https://cbonte.github.io/haproxy-dconv/
          Enterprise version:
          https://www.haproxy.com/documentation/hapee/


          配置比较

          通过两个简单的例子,直观感受一下吧。


          Nginx:

            user  nginx;
            worker_processes  1;
            error_log  /var/log/nginx/error.log warn;
            pid        /var/run/nginx.pid;
            events {
                worker_connections  1024;
            }


            http {
               upstream backend {
                   server www.example1.com;
                   server www.example2.com;
               }
               server {
                   listen 8080;
                   location / {
                       proxy_pass http://backend; 
                       health_check;
                   }
               } 


            HAProxy:

              global
              pidfile /var/run/haproxy.pid
              daemon
              nbproc 1
              maxconn 4096
              tune.ssl.default-dh-param 2048
              ssl-server-verify none
              stats socket var/run/haproxy-admin.sock mode 660 level admin


              defaults
              timeout connect 6000ms
              timeout client 60000ms
              timeout server 60000ms
              log stdout format raw daemon info


              frontend stats
              bind *:443 ssl crt etc/haproxy/server.pem
              mode http
              stats enable
              stats uri stats
              stats auth admin:abcd1234


              frontend http_in
              bind *:8080
              mode http
              default_backend myservers


              backend myservers
              balance leastconn
              mode http
              option httpchk GET
              server app1 192.168.1.100 check
              server app2 192.168.1.200 check


              集群

              Nginx和HAProxy自身都支持高可用(HA)部署,也就是可以部署多个实例组成一个集群。注意这里说的并不会后端的集群,而是多个LB实例,LB自身组成一个集群。

               

              HAProxy是在global配置块中定义peers,来列出多个HAProxy实例,例如(注:关键字peer可以用server取代):

                global
                    peers mypeers 
                        peer haproxy1 192.168.1.100:15015
                        peer haproxy2 192.168.1.200:15015
                        peer haproxy3 192.168.1.300:15015


                定义集群很容易,但通常还有一些状态信息需要在集群内多个HAProxy实例之间同步。例如在需要支持session persistence的应用中,就需要同步stick table数据。下面的例子中,就是将HTTP resposne中的cookie保存到stick table中,并同步到mypeers中定义的其它HAProxy实例。而在接收到HTTP request时,根据header中的cookie来转发到同一个后端来处理。

                  backend myservers
                      balance leastconn
                      mode http
                      dynamic-cookie-key MYKEY
                      cookie cookie1 insert dynamic maxidle 10m
                      stick-table type string len 32 size 8expire 10m peers mypeers
                      stick on req.cook(cookie1)
                      stick store-response res.cook(cookie1)


                  Nginx是通过一个module来支持在多个nginx实例之间同步共享内存区中的数据,具体参考如下链接。注意这个module不是免费的。

                  http://nginx.org/en/docs/stream/ngx_stream_zone_sync_module.html


                  另外,为了在多个nginx实例的场景下支持sticky session,那么必须使用"Sticky learn"的方式来配置cookie。同样,"Sticky learn"也不是免费的。总之,nginx对集群、高可用(HA)的支持不是免费的。


                  Reference

                    http://nginx.org/en/docs/
                    https://docs.nginx.com/nginx/admin-guide/
                    https://cbonte.github.io/haproxy-dconv/
                    https://www.keycdn.com/support/haproxy-vs-nginx
                    https://thehftguy.com/2016/10/03/haproxy-vs-nginx-why-you-should-never-use-nginx-for-load-balancing/
                    https://www.loadbalancer.org/blog/nginx-vs-haproxy/
                    https://www.freelancinggig.com/blog/2017/04/26/haproxy-vs-nginx-software-load-balancer-better
                    https://www.howtoforge.com/tutorial/how-to-setup-haproxy-as-load-balancer-for-nginx-on-centos-7/

                    --END--

                    文章转载自零君聊软件,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                    评论