# 加入 /etc/nginx/sites-available/live.gp777s.com 的 listen 443 ssl server { } 內
# 須放在 location / { ... } 之前，避免被 Next.js 吃掉

# 瀏覽器 WHEP 播放：/webRTC/play/{stream_key}
location ~ ^/webRTC/play/ {
    proxy_pass http://127.0.0.1:8787;
    proxy_http_version 1.1;
    proxy_request_buffering off;
    proxy_buffering off;

    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_read_timeout 3600s;
    proxy_send_timeout 3600s;
    proxy_connect_timeout 60s;

    client_max_body_size 10m;

    add_header Access-Control-Allow-Origin "*" always;
    add_header Access-Control-Allow-Methods "OPTIONS, POST, PATCH, DELETE" always;
    add_header Access-Control-Allow-Headers "Content-Type, Authorization, If-Match, ETag, Accept, Link" always;
    add_header Access-Control-Expose-Headers "Location, Link, ETag" always;
}

# OBS WHIP：/webRTC/publish/{stream_key}
location ~ ^/webRTC/publish/ {
    proxy_pass http://127.0.0.1:8787;
    proxy_http_version 1.1;
    proxy_request_buffering off;
    proxy_buffering off;

    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_read_timeout 3600s;
    proxy_send_timeout 3600s;
    proxy_connect_timeout 60s;

    client_max_body_size 10m;

    add_header Access-Control-Allow-Origin "*" always;
    add_header Access-Control-Allow-Methods "OPTIONS, POST, PATCH, DELETE" always;
    add_header Access-Control-Allow-Headers "Content-Type, Authorization, If-Match, ETag, Accept, Link" always;
    add_header Access-Control-Expose-Headers "Location, Link, ETag" always;
}

# Stream 縮圖：/stream/thumbnails/{stream_key}/thumbnail.jpg
location ~ ^/stream/thumbnails/ {
    proxy_pass http://127.0.0.1:8787;
    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_read_timeout 60s;
    proxy_connect_timeout 30s;
}

# Cloudflare Location 回傳的 session path：/{stream_key}/webRTC/...
location ~ ^/[a-zA-Z0-9_-]+/webRTC/ {
    proxy_pass http://127.0.0.1:8787;
    proxy_http_version 1.1;
    proxy_request_buffering off;
    proxy_buffering off;

    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_read_timeout 3600s;
    proxy_send_timeout 3600s;
    proxy_connect_timeout 60s;

    client_max_body_size 10m;

    add_header Access-Control-Allow-Origin "*" always;
    add_header Access-Control-Allow-Methods "OPTIONS, POST, PATCH, DELETE" always;
    add_header Access-Control-Allow-Headers "Content-Type, Authorization, If-Match, ETag, Accept, Link" always;
    add_header Access-Control-Expose-Headers "Location, Link, ETag" always;
}
