{% macro headers(debug=False, https=False) %}
add_header Vary Accept-Language;
{% if https %}
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
{% endif %}
{% if debug %}
    add_header Cache-Control "no-cache";
{% else %}
    add_header Cache-Control "max-age=86400";
{% endif %}
{% endmacro %}

{% if https %}
    server {
        listen 80;
        server_name {{ server_name }};
        return 301 https://$host$request_uri;
    }
{% endif %}
server {
    {{ headers(
        debug=app.project.configuration.debug,
        https=https
    ) }}
    {% if https %}
	    listen 443 ssl http2;
    {% else %}
	    listen 80;
    {% endif %}
	server_name {{ server_name }};
	root {{ www_directory_path }};
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_types text/css application/javascript application/json application/xml;

    {% if app.project.configuration.clean_urls %}
        set_by_lua_block $media_type_extension {
            local available_media_types = {'text/html', 'application/json'}
            local media_type_extensions = {}
            media_type_extensions['text/html'] = 'html'
            media_type_extensions['application/json'] = 'json'
            local media_type = require('content_negotiation').negotiate(ngx.req.get_headers()['Accept'], available_media_types)
            return media_type_extensions[media_type]
        }
    {% else %}
        set $media_type_extension html;
    {% endif %}
    index index.$media_type_extension;

    {% if app.project.configuration.locales.multilingual %}
        location @localized_redirect {
            {% if app.project.configuration.clean_urls %}
                set_by_lua_block $locale_alias {
                    local available_locales = {'{{ app.project.configuration.locales | join("', '") }}'}
                    local locale_aliases = {}
                    {% for locale_configuration in app.project.configuration.locales.values() %}
                        locale_aliases['{{ locale_configuration.locale }}'] = '{{ locale_configuration.alias }}'
                    {% endfor %}
                    local locale = require('content_negotiation').negotiate(ngx.req.get_headers()['Accept-Language'], available_locales)
                    return locale_aliases[locale]
                }
                {{ headers(
                    debug=app.project.configuration.debug,
                    https=https
                ) }}
                add_header Content-Language "$locale_alias" always;

                return 307 /$locale_alias$uri;
            {% else %}
                set $locale_alias {{ app.project.configuration.locales.default.alias }};
                return 301 /$locale_alias$uri;
            {% endif %}
        }

        # The front page.
        location = / {
            # nginx does not support redirecting to named locations, so we use try_files with an empty first
            # argument and assume that never matches a real file.
            try_files '' @localized_redirect;
        }

        # Localized resources.
        location ~* ^/({{ app.project.configuration.locales.values() | map(attribute='alias') | join('|') }})(/|$) {
            set $locale $1;

            {{ headers(
                debug=app.project.configuration.debug,
                https=https
            ) }}
            add_header Content-Language "$locale" always;

            # Handle HTTP error responses.
            error_page 401 /$locale/.error/401.$media_type_extension;
            error_page 403 /$locale/.error/403.$media_type_extension;
            error_page 404 /$locale/.error/404.$media_type_extension;
            location ~ ^/$locale/\.error {
                internal;
            }

            try_files $uri $uri/ =404;
        }

        # Static resources.
        location / {
            # Handle HTTP error responses.
            error_page 401 /{{ app.project.configuration.locales.default.alias }}/.error/401.$media_type_extension;
            error_page 403 /{{ app.project.configuration.locales.default.alias }}/.error/403.$media_type_extension;
            error_page 404 /{{ app.project.configuration.locales.default.alias }}/.error/404.$media_type_extension;
            location ~ ^/{{ app.project.configuration.locales.default.alias }}/\.error {
                internal;
            }

            try_files $uri $uri/ =404;
        }
    {% else %}
        location / {
            # Handle HTTP error responses.
            error_page 401 /.error/401.$media_type_extension;
            error_page 403 /.error/403.$media_type_extension;
            error_page 404 /.error/404.$media_type_extension;
            location /.error {
                internal;
            }

            try_files $uri $uri/ =404;
        }
    {% endif %}
}
