map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

upstream hypertwist_website_auth_server {
    server 127.0.0.1:3011;
    keepalive 32;
}

server {
    listen 80;
    listen [::]:80;
    server_name hypertwist.app www.hypertwist.app;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name hypertwist.app www.hypertwist.app;

    ssl_certificate /etc/letsencrypt/live/hypertwist.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/hypertwist.app/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    client_max_body_size 10m;

    location / {
        proxy_pass http://hypertwist_website_auth_server;
        proxy_http_version 1.1;
        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-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;
    }
}
