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

include /etc/nginx/conf.d/bemeal-upstream.inc;

server {
    listen 80;
    listen [::]:80;
    server_name bemeal.leonmorival.com;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

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

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name bemeal.leonmorival.com;

    ssl_certificate /etc/letsencrypt/live/bemeal.leonmorival.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/bemeal.leonmorival.com/privkey.pem;

    client_max_body_size 50m;

    location / {
        proxy_pass http://bemeal_api;
        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 $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 90s;
        proxy_send_timeout 90s;
        proxy_redirect off;
    }
}
