upstream app_server {
    server 127.0.0.1:8000 fail_timeout=0;
}

server {
    listen 443 ssl;
    keepalive_timeout 5;
    
    ssl_certificate /etc/ssl/geeksbot_app_cert_chain.crt;
    ssl_certificate_key /etc/ssl/geeksbot.app.key;

    access_log /tmp/logs/geeksbot/access.log;
    error_log /tmp/logs/geeksbot/error.log;

    location /static/ {
        alias /code/geeksbot_v2/staticfiles/;
    }

    location /error/ {
        alias /code/geeksbot_v2/staticfiles/errors/;
    }

    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
        proxy_buffering off;
        proxy_read_timeout 180;
        proxy_connect_timeout 180;

        if (!-f $request_filename) {
            proxy_pass http://app_server;
            break;
        }
    }

    error_page 500 502 503 504 /error/maintenance.html;
}

server {
    listen 80 default_server;
    return 301 https://$host$request_uri;
}