DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Implementing HTTP Load Balancer Using HAProxy on AWS
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It
  • A Guide to Microservices Deployment: Elastic Beanstalk vs Manual Setup
  • A Deep Dive on Read Your Own Writes Consistency

Trending

  • Using Java Stream Gatherers To Improve Stateful Operations
  • Advancing Your Software Engineering Career in 2025
  • Efficient API Communication With Spring WebClient
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. How to Configure HAProxy as a Proxy and Load Balancer

How to Configure HAProxy as a Proxy and Load Balancer

Learn how to configure HAProxy and look into some basic concepts such as ACLs, backends, and frontends in HAProxy Configuration.

By 
Beschi Antony D user avatar
Beschi Antony D
·
Apr. 14, 17 · Tutorial
Likes (16)
Comment
Save
Tweet
Share
129.0K Views

Join the DZone community and get the full member experience.

Join For Free

HAProxy (High-Availability Proxy) is a free, very fast, and reliable solution written in C that offers high-availability load balancing and proxying for TCP- and HTTP-based applications. 

Before we move to configure the proxy, let's look into some of the basic concepts such as ACLs, backends, and frontends in HAProxy Configuration.

Image title

HAProxy as proxying and load balancing server.

ACL

Access Control List (ACL) is a test condition. Actions are performed based on the result of the test conditions, for example, selecting the server to forward the request. The syntax of ACL is acl <aclname> <criterion> [flags] [operator] [<value>] ..., as demonstrated below.

acl acl_myApp path_sub myApp
  • acl_myapp: The name of the ACL.

  • path_sub: A function that validates if the request URL has myApp as a substring.

For more details, please see here. 

Backend

This is a set of servers that actually processes the forwarded requests. The backend consists of a load balancing algorithm and a list of servers with ports.

The basic syntax of the backend is:

backend <backendname>
balance <loadbalancing algorithm>
server <name of the server> <ip>:<port> check
....
server <name of the server> <ip>:<port> check

Here's an example with all of the details filled in:

backend myAppBackend
balance roundrobin
server myAppServer1 172.21.28.1:8080 check
server myAppServer2 172.21.28.2:8080 check
  • myAppBackend: The name of the backend to which the ACL will forward the request.

  • roundrobin: The name of the load balancing algorithm.

  • myAppServer, myAppServer1: The name of the server.

  • 172.21.28.1, 172.21.28.2: IP addresses of servers.

  • 8080: The port on which server listens.

  • check: Specifies to check the health of the server.

Frontend

The frontend defines how requests are forwarded to backends. It is composed of set of IP addresses, a port, ACLs, and use_backend rules.

The syntax is:

frontend <frontend name>
    bind <IPs or wild card>:80
    acl <aclname> <criterion> [flags] [operator] [<value>] ...
    use_backend <backend name> if <aclname>

Here's an example with all of the details filled in:

frontend myAppFrontEnd
  bind *:80
  acl acl_myApp path_sub myApp
  use_backend myAppBackend if acl_myApp
  • myAppFrontEnd: Name of the frontend.

The frontend listens on port 80 to all the interfaces *  available in server. If the URL has myApp as a substring, the request is forwarded to myAppBackend. Otherwise, you will get no service available exception.

Statistics (Optional)

The tatistics report of HAProxy will show the status of the servers, the number of connections, etc. This can be enabled easily by adding the following configuration to the config file:

listen stats
 bind *:<Port>
 stats enable
 stats hide-version
 stats uri </url>
 stats auth <username>:<password>

Here's an example with all of the details filled in:

listen stats
 bind *:9999
 stats enable
 stats hide-version
 stats uri /stats
 stats auth admin:admin@123
  • 9999: Listens on port 9999 for statistics report requests.

  • /stats: The URL of the statistics page (i.e. localhost:9999/stats).

  • admin/admin@123: The username and password to access the statistics page.

The complete content of the configuration file haproxy.cfg is as follows:

#HA Proxy Config
global
 daemon
 maxconn 256

defaults
 mode http
 timeout connect 5000ms
 timeout client 50000ms
 timeout server 50000ms

listen stats
 bind *:9999
 stats enable
 stats hide-version
 stats uri /stats
 stats auth admin:admin@123

frontend myApp
 bind *:80

 acl acl_myApp path_sub myApp

 use_backend myAppBackEnd if acl_myApp

backend myAppBackEnd
 balance roundrobin
 server myAppServer1 127.0.0.1:8081 check
 server myAppServer2 127.0.0.1:8082 check


How to Deploy on Linux

  1. Download the source code of HAProxy.

  2. Unzip the file into the desired location tar xvzf haproxy-1.8-dev1.tar.gz.

  3. Compile the source code using:

  4. cd haproxy-1.8-dev1/
    make TARGET=linux2628
  5. Create config file haproxy.cfg with the configuration details

  6. Start the haproxy with ./haproxy -f haproxy.cfg. 

  7. Access the status page on http:localhost:9999/stats.

The Statistics of HAProxy

Image title

For more details, please refer to the official HAProxy site.

Load balancing (computing) HAProxy

Opinions expressed by DZone contributors are their own.

Related

  • Implementing HTTP Load Balancer Using HAProxy on AWS
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It
  • A Guide to Microservices Deployment: Elastic Beanstalk vs Manual Setup
  • A Deep Dive on Read Your Own Writes Consistency

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!