Core concepts

Defaults

A defaults section stores common settings which will be inherited by frontend and backend sections that follow it. It offers a way to 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 go into a defaults. For example, bind and server lines cannot.

haproxy
defaults
mode http
balance roundrobin
# Inherits mode
frontend website
bind :80
default_backend web_servers
# Inherits mode and balance
backend web_servers
server s1 192.168.1.25:80
server s2 192.168.1.26:80
haproxy
defaults
mode http
balance roundrobin
# Inherits mode
frontend website
bind :80
default_backend web_servers
# Inherits mode and balance
backend web_servers
server s1 192.168.1.25:80
server 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 does not inherit settings from a 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.

haproxy
defaults
mode http
balance roundrobin
# Inherits mode
frontend website
bind :80
default_backend web_servers
# Inherits mode and balance
backend web_servers
server s1 192.168.1.25:80
server s2 192.168.1.26:80
# Overrides mode
frontend mysql
mode tcp
bind :3306
default_backend mysql_servers
# Overrides mode and balance
backend mysql_servers
mode tcp
balance leastconn
server db1 192.168.1.29:3306
server db2 192.168.1.30:3306
haproxy
defaults
mode http
balance roundrobin
# Inherits mode
frontend website
bind :80
default_backend web_servers
# Inherits mode and balance
backend web_servers
server s1 192.168.1.25:80
server s2 192.168.1.26:80
# Overrides mode
frontend mysql
mode tcp
bind :3306
default_backend mysql_servers
# Overrides mode and balance
backend mysql_servers
mode tcp
balance leastconn
server db1 192.168.1.29:3306
server 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.

haproxy
defaults http_defaults
mode http
defaults tcp_defaults
mode tcp
# Inherits from http_defaults
frontend website from http_defaults
bind :80
default_backend web_servers
# Inherits from tcp_defaults
frontend mysql from tcp_defaults
mode tcp
bind :3306
default_backend mysql_servers
haproxy
defaults http_defaults
mode http
defaults tcp_defaults
mode tcp
# Inherits from http_defaults
frontend website from http_defaults
bind :80
default_backend web_servers
# Inherits from tcp_defaults
frontend mysql from tcp_defaults
mode tcp
bind :3306
default_backend mysql_servers

If a frontend or backend does not 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 manner is not recommended, however, 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?