# Easy Study - Nginx 配置 (Linux)
# 统一通过 http://<host>/ 访问所有资源
#
# 资源汇总：所有静态资源已通过软链接（见 linux-link.sh）汇总到主工作空间 $workhome：
#   $workhome = /data/easystudy/static
# 汇总后 URI 子路径与文件子目录一一对应，因此 Nginx 只需以 $workhome 为 root：
#   /images/...            -> $workhome/images/...
#   /flashcards/...        -> $workhome/flashcards/...
#   /data/...              -> $workhome/data/...
#   /audio/{books,ipa,words,tts-words,en,cn,phonemes}/... -> $workhome/audio/...

server {
    listen 80;
    server_name localhost;

    # === 静态资源统一根目录（= $workhome）===
    root /data/easystudy/static;
    charset utf-8;

    # === 前端 (React dev server) ===
    location / {
        proxy_pass http://127.0.0.1:5173;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    # === 后端 API ===
    location /api/ {
        proxy_pass http://127.0.0.1:8000/api/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # === 后端 Swagger 文档 ===
    location /docs {
        proxy_pass http://127.0.0.1:8000/docs;
    }
    location /openapi.json {
        proxy_pass http://127.0.0.1:8000/openapi.json;
    }

    # === 绘本图片 (牛津树 stage-01 ~ stage-14) ===
    # 访问: /images/stage-03/AtTheSeaside/003.png
    location /images/ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # === 牛津拼读闪卡 ===
    # 访问: /flashcards/letters/OPH_WB1_Letter_Cards.pdf
    location /flashcards/ {
        expires 30d;
    }

    # === 结构化数据 (JSON) ===
    # 访问: /data/book-index.json, /data/book-structured/stage-03/AtTheSeaside.json
    location /data/ {
        expires 5m;
        add_header Cache-Control "public";
    }

    # === NAS 单词音频代理 (备用，比 /audio/ 更具体，优先匹配) ===
    # 访问: /audio/nas/ship.mp3
    location /audio/nas/ {
        proxy_pass http://192.168.11.94:10022/estudy/words/;
        expires 30d;
    }

    # === TTS 生成的单词发音 (短缓存，比 /audio/ 更具体) ===
    # 访问: /audio/tts-words/hello.mp3
    location /audio/tts-words/ {
        expires 1d;
    }

    # === 其余音频统一入口 (books/ipa/words/en/cn/phonemes) ===
    # 访问: /audio/books/...  /audio/ipa/...  /audio/words/ship.mp3
    #       /audio/en/ship.mp3  /audio/cn/船.mp3  /audio/phonemes/sh.mp3
    location /audio/ {
        expires 30d;
        add_header Cache-Control "public";
    }
}
