# /etc/nginx/sites-enabled/andymal on alisv. DNS andymal.com -> alisv; TLS terminates here;
# traffic goes over tunnel-t3090-andymal.service (autossh -L 127.0.0.1:63410 -> t3090 127.0.0.1:8090).
upstream andymal_backend {
    server 127.0.0.1:63410 max_fails=3 fail_timeout=10s;
    keepalive 16;
}
server {
    listen 80;
    server_name andymal.com www.andymal.com api.andymal.com;

    location /.well-known/acme-challenge/ {
        root /var/www/andymal;
        default_type text/plain;
    }

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

server {
    listen 443 ssl http2;
    server_name andymal.com www.andymal.com;

    ssl_certificate /etc/letsencrypt/live/andymal.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/andymal.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:andymal_SSL:10m;
    ssl_session_timeout 1d;

    add_header X-Content-Type-Options nosniff always;
    add_header Referrer-Policy strict-origin-when-cross-origin always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    location ~ /\.(?!well-known/) { return 404; }

    location / {
        proxy_pass http://andymal_backend/www/;   # landing page is served by the api container under /www/
        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_connect_timeout 10s;
        proxy_read_timeout 120s;
    }
}

server {
    listen 443 ssl http2;
    server_name api.andymal.com;

    ssl_certificate /etc/letsencrypt/live/andymal.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/andymal.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:andymal_api_SSL:10m;
    ssl_session_timeout 1d;

    client_max_body_size 64m;
    client_body_timeout 300s;

    add_header X-Content-Type-Options nosniff always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    location / {
        proxy_pass http://andymal_backend;
        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_connect_timeout 10s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
    }
}
