worker_processes auto;

events {
    worker_connections 1024;
}


http {

    sendfile on;

    keepalive_timeout 3600;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_proxied any;
    gzip_min_length 256;
    gzip_vary on;
    gzip_comp_level 6;

    server {

        include mime.types;
        default_type application/octet-stream;

        listen 3000;
        listen [::]:3000;
        server_name moviepilot;

        location / {
            # 主目录
            expires off;
            add_header Cache-Control "no-cache, no-store, must-revalidate";
            root html;
            try_files $uri $uri/ /index.html;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
            # 静态资源
            expires 1y;
            add_header Cache-Control "public, immutable";
            root html;
        }

        location /assets {
            # 静态资源
            expires 1y;
            add_header Cache-Control "public";
            root html;
        }

        location ~ ^/api/v1/system/(message|progress/) {
            # SSE MIME类型设置
            default_type text/event-stream;

            # 禁用缓存
            add_header Cache-Control no-cache;
            add_header X-Accel-Buffering no;
            proxy_buffering off;
            proxy_cache off;

            # 代理设置
            proxy_pass http://backend_api;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            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_read_timeout 3600s;
        }

        location /api {
            # 后端API
            proxy_pass http://backend_api;
            rewrite ^.+mock-server/?(.*)$ /$1 break;
            proxy_http_version 1.1;
            proxy_buffering off;
            proxy_cache off;
            proxy_redirect off;
            proxy_set_header Connection "";
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Nginx-Proxy true;

            # 超时设置
            proxy_read_timeout 600s;
        }

        location /cookiecloud {
            # 后端cookiecloud地址
            proxy_pass http://backend_api;
            rewrite ^.+mock-server/?(.*)$ /$1 break;
            proxy_http_version 1.1;
            proxy_buffering off;
            proxy_cache off;
            proxy_redirect off;
            proxy_set_header Connection "";
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Nginx-Proxy true;

            # 超时设置
            proxy_read_timeout 600s;
        }

    }

    upstream backend_api {
        # 后端API的地址和端口
        server 127.0.0.1:3001;
        # 可以添加更多后端服务器作为负载均衡
    }

}
