server {
    listen 80;
    server_name _;

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

    # 后端接口反向代理；Docker 启动时由 API_UPSTREAM 注入，例如：http://host.docker.internal:3000
    location /api/ {
        proxy_pass ${API_UPSTREAM};
        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_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
        proxy_buffering off;
    }

    # Vite build 产物：文件名带 hash，可长期缓存
    location /assets/ {
        expires 1y;
        add_header Cache-Control "public, max-age=31536000, immutable";
        try_files $uri =404;
    }

    # 单独图标按中等缓存（如果后续也迁到 /assets，可删除）
    location ~* ^/(favicon\.ico|favicon\.png|apple-touch-icon\.png)$ {
        expires 7d;
        add_header Cache-Control "public, max-age=604800";
        try_files $uri =404;
    }

    # 入口 HTML 每次协商缓存，确保能拿到最新资源清单
    location = /index.html {
        add_header Cache-Control "no-cache";
        try_files $uri =404;
    }

    # SPA 路由回退
    location / {
        try_files $uri $uri/ /index.html;
    }
}
