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
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

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

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

  • Exploring Reactive and Proactive Observability in the Modern Monitoring Landscape
  • Understanding the Mandelbrot Set: A Beautiful Benchmark for Computing Power
  • How I Built an AI Portal for Document Q and A, Summarization, Transcription, Translation, and Extraction
  • Operationalizing Data Quality in Cloud ETL Workflows: Automated Validation and Anomaly Detection
  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

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
  • [email protected]

Let's be friends: