worker_processes auto;
user nginx nginx;

events {
    worker_connections 4096;
}

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

    log_format main '$remote_addr - $remote_user [$time_local] $status "$request" $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$host"';

    error_log /var/log/nginx/error.log error;
    access_log /var/log/nginx/access.log main;

    sendfile on;
    keepalive_timeout 65;
    client_body_buffer_size 128k;
    client_header_buffer_size 2k;

    gzip on;
    gzip_types application/javascript text/css image/png image/jpeg image/gif;

    upstream backend {
        server monkeycode-server:8888;
    }

    server {
        listen 80;
        server_name _;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Port $http_x_forwarded_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        location /api {
            proxy_pass http://backend;
        }

        location /v1 {
            proxy_pass http://backend;
        }

        location /socket.io {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }

        location /v1/chat/completions {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_buffering off;
            proxy_cache off;
            proxy_read_timeout 24h; 
            proxy_connect_timeout 24h; 
            proxy_send_timeout 24h;
            keepalive_timeout 65;
        }
    }
}