worker_processes 1;

events { worker_connections 1024; }

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name _;

        # 前端静态资源
        root /usr/share/nginx/html;

        # 健康检查
        location /healthz {
            return 200 'ok';
            add_header Content-Type text/plain;
        }

        # 代理 Django 静态文件
        location /static/ {
            alias /app/static/;
            expires 30d;
            add_header Cache-Control "public";
        }

        # 代理 Django 媒体文件
        location /media/ {
            alias /app/media/;
            expires 30d;
            add_header Cache-Control "public";
        }

        # 代理后端 API
        location /api/ {
            proxy_pass http://backend:8000;
            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;
            client_max_body_size 100m;
            client_body_buffer_size 128k;
            proxy_connect_timeout 180;
            proxy_send_timeout 180;
            proxy_read_timeout 180;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            #proxy_cache_bypass $http_upgrade;
            #proxy_cache_key $uri$request_body; #增加此行
            #proxy_cache_methods GET POST;  #增加此行
            #add_header Access-Control-Allow-Headers X-API-Token;
            access_log /var/log/nginx/assets_access.log;
            error_log /var/log/nginx/assets_error.log;
        }

        # 前端 history 路由
        location / {
            try_files $uri $uri/ /index.html;
        }

        # gzip
        gzip on;
        gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css application/json;
        gzip_min_length 1k;
        gzip_comp_level 2;
    }
}
