Configuration basics
Defaults
A defaults section stores common settings inherited by the frontend and backend sections that follow it. It can also condense long configurations by reducing duplicated lines.
Defaults configuration example Jump to heading
By adding defaults to the configuration, you can define settings that all other sections below it will inherit when applicable. For instance, mode is applicable to both a frontend and a backend, but balance only applies to backends. Not all directives can be included in defaults; for example, the bind and server lines cannot.
haproxydefaultsmode httpbalance roundrobin# Inherits modefrontend websitebind :80default_backend web_servers# Inherits mode and balancebackend web_serversserver s1 192.168.1.25:80server s2 192.168.1.26:80
haproxydefaultsmode httpbalance roundrobin# Inherits modefrontend websitebind :80default_backend web_servers# Inherits mode and balancebackend web_serversserver s1 192.168.1.25:80server s2 192.168.1.26:80
You can define multiple defaults sections. Each one will apply to any frontend or backend that follows it, up until the next defaults section. In configurations containing multiple defaults sections, a defaults section doesn’t inherit settings from other defaults sections. Each defaults section conveys only the settings it directly specifies.
Override a defaults section Jump to heading
Each frontend and backend that follows a defaults section can still override a setting that was inherited. Building from the previous configuration sample, we have added a frontend and backend that load balance MySQL database instances. MySQL databases don’t communicate over HTTP and do better with least connections load balancing. Therefore, we override mode and balance in the mysql sections.
haproxydefaultsmode httpbalance roundrobin# Inherits modefrontend websitebind :80default_backend web_servers# Inherits mode and balancebackend web_serversserver s1 192.168.1.25:80server s2 192.168.1.26:80# Overrides modefrontend mysqlmode tcpbind :3306default_backend mysql_servers# Overrides mode and balancebackend mysql_serversmode tcpbalance leastconnserver db1 192.168.1.29:3306server db2 192.168.1.30:3306
haproxydefaultsmode httpbalance roundrobin# Inherits modefrontend websitebind :80default_backend web_servers# Inherits mode and balancebackend web_serversserver s1 192.168.1.25:80server s2 192.168.1.26:80# Overrides modefrontend mysqlmode tcpbind :3306default_backend mysql_servers# Overrides mode and balancebackend mysql_serversmode tcpbalance leastconnserver db1 192.168.1.29:3306server db2 192.168.1.30:3306
Define named defaults sections Jump to heading
Available since
- HAProxy 2.4
- HAProxy Enterprise 2.4r1
- HAProxy ALOHA 13.5
You can define more than one defaults section, each with a unique name. To apply a specific, named defaults to a frontend or backend, use the from keyword to specify the desired defaults section name.
Below, the website frontend takes its default settings from the defaults section named http_defaults. The mysql frontend takes its default settings from the defaults section named tcp_defaults.
haproxydefaults http_defaultsmode httpdefaults tcp_defaultsmode tcp# Inherits from http_defaultsfrontend website from http_defaultsbind :80default_backend web_servers# Inherits from tcp_defaultsfrontend mysql from tcp_defaultsmode tcpbind :3306default_backend mysql_servers
haproxydefaults http_defaultsmode httpdefaults tcp_defaultsmode tcp# Inherits from http_defaultsfrontend website from http_defaultsbind :80default_backend web_servers# Inherits from tcp_defaultsfrontend mysql from tcp_defaultsmode tcpbind :3306default_backend mysql_servers
If a frontend or backend doesn’t indicate a named defaults section using the from keyword, it takes the default values specified in the nearest preceding defaults section. Relying on section ordering in this way is not recommended because it can lead to ambiguity and slow troubleshooting. Where possible, specify a unique name for each defaults section and use the from keyword to indicate the defaults section to use.
See also Jump to heading
Do you have any suggestions on how we can improve the content of this page?