# Redirect non-www to www
server {
    listen 80;
    {% if enable_ssl %}listen 443 ssl;{% endif %}
    server_name www.{{ site_hostname }};
    return 301 $scheme://{{ site_hostname }}$request_uri;
    {% if enable_ssl %}
    ssl_certificate /etc/nginx/ssl/certificate.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;
    {% endif %}
}

# Main server
server {
    listen 80 default_server;
    {% if enable_ssl %}listen 443 ssl default_server;{% endif %}
    server_name {{ site_hostname }};
    server_tokens off;

    client_max_body_size 50M;
    charset utf-8;

    access_log {{ app_logs_folder }}/ngin.access.log;
    error_log {{ app_logs_folder }}/nginx.error.log;

    {% if enable_ssl %}
    ssl_certificate /etc/nginx/ssl/certificate.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;

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

    location /archivos {
        autoindex off;
        alias {{ app_public_files_folder }};
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8000/;
        proxy_redirect off;
        expires -1;
    }
}

server {
    listen 80;
    {% if enable_ssl %}listen 443 ssl;{% endif %}
    server_name {{ site_media_hostname }};

    charset utf-8;

    access_log {{ app_logs_folder }}/nginx-media.access.log;
    error_log {{ app_logs_folder }}/nginx-media.error.log;

    {% if enable_ssl %}
    ssl_certificate /etc/nginx/ssl/certificate.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;

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

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'Content-Type,Accept';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';

        root {{ app_media_folder }};
        autoindex off;
        expires 1h;
    }
}

server {
    listen 80;

    {% if enable_ssl %}
    listen 443 ssl;
    {% endif %}
    server_name {{ site_static_hostname }};

    charset utf-8;

    access_log {{ app_logs_folder }}/nginx-static.access.log;
    error_log {{ app_logs_folder }}/nginx-static.error.log;

    {% if enable_ssl %}
    ssl_certificate /etc/nginx/ssl/certificate.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;

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

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'Content-Type,Accept';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';

        root {{ app_static_folder }};
        autoindex off;
        expires 1h;
    }
}
