worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    access_log /dev/stdout main;
    error_log /dev/stderr info;

    upstream maubot_upstream {
        server 127.0.0.1:3001;
    }

    server {
        listen 3000 default_server;
        listen [::]:3000 default_server;

        client_max_body_size 64m;

        location = /healthz {
            add_header Content-Type text/plain;
            return 200 "OK";
        }

        location / {
            proxy_pass http://maubot_upstream;
            proxy_http_version 1.1;
            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_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_buffering off;
        }
    }
}
