On this page
- What does HAProxy do on a single VPS?
- How do you install HAProxy?
- How is haproxy.cfg structured?
- How do you get a certificate while HAProxy owns port 80?
- How do you terminate TLS and route to backends?
- How do you secure the firewall and backends?
- How do you watch HAProxy with the stats page?
- How do you reload safely and troubleshoot?
- Questions
What does HAProxy do on a single VPS?
HAProxy accepts client connections, ends TLS, and forwards each request to a backend that you choose by host name, path or any header. On one server it is a fast TLS front door for apps that listen on local ports. Across several of your servers it spreads load and pulls failed nodes out of rotation after health checks.
Because BitcoinVPS has no load balancer product or private network, you build this yourself: HAProxy runs on one VPS with a public address, and the backends are either local ports or other servers reached over their public IPs. When backends are remote, lock them down so only the HAProxy server can reach the app port, as shown below.
How do you install HAProxy?
The distribution packages are stable long-term branches and receive security updates.
# Ubuntu 24.04 / 22.04, Debian 12 / 13
sudo apt update
sudo apt install -y haproxy
# Rocky Linux 9, AlmaLinux 9
sudo dnf install -y haproxy
sudo systemctl enable --now haproxy
haproxy -vVersion numbers differ between releases: Ubuntu 24.04 ships the 2.8 branch, while older systems carry older branches. All of them support the configuration in this guide. If you want a newer LTS branch, the HAProxy community maintains packages for Debian and Ubuntu at haproxy.debian.net; pick your system there and follow its instructions.
How is haproxy.cfg structured?
The file in /etc/haproxy/haproxy.cfg has four kinds of sections. global holds process settings, defaults sets values that later sections inherit, frontend describes what HAProxy listens on, and backend lists the servers it forwards to. Keep the packaged global and defaults blocks and append your own below them. These defaults are sensible for HTTP:
defaults
mode http
log global
option httplog
option forwardfor
timeout connect 5s
timeout client 60s
timeout server 60soption forwardfor adds an X-Forwarded-For header so the app sees the visitor address instead of the proxy.
How do you get a certificate while HAProxy owns port 80?
Let Certbot answer the challenge on a local side port and tell HAProxy to send the challenge path there. First add an HTTP frontend that only does that and redirects everything else:
frontend web_http
bind :80
bind :::80
acl acme path_beg /.well-known/acme-challenge/
use_backend certbot if acme
http-request redirect scheme https code 301 unless acme
backend certbot
server local 127.0.0.1:8899Validate, reload and request the certificate:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg && sudo systemctl reload haproxy
sudo apt install -y certbot # or: sudo dnf install -y epel-release certbot
sudo certbot certonly --standalone --http-01-port 8899 -d example.net -d www.example.netHAProxy wants the chain and key in one file. A deploy hook builds it now and after every renewal. Save this as /etc/letsencrypt/renewal-hooks/deploy/haproxy.sh and make it executable with chmod +x:
#!/bin/sh
mkdir -p /etc/haproxy/certs
name=$(basename "$RENEWED_LINEAGE")
cat "$RENEWED_LINEAGE/fullchain.pem" "$RENEWED_LINEAGE/privkey.pem" > "/etc/haproxy/certs/$name.pem"
chmod 600 "/etc/haproxy/certs/$name.pem"
systemctl reload haproxyRun it once by hand with sudo RENEWED_LINEAGE=/etc/letsencrypt/live/example.net sh /etc/letsencrypt/renewal-hooks/deploy/haproxy.sh. The Certbot guide explains the challenge types in more depth.
How do you terminate TLS and route to backends?
Add an HTTPS frontend that loads every PEM in the certificate folder and picks a backend by host name. This example sends the main site to an app on a local port and an API name to two of your servers:
frontend web_https
bind :443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1
bind :::443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1
http-request set-header X-Forwarded-Proto https
http-response set-header Strict-Transport-Security "max-age=31536000"
use_backend api if { req.hdr(host) -i api.example.net }
default_backend site
backend site
server app1 127.0.0.1:3000 check
backend api
balance leastconn
option httpchk GET /health
http-check expect status 200
server api1 203.0.113.10:8080 check
server api2 203.0.113.11:8080 checkcheck turns on health checks; a server that fails is skipped until it recovers. balance accepts roundrobin (the default), leastconn for long requests, or source to keep a client on the same node. The same ideas with Nginx are covered in load balancing with Nginx.
How do you secure the firewall and backends?
Open only 80 and 443 on the HAProxy server, and on each remote backend accept the app port only from the proxy address 198.51.100.24.
# HAProxy server, Ubuntu and Debian
sudo ufw allow 80,443/tcp
# HAProxy server, Rocky and Alma
sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload
# each backend, Ubuntu and Debian
sudo ufw allow from 198.51.100.24 to any port 8080 proto tcp
# each backend, Rocky and Alma
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.24" port port="8080" protocol="tcp" accept'
sudo firewall-cmd --reloadTraffic between the proxy and remote backends crosses the public internet, so use HTTPS to the backends (server api1 203.0.113.10:8443 check ssl verify required ca-file /etc/ssl/certs/ca-certificates.crt) or a WireGuard tunnel from the WireGuard guide. On Rocky and Alma, SELinux blocks HAProxy from connecting to unusual ports until you run sudo setsebool -P haproxy_connect_any 1.
How do you watch HAProxy with the stats page?
Bind the stats page to localhost and view it through an SSH tunnel:
frontend stats
bind 127.0.0.1:8404
stats enable
stats uri /stats
stats refresh 10sRun ssh -L 8404:127.0.0.1:8404 deploy@198.51.100.24 on your computer and open http://localhost:8404/stats. Each server shows as green or red with its check result, session count and bytes. Request logs go through syslog: on Ubuntu and Debian the package writes them to /var/log/haproxy.log, and on Rocky and Alma you read them with sudo journalctl -u haproxy.
How do you reload safely and troubleshoot?
Always validate before reloading. A reload starts new processes with the new file and lets old ones finish their connections, so visitors do not notice.
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl reload haproxy
sudo ss -ltnp | grep haproxy- 503 Service Unavailable: every server in the backend failed its health check. Look at the stats page and test the backend directly with
curl. - Startup fails on the crt line: the certificate folder is empty or a PEM lacks its key.
- Certbot renewal fails: confirm the ACME rule is in the port 80 frontend and that nothing else uses port 8899.
- App sees the proxy IP: read
X-Forwarded-Forin the app or configure its trusted proxy setting.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about HAProxy load balancer and TLS proxy
Does BitcoinVPS offer a managed load balancer?
No. You run HAProxy or Nginx yourself on a VPS and point it at your other servers over their public IPs.
Why does HAProxy need a combined PEM file?
HAProxy reads the certificate chain and private key from one file per name. The deploy hook in this guide creates that file after every renewal.
How do I keep users on the same backend?
Use balance source, or add a cookie with cookie SRV insert indirect nocache in the backend and a cookie value on each server line.
Can HAProxy proxy raw TCP such as a database?
Yes. Set mode tcp in the frontend and backend. Header rules then no longer apply, but health checks and balancing still work.
How do I see which version I run?
Run haproxy -v. Configuration keywords can differ slightly between branches, so check the documentation for your version.
More in Web servers and proxies
- Install and configure Nginx
- Install and configure Apache
- OpenLiteSpeed web server
- Load balancing with Nginx upstreams
- Nginx as a reverse proxy for an app
- Apache as a reverse proxy
- Compression with gzip and Brotli
- HTTPS and www redirects
- Turn off Apache directory listings
- Nginx Proxy Manager with Docker
- Nginx UI web interface
- Traefik for Docker containers
- Apache Tomcat for Java apps
- Encrypted FTP with vsftpd (FTPS)
- Varnish cache in front of a site