# 陪练试用版 Nginx配置
# 域名: pl.ireborn.com.cn

upstream pl_backend {
    server 172.18.0.1:8020;
}

upstream pl_frontend {
    server 172.18.0.1:3020;
}

# HTTP -> HTTPS 重定向
server {
    listen 80;
    server_name pl.ireborn.com.cn;
    
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }
    
    location / {
        return 301 https://$host$request_uri;
    }
}

# HTTPS 服务
server {
    listen 443 ssl;
    server_name pl.ireborn.com.cn;

    ssl_certificate /etc/letsencrypt/live/pl.ireborn.com.cn/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/pl.ireborn.com.cn/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    access_log /var/log/nginx/pl_access.log;
    error_log /var/log/nginx/pl_error.log;

    location / {
        proxy_pass http://pl_frontend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        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;
        
        # 禁用缓存
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        add_header Pragma "no-cache";
        add_header Expires "0";
    }

    location /api/ {
        proxy_pass http://pl_backend;
        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_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    location /@vite/ {
        proxy_pass http://pl_frontend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}
