SoFunction
Updated on 2025-04-11

Detailed explanation of the problem of Nginx anti-generation Ollama interface not being able to output verbatim across domains

Scene

Deepseek model is deployed locally, using Ollama management, the intranet penetrates to the public network, and is connected to the nginx anti-generation ollama interface.

Problem description

  • Cross-domain issues
  • When forwarding nginx, origin needs to be added to the request header, and origin must also be the same as the Ollama interface (protocol, IP, and port).
proxy_set_header origin http://ip:11434;
  • Cannot output verbatim
  • Key configuration: Disable buffering and cache
proxy_buffering off;
proxy_cache off;
  • Ensure real-time streaming
proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;

Complete configuration

server {
        listen 80;
        server_name domain name;
        return 301 https://$server_name$request_uri;
}
server {
        listen 443 ssl;
        server_name domain name;
        
		#ssl certificate configuration        ssl_certificate      /opt/nginx/conf/ssl/xxx/;
        ssl_certificate_key  /opt/nginx/conf/ssl/xxx/;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        
 	# Anti-generation to the Ollama API    location /api/ {
    	# Ollama's default port is 11434        proxy_pass  http://Server IP: 11434;        # Origin request header at request        proxy_set_header origin http://Server IP: 11434;        
        # Turn off Nginx's response buffering and force data to be transferred to the client in real time        proxy_buffering off;
        # Use HTTP/1.1 to support long connections to avoid short connection problems in HTTP/1.0        proxy_cache off;
        # Ensure real-time streaming        proxy_set_header Connection '';
        proxy_http_version 1.1;
        # Close Nginx's block encoding processing (adjust according to actual situation)        chunked_transfer_encoding off;

        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;

        # Add CORS header        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;

        # Process preflight requests (OPTIONS)        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

Summarize

This is the article about the problem of the Nginx anti-generation Ollama interface that cannot be output verbatim verbatim. For more related content related to Nginx anti-generation Ollama, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!