upstream hubs {
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response

    # for UNIX domain socket setups
    #server unix:/tmp/gunicorn.sock fail_timeout=0;

    # for a TCP configuration
    server 127.0.0.1:8000 fail_timeout=0;
}

upstream hubs-sse {
    # SSE server (twisted-based)
    server 127.0.0.1:8080 fail_timeout=0;
}

# Main server block
server {
{% if hubs_ssl_cert == None %}
    listen 80;
    listen [::]:80;
{% else %}
    listen 443 deferred;
    listen [::]:443 deferred;
    include ssl_params;
{% endif %}

    server_name {{ hubs_url_hostname }};

    client_max_body_size 4G;
    keepalive_timeout 5;

    location / {
      # checks for static file, if not found proxy to app
      try_files $uri @proxy_to_app;
    }

    # path for static files
    location /static {
      alias /usr/lib/python3.6/site-packages/hubs/static;
    }

    location /sse/ {
      include proxy_params;
      proxy_pass http://hubs-sse/;
      # Allow long-running queries (SSE):
      proxy_buffering off;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      proxy_cache off;
      chunked_transfer_encoding off;
      keepalive_timeout 0;
      proxy_read_timeout 30m;
    }

    location @proxy_to_app {
      include proxy_params;
      proxy_pass http://hubs;
    }

    #error_page 500 502 503 504 /500.html;
    #location = /500.html {
    #  root /path/to/app/current/public;
    #}
  }


{% if hubs_ssl_cert != None %}
# Redirect cleartext traffic to HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name {{ hubs_url_hostname }};
    return 301 https://$server_name$request_uri;
}
{% endif %}
