nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Nginx反代Ollama无法逐字输出

Nginx反代Ollama接口跨域无法逐字输出问题详解

作者:null_17

这篇文章主要介绍了在本地部署DeepSeek模型,并通过Ollama管理,内网穿透到公网,再使用Nginx反向代理Ollama接口时遇到的跨域问题,文中通过代码介绍的非常详细,需要的朋友可以参考下

场景

本地部署deepseek模型,用的Ollama管理,内网穿透到公网,在通过nginx反代ollama接口。

问题描述

proxy_set_header origin http://ip:11434;
proxy_buffering off;
proxy_cache off;
proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;

完整配置

server {
        listen 80;
        server_name 域名;
        return 301 https://$server_name$request_uri;
}
server {
        listen 443 ssl;
        server_name 域名;
        
		#ssl证书配置
        ssl_certificate      /opt/nginx/conf/ssl/xxx/fullchain.pem;
        ssl_certificate_key  /opt/nginx/conf/ssl/xxx/key.pem;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        
 	# 反代到 Ollama API
    location /api/ {
    	# Ollama 默认端口是 11434
        proxy_pass  http://服务器IP:11434;  
        # 请求时的origin请求头
        proxy_set_header origin http://服务器IP:11434;
        
        # 关闭 Nginx 的响应缓冲,强制数据实时传输到客户端
        proxy_buffering off;
        # 使用 HTTP/1.1 以支持长连接,避免 HTTP/1.0 的短连接问题
        proxy_cache off;
        # 确保流式传输的实时性
        proxy_set_header Connection '';
        proxy_http_version 1.1;
        # 关闭 Nginx 的分块编码处理(根据实际情况调整)
        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;

        # 添加 CORS 头部
        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;

        # 处理预检请求 (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;
        }
    }
}

总结 

到此这篇关于Nginx反代Ollama接口跨域无法逐字输出问题的文章就介绍到这了,更多相关Nginx反代Ollama无法逐字输出内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文