
Using HAProxy with multiple ports on front- and backend
HAProxy is an awesome tool. It helps distribute traffic across multiple servers. But what if you need multiple ports on both frontend and backend? Don’t worry! It’s easier than you think.
Why Use Multiple Ports?
Sometimes, one port isn’t enough. Maybe you have different services running on different ports. Or you need to serve both HTTP and HTTPS. HAProxy can handle it all!
Setting Up the Frontend
The frontend is where HAProxy listens for incoming connections. You can configure it to listen on multiple ports.
Here’s a simple example:
frontend my_frontend
bind *:80
bind *:443
mode http
default_backend my_backend
This makes HAProxy listen on both port 80 (HTTP) and 443 (HTTPS).
Adding More Frontend Rules
You might need to route different ports to different backends. No problem! Just add ACLs.
frontend multi_port_frontend
bind *:8080
bind *:8443
mode http
acl is_api path_beg /api
use_backend api_backend if is_api
default_backend web_backend
Now, requests going to /api are sent to api_backend, while everything else goes to web_backend.

Setting Up the Backend
Backends define which servers handle requests. Each backend can have multiple servers and ports.
Here’s how to set one up:
backend my_backend
server server1 192.168.1.10:8080
server server2 192.168.1.11:8081
This means:
- Requests are balanced between server1 and server2.
- Server1 listens on port 8080.
- Server2 listens on port 8081.
Mixing Multiple Ports in Backends
What if your backend services run on different ports?
Here’s how to handle that:
backend multi_service_backend
server app1 192.168.1.20:9000
server app2 192.168.1.21:9100
Now HAProxy knows where to send the traffic.

Handling SSL Traffic
If you’re using both HTTP and HTTPS, you’ll want HAProxy to handle SSL connections.
Modify your frontend like this:
frontend ssl_frontend
bind *:443 ssl crt /etc/haproxy/certs/server.pem
mode http
default_backend secure_backend
HAProxy now terminates SSL and forwards traffic securely.
Final Thoughts
Using HAProxy with multiple ports on frontend and backend is simple. Just:
- Define multiple bind statements in your frontend.
- Use ACLs to route traffic based on conditions.
- Point backends to servers with the correct ports.
With these steps, you can manage different services, balance load, and keep traffic flowing smoothly.
Happy load balancing!