Skip to content

Browser CORS issues when connecting to remote ollama backend #39

@phillpott63464

Description

@phillpott63464

When running the app against a remote ollama server, the browser will tag OPTIONS and CORS requests to the request, which ollama can't natively handle, resulting in browsers blocking requests.

This can be bypassed with a reverse proxy, such as the below nginx config, which works for me.

server {
    listen 8080;
    server_name localhost;
    
    # Proxy openai style endpoint
    location /v1/chat/completions {
        proxy_pass YOUR_OLLAMA;

        proxy_hide_header Access-Control-Allow-Origin; # Strips ollama's default wildcard

        # Handle CORS preflight
        if ($request_method = OPTIONS) {
            add_header "Access-Control-Allow-Origin" "$http_origin" always;
            add_header "Access-Control-Allow-Headers" "$http_access_control_request_headers" always;
            add_header "Access-Control-Allow-Methods" "POST, OPTIONS" always;
            add_header "Access-Control-Max-Age" 86400;
            add_header "Vary" "Origin" always;
            return 204;
        }
        
        add_header "Access-Control-Allow-Origin" "$http_origin" always;
        add_header "Access-Control-Allow-Headers" "$http_access_control_request_headers" always;
        add_header "Vary" "Origin" always;
    }

    # Proxy everything else transparently
    location / {
        proxy_pass YOUR_OLLAMA;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions