How to Install and Configure HAProxy on Ubuntu

Introduction

HAProxy is a high-performance load balancer and proxy that can be used to forward incoming requests to backend servers. It is useful when you need to route traffic from one server to another service or distribute traffic across multiple backend servers.

In this post, we will explain how to install HAProxy on an Ubuntu server and configure it to forward requests received on port 8080 to a backend server running on port 8080.

Prerequisites

Before proceeding, ensure you have:

  1. An Ubuntu server with root or sudo access.
  2. A backend server running the required service.
  3. A custom service that can receive requests on port 8080.

In this example, requests received on port 8080 of the HAProxy server will be forwarded to another server at xx.xx.xx.xx:8080.

Implementation

Step 1: Install HAProxy

Install HAProxy using the following command:

apt-get install haproxy

Step 2: Start and Enable HAProxy

Start the HAProxy service:

systemctl start haproxy

Enable HAProxy to start automatically after a system reboot:

systemctl enable haproxy

Configuration

In this example, we will use the default HAProxy configuration and add a listener that forwards requests from port 8080 to the backend server.

Open the HAProxy configuration file:

vi /etc/haproxy/haproxy.cfg

Add the following configuration:

listen nameapp 0.0.0.0:8080
    mode http
    balance roundrobin
    server server1 xx.xx.xx.xx:8080 check

The above configuration listens for requests on port 8080 of the HAProxy server and forwards them to xx.xx.xx.xx on port 8080.

Configuration Details

  • listen nameapp – Defines the HAProxy listener and gives it a name.
  • 0.0.0.0:8080 – HAProxy listens for incoming connections on port 8080 on all available interfaces.
  • mode http – Configures HAProxy to handle HTTP traffic.
  • balance roundrobin – Distributes traffic between multiple backend servers using the round-robin method when multiple servers are configured.
  • server server1 – Defines the backend server to which the requests are forwarded.
  • check – Enables health checking of the backend server.

If multiple backend servers are added, HAProxy can distribute incoming traffic between them using the configured load-balancing method.

Step 3: Restart HAProxy

Restart the HAProxy service to apply the configuration:

systemctl restart haproxy

Verification

Access the HAProxy server using port 8080 from a browser:

http://<HaproxyIPaddress>:8080

The request should be forwarded to the configured backend server.

Conclusion

HAProxy can be used to efficiently forward incoming requests to backend services and distribute traffic across multiple servers. In this example, HAProxy was installed on Ubuntu and configured to forward port 8080 requests to a backend server.

This basic configuration can also be extended by adding multiple backend servers and using HAProxy’s load-balancing capabilities.

FAQ

1. What is HAProxy used for?

HAProxy is commonly used as a load balancer and proxy to route incoming traffic to backend servers.

2. Which configuration file is used by HAProxy?

The default HAProxy configuration file is:

/etc/haproxy/haproxy.cfg

3. How can I restart HAProxy after changing the configuration?

Run:

systemctl restart haproxy

4. Can HAProxy forward traffic to multiple servers?

Yes. Multiple backend servers can be added to the configuration, and HAProxy can distribute traffic between them using a load-balancing method such as roundrobin.

5. How can I verify that HAProxy is working?

Access the HAProxy server using its IP address and configured port, for example:

http://<HaproxyIPaddress>:8080

If the configuration is working correctly, the request will be routed to the backend server.

How to Lock and Unlock a User on Ubuntu 24.04?

Liquibase Installation on Ubuntu 24

Talk to our experts

Have a technology challenge or are you looking for the right solution for your business? Our team can help you with cloud, DevOps, development, infrastructure, design, and more. Feel free to reach out to our experts here.

admin

Writes about Web & Architecture at Pheonix Solutions.

Leave a Reply

Scroll to Top