{{ 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 }};
{% 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") }} {{ server.health_check | default("") }}{% 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 %}
{% 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 and server.no_tls is not defined %}
{% set tls_certificate = item.value.tls_certificate %}
{% set tls_certificate_key = item.value.tls_certificate_key %}
{% endif %}
{% if listen_port == 443 and server.create_http_to_https_redirect is sameas true or item.value.create_http_to_https_redirect is sameas true %}
{% if server.create_http_to_https_redirect is not sameas false %}

server {
    server_name {{ server.name }}{% if server.additional_server_names is defined %} {{ server.additional_server_names }}{% endif %};
    listen {{ load_balancer_service_interface_ip_info.split('/')[0] }}:80;
    listen {{ load_balancer_service_vip }}:80;

    return 301 https://{{ server.name }}$request_uri;

    access_log {{ load_balancer_service_log_directory }}/{{ server.name }}-80_redirect_access.log main;
    error_log {{ load_balancer_service_log_directory }}/{{ server.name }}-80_redirect_error.log warn;
}
{% endif %}
{% endif %}

server {
    server_name {{ server.name }}{% if server.additional_server_names is defined %} {{ server.additional_server_names }}{% endif %};
    listen {{ load_balancer_service_interface_ip_info.split('/')[0] }}:{{ listen_port }}{% if tls_certificate is defined %} ssl{% endif %}{% if server.listen_options is defined %} {{ server.listen_options }}{% endif %};
    listen {{ load_balancer_service_vip }}:{{ listen_port }}{% if tls_certificate is defined %} ssl{% endif %}{% if server.listen_options is defined %} {{ server.listen_options }}{% endif %};
{% if server.return is defined %}
    return {{ server.return }};
{% 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.locations is defined %}
{% for location in server.locations %}
    location {{ location.location }} {
{% if location.proxy_pass is defined %}
        proxy_pass {{ location.proxy_pass }};
{% elif item.value.upstreams is defined %}
        proxy_pass {{ 'https' if tls_certificate is defined else 'http'}}://{{ item.value.upstreams[0].name }}/;
{% endif %}
{% if location.allows is defined %}
{% for allow in location.allows %}
        allow {{ allow }};
{% endfor %}
{% endif %}
{% if location.denies is defined %}
{% for deny in location.denies %}
        deny {{ deny }};
{% endfor %}
{% endif %}
{% if location.proxy_set_header is defined %}
{% for header in location.proxy_set_header %}
        proxy_set_header {{ header.name }} {{ header.value }};
{% endfor %}
{% else %}
{% for header in load_balancer_service_proxy_set_header.values() %}
        proxy_set_header {{ header.name }} {{ header.value }};
{% endfor %}
{% if location.additional_proxy_set_headers is defined %}
{% for key, value in location.additional_proxy_set_headers.items() %}
        proxy_set_header {{ key }} {{ value }};
{% endfor %}
{% endif %}

{% if location.additional_directives is defined %}
{% for key, value in location.additional_directives.items() %}
        {{ key }} {{ value }};
{% endfor %}
{% endif %}
{% endif %}
    }

{% endfor %}
{% else %}
    location / {
{% if server.proxy_pass is defined %}
        proxy_pass {{ server.proxy_pass }};
{% elif item.value.upstreams is defined %}
        proxy_pass {{ 'https' if tls_certificate is defined else 'http'}}://{{ item.value.upstreams[0].name }}/;
{% endif %}
{% if server.allows is defined %}
{% for allow in server.allows %}
        allow {{ allow }};
{% endfor %}
{% elif item.value.allows is defined %}
{% for allow in item.allows %}
        allow {{ allow }};
{% endfor %}
{% endif %}
{% if server.denies is defined %}
{% for deny in server.denies %}
        deny {{ deny }};
{% endfor %}
{% elif item.value.denies is defined %}
{% for deny in item.value.denies %}
        deny {{ deny }};
{% endfor %}
{% 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_read_timeout is defined %}
        proxy_read_timeout {{ server.proxy_read_timeout }};
{% elif item.value.proxy_read_timeout is defined %}
        proxy_read_timeout {{ item.value.proxy_read_timeout }};
{% endif %}
{% if server.proxy_send_timeout is defined %}
        proxy_send_timeout {{ server.proxy_send_timeout }};
{% elif item.value.proxy_send_timeout is defined %}
        proxy_send_timeout {{ item.value.proxy_send_timeout }};
{% endif %}
{% if server.proxy_set_header is defined %}
{% for header in server.proxy_set_header %}
        proxy_set_header {{ header.name }} {{ header.value }};
{% endfor %}
{% elif item.value.proxy_set_header is defined %}
{% for header in item.value.proxy_set_header %}
        proxy_set_header {{ header.name }} {{ header.value }};
{% endfor %}
{% else %}
{% for header in load_balancer_service_proxy_set_header.values() %}
        proxy_set_header {{ header.name }} {{ header.value }};
{% endfor %}
{% endif %}

{% if server.proxy_tls_protocols is defined %}
        proxy_ssl_protocols {{ server.proxy_tls_protocols }};
{% elif item.value.proxy_tls_protocols is defined %}
        proxy_ssl_protocols {{ item.value.proxy_tls_protocols }};
{% endif %}
{% if server.proxy_tls_ciphers is defined %}
        proxy_ssl_ciphers {{ server.proxy_tls_ciphers }};
{% elif item.value.proxy_tls_ciphers is defined %}
        proxy_ssl_ciphers {{ item.value.proxy_tls_ciphers }};
{% endif %}
    }
{% 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_http_reverse_proxy;
{% endif %}
{% if server.error_log is defined %}
{% for error_log in server.error_log %}
    error_log {{ error_log.location }}{{ error_log.name}} {{ error_log.level }};
{% endfor %}
{% else %}
    error_log {{ load_balancer_service_log_directory }}/{{ server.name }}-{{ listen_port }}_error.log warn;
{% endif %}
}
{% endfor %}
