worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    
    # === File serving optimizations ===
    sendfile on;
    sendfile_max_chunk 1m;      # Prevent large files from blocking workers
    tcp_nopush on;
    tcp_nodelay on;
    
    # Async I/O for large files (>10MB uses direct I/O)
    aio threads;
    directio 10m;
    
    # Disable access logging for performance
    access_log off;
    
    # Keep-alive optimization
    keepalive_timeout 65;
    keepalive_requests 1000;
    
    # Buffer optimizations
    client_body_buffer_size 128k;
    output_buffers 2 1m;
    
    server {
        listen 80;

        # Serve DWG files (public, no auth)
        location / {
            root /app;
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
        }

        # Internal location for X-Accel-Redirect - dwg_files
        # Only accessible via internal redirect from Flask
        location /internal/dwg_files/ {
            internal;
            alias /app/dwg_files/;
        }

        # Internal location for X-Accel-Redirect - database_files
        location /internal/database_files/ {
            internal;
            alias /app/database_files/;
        }

        # Reverse proxy to Flask API
        location /api/ {
            proxy_pass http://127.0.0.1:5000/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
            # Keep-alive to upstream
            proxy_http_version 1.1;
            proxy_set_header Connection "";
        }
    }
}
