{% from 'states/nginx/map.jinja' import nginx as nginx_map with context %}

proxy_set_header Host              $host;
proxy_set_header X-Real-IP         $remote_addr;
proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Scheme          $scheme;

proxy_buffering on;

{% for target in upstream %}
server {
{% if target.protocols is defined %}
{% set proto = target.protocols %}
{% else %}
{% set proto = protocol %}
{% endif %}
{% if 'http' in proto %}
    listen {{ nginx_map.ports.http }};
{% if ipv6 %}
    listen [::]:{{ nginx_map.ports.http }};
{% endif %}
{% endif %}
{% if 'https' in proto %}
    listen {{ nginx_map.ports.https }} ssl;
{% if ipv6 %}
    listen [::]:{{ nginx_map.ports.https }} ssl;
{% endif %}
{% endif %}

    server_name {{ target.servername }};

    location / {
        proxy_pass {{ target.url }};
    }
{% if acme_backend %}
    location /.well-known/acme-challenge/ {
        proxy_pass http://{{ acme_backend.ip }}/acme/$host$uri;
    }
{% endif %}
}

{% endfor %}

{% from 'states/nginx/map.jinja' import nginx as nginx_map with context %}

proxy_set_header Host              $host;
proxy_set_header X-Real-IP         $remote_addr;
proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Scheme          $scheme;

proxy_buffering on;

{% macro listen(target) %}
{% if target.protocols is defined %}
{% set proto = target.protocols %}
{% else %}
{% set proto = protocol %}
{% endif %}
{% if 'http' in proto %}
listen {{ nginx_map.ports.http }};
{% if ipv6 %}
listen [::]:{{ nginx_map.ports.http }};
{% endif %}
{% endif %}
{% if 'https' in proto %}
listen {{ nginx_map.ports.https }} ssl;
{% if ipv6 %}
listen [::]:{{ nginx_map.ports.https }} ssl;
{% endif %}
{% endif %}
{% endmacro %}

{% for target in upstream %}
server {
    {{ listen(target)|indent }}
    server_name {{ target.servername }};

    location / {
        proxy_pass {{ target.url }};
    }
{% if acme_backend %}
    location /.well-known/acme-challenge/ {
        proxy_pass http://{{ acme_backend.ip }}/acme/$host$uri;
    }
{% endif %}
}

{% if target.get('redirect_from', None) is not none %}
server {
    {{ listen(target)|indent }}
{% for server in target['redirect_from'] %}
    server_name {{ server }};
{% endfor %}
    return 301 $scheme://{{ target.servername }}$request_uri;
}
{% endif %}
{% endfor %}

Add redirects for reverse proxies.
nginx: Add redirects for reverse proxies.
