backend-haproxy.cfg.in 4.86 KB
Newer Older
1 2 3 4 5
global
  pidfile {{ configuration['pid-file'] }}
  # master-worker is compatible with foreground with process management
  master-worker

6
log {{ configuration['log-socket'] }} local0
7 8 9 10 11 12 13 14 15 16
defaults
  log global
  mode http
  option httplog
  timeout queue 60s
  timeout server {{ configuration['request-timeout'] }}s
  timeout client {{ configuration['request-timeout'] }}s
  timeout connect {{ configuration['backend-connect-timeout'] }}s
  retries {{ configuration['backend-connect-retries'] }}

17
{%- set SCHEME_PREFIX_MAPPING = { 'http': 'http_backend', 'https': 'https_backend'} %}
18 19
{%- macro frontend_entry(slave_instance, scheme, wildcard) %}
{#-   wildcard switch allows to put dangerous entries in the end, as haproxy parses with first match #}
20 21 22 23 24 25 26 27 28 29 30
{%-   if slave_instance[SCHEME_PREFIX_MAPPING[scheme]]['hostname'] and slave_instance[SCHEME_PREFIX_MAPPING[scheme]]['port'] %}
{%-     set host_list = (slave_instance.get('server-alias') or  '').split() %}
{%-     if slave_instance.get('custom_domain') not in host_list %}
{%-       do host_list.append(slave_instance.get('custom_domain')) %}
{%-     endif %}
{%-     set matched = {'count': 0} %}
{%-     for host in host_list %}
{#-       Match up to the end or optional port (starting with ':') #}
{#-       Please note that this matching is quite sensitive to changes and hard to test, so avoid needless changes #}
{%-       if wildcard and host.startswith('*.') %}
{%-         do matched.__setitem__('count', matched['count'] + 1) %}
31 32
# match wildcard {{ host }}
  acl is_{{ slave_instance['slave_reference'] }} hdr_reg(host) -i {{ host[2:] }}($|:.*)
33 34
{%-       elif not wildcard and not host.startswith('*.') %}
{%-         do matched.__setitem__('count', matched['count'] + 1) %}
35
  acl is_{{ slave_instance['slave_reference'] }} hdr_reg(host) -i ^{{ host }}($|:.*)
36 37 38
{%-       endif %}
{%-     endfor %}
{%-     if matched['count'] > 0 %}
39
  use_backend {{ slave_instance['slave_reference'] }}-{{ scheme }} if is_{{ slave_instance['slave_reference'] }}
40
{%-     endif %}
41
{%-   endif %}
42 43
{%- endmacro %}

44 45 46 47 48 49 50 51 52
# statistic
frontend statistic
  bind {{ configuration['global-ipv6']}}:{{ configuration['statistic-port'] }} ssl crt {{ configuration['statistic-certificate'] }}
  stats enable
  stats uri /
  stats show-desc {{ configuration['statistic-identification'] }}
  stats auth {{ configuration['statistic-username'] }}:{{ configuration['statistic-password'] }}
  stats realm {{ configuration['statistic-identification'] }}

53 54
frontend http-backend
  bind {{ configuration['local-ipv4'] }}:{{ configuration['http-port'] }}
55
{%- for slave_instance in backend_slave_list -%}
56 57
{{ frontend_entry(slave_instance, 'http', False) }}
{%- endfor %}
58
{%- for slave_instance in backend_slave_list -%}
59
{{ frontend_entry(slave_instance, 'http', True) }}
60 61 62 63
{%- endfor %}

frontend https-backend
  bind {{ configuration['local-ipv4'] }}:{{ configuration['https-port'] }}
64
{%- for slave_instance in backend_slave_list -%}
65 66
{{ frontend_entry(slave_instance, 'https', False) }}
{%- endfor %}
67
{%- for slave_instance in backend_slave_list -%}
68
{{ frontend_entry(slave_instance, 'https', True) }}
69 70 71
{%- endfor %}

{%- for slave_instance in backend_slave_list %}
72
{%-   for (scheme, prefix) in SCHEME_PREFIX_MAPPING.items() %}
73
{%-     set info_dict = slave_instance[prefix] %}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
{%-     if info_dict['hostname'] and info_dict['port'] %}
{%-       set ssl_list = [] %}
{%-       if info_dict['scheme'] == 'https' %}
{%-         if slave_instance['authenticate-to-backend'] %}
{%-           do ssl_list.append('crt %s' % (configuration['certificate'],)) %}
{%-         endif %}
{%-         do ssl_list.append('ssl verify') %}
{%-         set path_to_ssl_proxy_ca_crt = slave_instance.get('path_to_ssl_proxy_ca_crt') %}
{%-         if slave_instance['ssl_proxy_verify'] %}
{%-           if path_to_ssl_proxy_ca_crt %}
{%-             do ssl_list.append('required ca-file %s' % (path_to_ssl_proxy_ca_crt,)) %}
{%-           else %}
{#-           Backend SSL shall be verified, but not CA provided, disallow connection #}
{#-           Simply dropping hostname from the dict will result with ignoring it... #}
{%-           do info_dict.__setitem__('hostname', '') %}
{%-           endif %}
90
{%-         else %}
91
{%-           do ssl_list.append('none') %}
92 93 94 95
{%-         endif %}
{%-       endif %}

backend {{ slave_instance['slave_reference'] }}-{{ scheme }}
96 97 98 99
{%-       set hostname = info_dict['hostname'] %}
{%-       set port = info_dict['port'] %}
{%-       set path = info_dict['path'].rstrip('/') %}
{%-       if hostname and port %}
100 101 102
  timeout server {{ slave_instance['request-timeout'] }}s
  timeout connect {{ slave_instance['backend-connect-timeout'] }}s
  retries {{ slave_instance['backend-connect-retries'] }}
103
  server {{ slave_instance['slave_reference'] }}-backend {{ hostname }}:{{ port }} {{ ' '.join(ssl_list) }}
104
{%-         if path %}
105
  http-request set-path {{ path }}%[path]
106
{%-         endif %}
107 108 109 110
{%-       endif %}
{%-     endif %}
{%-   endfor %}
{%- endfor %}