# Emotion Museum HTTPS Nginx 配置
# 配置路径：/www/server/panel/vhost/nginx/emotion-museum.conf

server {
    listen 443 ssl;
    http2 on;
    server_name lifescript.happylifeos.com;

    ssl_certificate /etc/letsencrypt/live/lifescript.happylifeos.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/lifescript.happylifeos.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    location ^~ /.well-known/acme-challenge/ {
        root /data/www/acme-challenge;
        allow all;
        try_files $uri =404;
    }

    location = / {
        return 404;
    }

    location /emotion-museum/ {
        alias /data/www/emotion-museum/;
        autoindex off;
        # 静态资源直接返回文件，不经过 try_files
        location ~* ^/emotion-museum/(assets|static)/ {
            add_header Cache-Control "public, max-age=31536000, immutable";
            expires 1y;
            try_files $uri =404;
        }
        # SPA 路由兜底
        try_files $uri $uri/ /emotion-museum/index.html;
    }

    location = /emotion-museum {
        rewrite ^(.*)$ $1/ permanent;
    }

    location /emotion-museum-admin/ {
        alias /data/www/emotion-museum-admin/;
        autoindex off;
        try_files $uri $uri/ /emotion-museum-admin/index.html;
    }

    location = /emotion-museum-admin {
        rewrite ^(.*)$ $1/ permanent;
    }

    location /course-of-life/ {
        alias /data/www/course-of-life/;
        autoindex off;
        try_files $uri $uri/ /course-of-life/index.html;
    }

    location = /course-of-life {
        rewrite ^ /course-of-life/ last;
    }

    # life-script React 应用
    location /life-script/ {
        alias /data/www/life-script/;
        autoindex off;
        # 静态资源直接返回文件
        location ~* ^/life-script/(assets|static)/ {
            add_header Cache-Control "public, max-age=31536000, immutable";
            expires 1y;
            try_files $uri =404;
        }
        # SPA 路由兜底
        try_files $uri $uri/ /life-script/index.html;
    }

    location = /life-script {
        rewrite ^ /life-script/ last;
    }

    location /api {
        proxy_pass http://127.0.0.1:19089;
        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 /ws {
        proxy_pass http://127.0.0.1:19089;
        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;
        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
    }

    location /health {
        access_log off;
        return 200 "healthy\n";
        add_header Content-Type text/plain;
    }

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    access_log /www/wwwlogs/ssl-access.log;
}

server {
    listen 80;
    server_name lifescript.happylifeos.com;
    return 301 https://$host$request_uri;
}
