{% if item.type == 'ssl_preread' %}
# Ansible Managed: DO NOT EDIT
# Конфигурация для {{ item.stream }} (SSL preread)

{% if item.ssl_preread is defined and item.ssl_preread.server_names is defined %}
map $ssl_preread_server_name $name {
{% for mapping in item.ssl_preread.server_names %}
{% if mapping is string %}
    {{ mapping }};
{% else %}
{% for hostname, target in mapping.items() %}
    {{ hostname }} {{ target }};
{% endfor %}
{% endif %}
{% endfor %}
    default {{ item.ssl_preread.default_target | default('https') }};
}
{% endif %}

{% if item.upstreams is defined %}
{% for upstream in item.upstreams %}
upstream {{ upstream.name }} {
{% for server in upstream.servers %}
    server {{ server }};
{% endfor %}
}
{% endfor %}
{% endif %}

server {
    listen {{ item.listen_port }};
{% if item.ssl_preread is defined and item.ssl_preread.ssl_preread | default(false) %}
    ssl_preread on;
{% endif %}
{% if item.ssl_preread is defined and item.ssl_preread.proxy_protocol | default(false) %}
    proxy_protocol on;
{% endif %}
    proxy_pass $name;
{% if item.responses is defined %}
    proxy_responses {{ item.responses }};
{% endif %}
{% if item.timeout is defined %}
    proxy_timeout {{ item.timeout }};
{% endif %}
{% if item.connect_timeout is defined %}
    proxy_connect_timeout {{ item.connect_timeout }};
{% endif %}
}
{% else %}
# Ansible Managed: DO NOT EDIT
# Конфигурация для {{ item.stream }}

upstream {{ item.stream }}_backend {
#    hash $remote_addr;   
{% for server in item.backends %}
    server {{ server }} max_fails={{ max_fails | default(0) }};
{% endfor %}
}

server {
    listen {{ item.listen_port }}{% if item.protocol == 'udp' %} udp reuseport{% endif %};
    proxy_pass {{ item.stream }}_backend;
{% if item.transparent | default(false) %}
    proxy_bind $remote_addr transparent;
{% endif %}
{% if item.responses is defined %}
    proxy_responses {{ item.responses }};
{% endif %}
{% if item.timeout is defined %}
    proxy_timeout {{ item.timeout }};
{% endif %}
{% if item.connect_timeout is defined %}
    proxy_connect_timeout {{ item.connect_timeout }};
{% endif %}
}
{% endif %}
