Layer 4 (LVS)
Load balance FTP (active mode) using LVS and NAT.
This page applies to:
- HAProxy ALOHA - all versions
In active mode FTP, the FTP server responds to client requests by initiating a connection back to the client.
Prefer passive FTP
Active FTP is not recommended because it can fail when the FTP client is behind a NAT router or firewall, which prevents the FTP server from establishing the connection with the client. To ensure that FTP service can be provided for clients in such networks, use passive FTP instead. The passive FTP solution also provides superior logging than the one described on this page.
Resource usage
NAT load balancing introduces:
- a limit on the number of simultaneous connections to a backend server. Only 65534 source ports are available for HAProxy ALOHA addresses.
- a heavy load on HAProxy ALOHA (connection tracking must be enabled).
To load balance active FTP services, use Linux Virtual Server (LVS) via the LB Layer4 tab to perform the load balancing at layer 4. In this scenario, we define a NAT rule so that responses from servers return through HAProxy ALOHA (that is, not Direct Server Return).
Example network architecture Jump to heading
The procedure in this section is based on an example network architecture where clients access the FTP services at the load balancer IP address 192.168.0.100. The load balancer then directs traffic to FTP servers at 192.168.1.10 and 192.168.1.11.

Enable FTP load balancing Jump to heading
To enable HAProxy ALOHA to load balance FTP at layer 4:
-
Go to the NAT tab and click Insert to add a NAT rule. In the New Rule area, fill in the fields as described below to create a NAT rule that changes the client’s source IP to the HAProxy ALOHA appliance’s VIP.
Field Description IN Inbound network interface. This indicates the interface of your VIP, where you receive traffic from clients. OUT Outbound network interface. If your appliance has only one network interface, set this to the same as the inbound interface. If your appliance has two interfaces and one of those interfaces is in a network containing the backend servers, use that interface for OUT. Protocol TCP. Before - Source Leave blank or set to any, which means match any source IP address.Before - Source port Leave blank or set to any, which means match any source port. Source ports are random.Before - Destination The VIP address that clients connect to, to match traffic destined for that address. Before - Destination port The FTP port or range. For example, set the port range 20-21. After - Source The VIP address to set as the source address on incoming packets. We recommend using a VIP instead of a physical IP address so that it’s highly available between HAProxy ALOHA appliances. When you have two network interfaces, you can create a new VIP for the private network containing your real servers, to better differentiate client-facing and server-facing traffic by having two, distinct VIPs. In contrast, a physical IP address can’t be shared between the members of a cluster. After - Source port Leave blank or set to same, since we won’t change the source port.After - Destination Leave blank or set to same, since we won’t change the destination IP address via the NAT tab. Destination NAT is handled by LVS.After - Destination port Leave blank or set to same, since we won’t change the destination port.Info
In configurations having two network interfaces, if you have failover configured on one VIP, configure a VIP with failover on the other interface as well.
-
Check your configuration.
Here’s an example NAT rule when you have one network interface.

-
Click Add and Apply.
-
Configure LVS on the LB Layer4 tab so that it load balances the FTP traffic between the real FTP servers. LVS performs destination NAT. Update the
serverlines in this example snippet to use the IP addresses of your servers. Notice that we aren’t setting an IP address on thedirectorline, since we’ll define a flow that sends traffic to this director; it doesn’t need to listen for traffic on its own.haproxydirector d_ftpmode natbalance sourceoption icmpcheck interval 10server ftp1 <FTP real server 1 IP> weight 10 checkserver ftp2 <FTP real server 2 IP> weight 10 checkhaproxydirector d_ftpmode natbalance sourceoption icmpcheck interval 10server ftp1 <FTP real server 1 IP> weight 10 checkserver ftp2 <FTP real server 2 IP> weight 10 checkHere, the FTP real servers are located at
192.168.1.10and192.168.1.11:haproxydirector d_ftpmode natbalance sourceoption icmpcheck interval 10server ftp1 192.168.1.10 weight 10 checkserver ftp2 192.168.1.11 weight 10 checkhaproxydirector d_ftpmode natbalance sourceoption icmpcheck interval 10server ftp1 192.168.1.10 weight 10 checkserver ftp2 192.168.1.11 weight 10 check -
Click OK and then Apply.
-
Click the Flows tab to configure a flow that captures traffic sent to the VIP and sends it to the LVS director
d_ftp. We do this to capture traffic destined for ports 20 and 21.Add this flow definition:
haproxyflow f_ftp director d_ftpmatch proto tcp dst <VIP> dstport 20:21haproxyflow f_ftp director d_ftpmatch proto tcp dst <VIP> dstport 20:21In this example, the IP address for FTP access is
192.168.0.100.haproxyflow f_ftp director d_ftpmatch proto tcp dst 192.168.0.100 dstport 20:21haproxyflow f_ftp director d_ftpmatch proto tcp dst 192.168.0.100 dstport 20:21Info
Make sure ports 20 and 21 are open on the FTP servers.
-
Click OK and then Apply.
-
To configure the flow manager service for automatic startup, go to the Services tab, locate the flowmgr service, and click Setup.
-
If the configuration contains the line
no autostart, delete the line to ensure that the flow manager service starts automatically on boot. -
Click OK and then Close.
-
Locate the flowmgr service and click Restart.
-
To ensure that packets from the same client flow to the same backend server during a session, enable LVS connection tracking. Click the Services tab, locate the lvs service, and click Setup.
-
If the configuration contains the line
no autostart, delete the line to ensure that the LVS service starts automatically on boot. -
Enable connection tracking by adding the
conntrackkeyword.textservice lvs############ Linux Virtual Server, layer 3/4 load balancingconntracktextservice lvs############ Linux Virtual Server, layer 3/4 load balancingconntrack -
Click OK and then Close.
-
Locate the lvs service and click Restart.
-
To make the changes persist after a reboot, go to the Setup tab and click Save within the Configuration section.