{{ ansible_managed | comment }}

{% if item.value.extra_directives is defined and item.value.extra_directives | length -%}

{% for extra_directive in item.value.extra_directives %}
{{ extra_directive }}
{% endfor %}

{% endif -%}

{% if item.value.upstreams is defined %}
{% for upstream in item.value.upstreams %}
upstream {{ upstream.name }} {
    {{ upstream.lb_method if upstream.lb_method is defined else load_balancer_service_lb_method }};
    zone {{ upstream.zone if upstream.zone is defined else upstream.name }} {{ upstream.zone_size if upstream.zone_size is defined else load_balancer_service_stream_zone_size }};
{% for server in upstream.servers %}
    server {{ server.address }}{{":" + server.port | string }} {% if server.down is defined and server.down %}down{% else %}weight={{ server.weight | default("1") }}{% endif %}{% if server.options is defined and server.options %} {{ server.options }}{% endif %};
{% endfor %}
{% if upstream.sticky_cookie is defined and upstream.sticky_cookie %}
    sticky cookie srv_id expires=1h  path=/;
{% endif %}
{% if upstream.custom_options is defined and upstream.custom_options | length %}
{% for inline_option in upstream.custom_options %}
    {{ inline_option }}
{% endfor %}
{% endif %}
}
{% endfor %}
{% endif %}
{% for server in item.value.servers %}

server {
{% if server.port is defined %}
{% set listen_port = server.port %}
{% else  %}
{% set listen_port = item.value.port %}
{% endif %}
{% if server.tls_certificate is defined %}
{% set tls_certificate = server.tls_certificate %}
{% set tls_certificate_key = server.tls_certificate_key %}
{% elif item.value.tls_certificate is defined %}
{% set tls_certificate = item.value.tls_certificate %}
{% set tls_certificate_key = item.value.tls_certificate_key %}
{% endif %}
{% if server.custom_listen_vip is defined %}
    listen {{ server.custom_listen_vip }}:{{ listen_port }}{% if tls_certificate is defined %} ssl{% endif %};
{% else %}
    listen {{ load_balancer_service_vip }}:{{ listen_port }}{% if tls_certificate is defined %} ssl{% endif %};
    listen {{ load_balancer_service_interface_ip_info.split('/')[0] }}:{{ listen_port }}{% if tls_certificate is defined %} ssl{% endif %};
{% endif %}
{% if tls_certificate is defined %}
    ssl_certificate {{ tls_certificate }};
    ssl_certificate_key {{ tls_certificate_key }};
{% if server.tls_protocols is defined %}
{% set tls_protocols = server.tls_protocols %}
{% elif item.value.tls_protocols is defined %}
{% set tls_protocols = item.value.tls_protocols %}
{% else %}
{% set tls_protocols = load_balancer_service_tls.protocols %}
{% endif %}
    ssl_protocols {{ tls_protocols }};
{% if server.tls_ciphers is defined %}
{% set tls_ciphers = server.tls_ciphers %}
{% elif item.value.tls_ciphers is defined %}
{% set tls_ciphers = item.value.tls_ciphers %}
{% else %}
{% set tls_ciphers = load_balancer_service_tls.ciphers %}
{% endif %}
    ssl_ciphers {{ tls_ciphers }};
{% endif %}

{% if server.extra_directives is defined and server.extra_directives | length -%}

{% for extra_directive in server.extra_directives %}
    {{ extra_directive }}
{% endfor %}

{% endif -%}

{% if server.proxy_pass is defined %}
    proxy_pass {{ server.proxy_pass }};
{% else %}
    proxy_pass {{ item.value.upstreams[0].name }};
{% endif %}
{% if server.proxy_connect_timeout is defined %}
    proxy_connect_timeout {{ server.proxy_connect_timeout }};
{% elif item.value.proxy_connect_timeout is defined %}
    proxy_connect_timeout {{ item.value.proxy_connect_timeout }};
{% endif %}
{% if server.proxy_timeout is defined %}
    proxy_timeout {{ server.proxy_timeout }};
{% elif item.value.proxy_timeout is defined %}
    proxy_timeout {{ item.value.proxy_timeout }};
{% endif %}
{% if server.proxy_protocol is defined %}
    proxy_protocol {{ server.proxy_protocol }};
{% elif item.value.proxy_protocol is defined %}
    proxy_protocol {{ item.value.proxy_protocol }};
{% else %}
    proxy_protocol off;
{% endif %}
{% if server.access_log is defined %}
{% for access_log in server.access_log %}
    access_log {{ access_log.location }}{{ access_log.name }} {{ access_log.type}};
{% endfor %}
{% else %}
    access_log {{ load_balancer_service_log_directory }}/{{ server.name }}-{{ listen_port }}_access.log log_format_stream_reverse_proxy;
{% endif %}
}
{% endfor %}
