{%- macro SET_PATH(directory, path="") %}
    {%- if path and path.startswith('/') -%}
        {{ path }}
    {%- else -%}
        {{ directory }}/{{ path }}
    {%- endif -%}
{% endmacro -%}


server {
    listen {{ PORT }};
    server_name {{ SERVER_NAME }};
    root {{ SET_PATH(DIRECTORY, ROOT_DIR) }};

    {% if LOGS_DIR %}
    access_log {{ LOGS_DIR }}/access_{{ SERVER_NAME }}.log;
    error_log {{ LOGS_DIR }}/error_{{ SERVER_NAME }}.log;
    {% endif %}

{%- if SSL_DIRECTIVES %}

    {{ SSL_DIRECTIVES }}

{%- elif SSL_CERT and SSL_KEY %}

    if ($scheme = "http") {
        return 301 https://{{ SERVER_NAME }}$request_uri;
    }

    listen 443 ssl;
    ssl_certificate     {{ SET_PATH(DIRECTORY, SSL_CERT) }} ;
    ssl_certificate_key {{ SET_PATH(DIRECTORY, SSL_KEY) }} ;

    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

{% endif -%}


{% if MAINTENANCE["ACTIVE"] %}
    set $maintenance on;

    {% set maintenance_page = "maintenance.html" %}
    {% if MAINTENANCE["PAGE"] %}
        {% set maintenance_page =  MAINTENANCE["PAGE"] %}
    {% endif %}

    # allow ips
    {% if MAINTENANCE["ALLOW_IPS"] %}
        if ($remote_addr ~ ({{ MAINTENANCE["ALLOW_IPS"] | join("|") }})) {
            set $maintenance off;
        }
    {% endif %}

    if ($maintenance = on) {
        return 503;
    }

    error_page 503 @maintenance;
    location @maintenance {
        {% if not MAINTENANCE["PAGE"] %}
            root /var/propel;
        {% endif %}

         rewrite ^(.*)$ /{{ maintenance_page }} break;
    }
{% endif %}


{% if (not MAINTENANCE["ACTIVE"])  or (MAINTENANCE["ACTIVE"] and MAINTENANCE["ALLOW_IPS"]) %}
    {% if PROXY_PORT %}
        location / {
            proxy_pass http://127.0.0.1:{{ PROXY_PORT }}/;
            proxy_redirect off;
            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-Host $server_name;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

    {% else %}

        location / {
            index index.html index.htm index.php;
        }

        # Pass PHP scripts to PHP-FPM
        location ~* \.php$ {
            fastcgi_index   index.php;
            fastcgi_pass    127.0.0.1:9000;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }

    {% endif %}

{% else %}

    location / {
        return 503;
    }

{% endif %}


{%- if ALIASES %}
    {%- for alias, location in ALIASES.items() %}
    location {{ alias }} {
        alias {{ SET_PATH(DIRECTORY, location) }} ;
    }
    {% endfor -%}
{% endif -%}

    {{ SERVER_DIRECTIVES }}
}

{% if FORCE_NON_WWW or FORCE_WWW %}

server {
    listen {{ PORT }};

    {% if SSL_CERT and SSL_KEY %}
    listen 443 ssl;
    {% endif %}

    {% if FORCE_NON_WWW %}

        server_name www.{{ NAME }};
        return 301 $scheme://{{ NAME }}$request_uri;

    {% elif FORCE_WWW and not NAME.startswith('www.') %}

        server_name {{ NAME }};
        return 301 $scheme://www.{{ NAME }}$request_uri;

    {% endif %}
}

{% endif %}