# =============================================================================
# Nginx 反向代理配置 - Apache Superset
# 複製此配置到 /etc/nginx/sites-available/superset
# =============================================================================

# 將此內容加入到 monitor.wooo.work 的 server 區塊中
# 2026-02-08 更新：修復登入重定向問題

# =============================================================================
# 關鍵設定 0：重定向 Superset 相關路徑
# 解決 Superset 生成的 URL 沒有 /superset/ 前綴的問題
# =============================================================================

# 認證相關路徑重定向
location = /login/ {
    return 302 /superset/login/;
}
location = /logout/ {
    return 302 /superset/logout/;
}

# 語言切換路徑重定向
location ^~ /lang/ {
    return 302 /superset$request_uri;
}

# 用戶資訊路徑重定向
location ^~ /users/ {
    return 302 /superset$request_uri;
}

# 靜態資源路徑重定向 (SPA 動態載入)
location ^~ /static/ {
    return 302 /superset$request_uri;
}

# Superset BI 分析平台
# 重要：Superset 內部路由已是 /superset/...，不需要再添加前綴
# 2026-02-08 更新：修復雙重前綴問題 (/superset/superset/)
location /superset/ {
    proxy_pass http://127.0.0.1:8088/;
    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;
    # 注意：不設置 X-Forwarded-Prefix 和 X-Script-Name
    # 因為 superset_config.py 中 x_prefix=0，Superset 不讀取這些標頭

    # 關鍵設定 1：Superset 內部路由已是 /superset/，保持不變
    # 只對非 /superset/ 開頭的路徑添加前綴
    proxy_redirect /superset/ /superset/;
    proxy_redirect ~^/(?!superset)(.*)$ /superset/$1;

    # 關鍵設定 2：禁用 gzip 壓縮，讓 sub_filter 可以正常運作
    proxy_set_header Accept-Encoding "";
    gzip off;

    # 關鍵設定 3：重寫 HTML 中的 URL
    # 只處理靜態資源路徑（不處理 href/action，避免重複添加）
    sub_filter '"/static/' '"/superset/static/';
    sub_filter "'/static/" "'/superset/static/";
    sub_filter_once off;
    sub_filter_types text/html application/javascript text/javascript text/css;

    # WebSocket 支援 (SQL Lab 需要)
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    # 超時設定 (SQL Lab 查詢可能較長)
    proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;

    # 緩衝設定
    proxy_buffering on;
    proxy_buffer_size 4k;
    proxy_buffers 8 4k;
}

# Superset 靜態資源 (可選，提升效能)
location /superset/static/ {
    proxy_pass http://127.0.0.1:8088/static/;
    proxy_cache_valid 200 1d;
    proxy_cache_valid any 1m;
    expires 1d;
    add_header Cache-Control "public, immutable";
}

# =============================================================================
# 或者使用獨立的 server 區塊 (superset.wooo.work)
# =============================================================================

# server {
#     listen 443 ssl http2;
#     server_name superset.wooo.work;
#
#     ssl_certificate /etc/letsencrypt/live/superset.wooo.work/fullchain.pem;
#     ssl_certificate_key /etc/letsencrypt/live/superset.wooo.work/privkey.pem;
#
#     location / {
#         proxy_pass http://127.0.0.1:8088;
#         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;
#
#         # WebSocket 支援
#         proxy_set_header Upgrade $http_upgrade;
#         proxy_set_header Connection "upgrade";
#
#         # 超時設定
#         proxy_connect_timeout 300;
#         proxy_send_timeout 300;
#         proxy_read_timeout 300;
#     }
# }
