# /etc/nginx/sites-available/dsp
server {
    listen 443 ssl;
    server_name dsp.niph.org.kh;

    ssl_certificate     /etc/letsencrypt/live/dsp.niph.org.kh/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dsp.niph.org.kh/privkey.pem;

    # DSP app
    location / {
        proxy_pass http://127.0.0.1:4010;
        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;
    }

    # JupyterHub
    location /hub/ {
        proxy_pass http://127.0.0.1:443/;
        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

server {
    listen 443 ssl;
    server_name hub.niph.org.kh;

    ssl_certificate     /etc/letsencrypt/live/dsp.niph.org.kh/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dsp.niph.org.kh/privkey.pem;

    # Force bare-domain hits to /hub/ so JupyterHub routes exist
    location = / {
        return 302 https://hub.niph.org.kh/hub/;
    }

    location /hub/ {
        proxy_pass http://127.0.0.1:443/;
        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

server {
    listen 80;
    server_name dsp.niph.org.kh hub.niph.org.kh;

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

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