server {
    listen 443 ssl http2;
    server_name server.schooltech.ch;

    ssl_certificate /etc/letsencrypt/live/server.schooltech.ch/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/server.schooltech.ch/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    root /usr/share/nginx/html;
    index index.html;

    auth_request /nginxauth;

    # UI / SPA
    location / {
        try_files $uri $uri/ /index.html;
    }

    # API forwarding (auth)
    location /api/ {
        proxy_pass http://appserverauth:3000/api/;
        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;
    }

    # Internal auth endpoint for auth_request
    location = /nginxauth {
        internal;
        proxy_pass http://appserverauth:3000/internal/auth;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Original-URI $request_uri;
        proxy_set_header X-Original-Host $host;
        proxy_set_header X-Forwarded-Host $host;
    }

    # Security headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}
