# Open WebUI reverse proxy via Nginx
# Place in /etc/nginx/sites-available/openwebui and symlink to sites-enabled

upstream openwebui_backend {
    server 127.0.0.1:3081;
    keepalive 64;
}

server {
    listen 80;
    listen [::]:80;
    server_name webui.local;

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer" always;

    # Increase body size for file uploads
    client_max_body_size 50m;

    location / {
        proxy_pass http://openwebui_backend;
        proxy_http_version 1.1;

        # WebSocket support (required for streaming)
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Pass real client info
        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 Connection "";

        # Timeouts
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 86400s;  # Long timeout for streaming

        # Buffering
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
    }

    # Logging
    access_log /var/log/nginx/openwebui_access.log;
    error_log /var/log/nginx/openwebui_error.log;
}
