What is HAProxy and why it is used?
If you've been in the web-design business for more than five years or so, you may have heard the name HAProxy a couple of times.
At its core it's an efficient and scalable way to load balance several Web servers (eg Web, mail) on a single machine or even on the cloud. That is, instead of spreading traffic between multiple boxes and managing the process yourself, you send it to a central node that handles load balancing as well as some other tasks, usually DNS queries and content caching. The best place to learn more about it is their website -
HAProxy is also popular among those working with the software in question - software which I'm a user of and a reviewer of; I'll go into detail about that later on. For now I'll just say that my experience has been positive - for example, when starting from scratch and using it as a load balancer for a single blog, I set it up, tested it and now it's been serving the site without problem for over two years.
Why did HAProxy become popular? If you're building high-availability stuff, you'd be willing to do extra work to ensure that your customers get a reliable service. As for the reliability aspect, you do run the risk of not getting the expected service by using this technique. However, once you're up and running you get to spend time and money fixing problems related to a failed server rather than spending it dealing with the service outage. All in all - no bad options.
HAProxy can also make life simpler in cases where there are more than two nodes (eg if your site also hosts a blog or forum or eCommerce application) or if you want to have HAproxy manage network traffic only. In these situations you don't want to create individual instances of all the applications, and having one central node take care of everything might be easier, less costly and more scalable.
Also, this article isn't about HAProxy. It's about how to use HAProxy to improve the performance of a WordPress based site. So, assuming you want that, let's move on!
Using an internal load balancer is probably not the cheapest option - most hosts will charge you more for the ability to do that than you'd pay to buy a cheap dedicated machine and put a HAProxy setup on it yourself.
Why is HAProxy better than nginx?
Let's start with a few definitions before we jump into the content.
Application server: A server that runs and manages HTTP web traffic. Nginx is an application server. HAProxy is a load balancer and application proxy.
Load balancer: An entity that distributes incoming workloads across multiple servers. In contrast, nginx is not a load balancer. It does not manage traffic across multiple servers, but rather serves data directly from memory. The nginx service is just a very fast HTTP server which listens for incoming requests and returns the correct file for the requested URL.
Proxy: A layer of an application or network stack which filters traffic. HAProxy is an HTTP proxy. Nginx is not an HTTP proxy. It can load HTTP files from disk, but it does not filter traffic.
The two of them are different use cases, and I've used them both extensively in my career. Nginx has been my favourite for many years. I've used HAProxy mainly during the development of Nginx web sites and HTTP load balancing on Kubernetes for the last few years. You can use one or the other. Both do the job very well.
In this blog post, I'll explore what makes HAProxy better than nginx, some of the common myths about using HAProxy, why choosing HAProxy is not always the best choice, and how you can make use of some open source projects to build out new applications with HAProxy. If you'd like to read the earlier version of this post, please visit the original post here. HAProxy versus Nginx: The Differences. There are some basic differences between HAProxy and Nginx that I'll list below. Some people claim that these differences are very subtle, but they are important to understand if you're thinking of using HAProxy as your web server.
HAProxy does not keep a persistent connection with the user. It passes through each HTTP request to a backend server as it is received. The server receives the request, and responds to the user as required. On the other hand, nginx holds a connection between client and server and stays idle while a single request is processed. This means that there is much less overhead on a highly utilized website. In particular, when a lot of clients hit the same URL at the same time, nginx will create a persistent connection.
Can nginx replace HAProxy?
I've recently setup a cluster of HAProxy servers to load balance traffic to a MySQL server.
The HAProxy setup is running on a RedHat Linux 7 server and the MySQL cluster is running on a RedHat Linux 7 server.
The HAProxy servers are configured to use port forwarding and are able to distribute the traffic to the MySQL servers in a round-robin fashion. The HAProxy servers are also configured to keep-alive connections using TCPKeepAlive. The MySQL servers are configured with bind-address and all servers are configured to listen to the same address. The MySQL servers are configured with the proper permissions for TCP and UDP socket access. The MySQL servers have their ports forwarded and are able to listen to the HAProxy servers for client connections.
There are three HAProxy servers and three MySQL servers in this configuration. The HAProxy servers are setup to listen to all ports (and have clients connect to all ports).
When I run nginx to proxy traffic to the HAProxy servers, there is no issue. However, when I run nginx to proxy traffic to the MySQL servers, it is unable to connect to the MySQL servers. The MySQL servers are listening on the correct ports and have bind-address configured to the HAProxy servers. Nginx seems to be unable to forward the client connections to the MySQL servers.
The HAProxy configuration and the nginx configuration are similar to how HAProxy is setup on the official HAProxy website. Can HAProxy be replaced by nginx? Are there any configuration options that I have missed? UPDATE: For future reference, the answer is "No". HAProxy is not a replacement for nginx.
Yes, you can use nginx in place of HAProxy. You can just set nginx as a load balancer for your MySQL instance(s). You don't need to have a separate load balancer for HAProxy.
Your HAProxy setup is doing a lot of work that nginx can do. Your HAProxy servers should be using keepalives (so that they don't get to busy and to slow down the request). Nginx will do that for you.
The configuration you posted doesn't really match your question.
Related Answers
How much does HAProxy load balancer cost?
HAProxy is a highly versatile and capable load balancer. It also lack...
What is HAProxy used for?
In a nutshell: If you need a load balancer with stateless backend servers, HAProx...
Can HAProxy run on Windows?
Yes, it can. The latest version of the HAProxy daemon, 1.6.1, has been...