Introduction

HAProxy is a popular open-source load balancer and proxy server. It can be used to forward incoming requests to backend servers and distribute traffic across multiple servers.

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

Prerequisites

Before proceeding, ensure you have:

  • Ubuntu Server
  • Root or sudo access
  • A backend server
  • A service running on port 8080

In this example, HAProxy will forward requests from port 8080 to a backend server at xx.xx.xx.xx:8080.

Implementation

Architecture Diagram

Step 1: Install HAProxy

Install HAProxy using the following command:

apt-get install haproxy

Start the HAProxy service:

systemctl start haproxy

Enable HAProxy to start automatically during system boot:

systemctl enable haproxy

Configuration

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 forwards incoming requests on port 8080 from the HAProxy server to xx.xx.xx.xx:8080.

HAProxy Configuration

  • listen – Defines the listening service and port.
  • mode http – Configures HAProxy to operate in HTTP mode.
  • balance roundrobin – Distributes traffic between multiple backend servers using round-robin load balancing.
  • server – Defines the backend server and enables health checking using check.

If multiple backend servers are configured, HAProxy can distribute traffic between them using the roundrobin method.

Restart HAProxy to apply the configuration:

systemctl restart haproxy

Verification

Open the following URL in a browser:

http://<HAProxy-IP-address>:8080

The request should be forwarded to the configured backend server.

You can also check the HAProxy service status using:

systemctl status haproxy

Conclusion

HAProxy can be used to forward and distribute application traffic between backend servers. In this example, we installed HAProxy on Ubuntu and configured it to forward port 8080 requests to a backend server.

The same configuration can be extended by adding multiple backend servers to distribute traffic and provide basic load balancing.

Leave a Reply