亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Nginx反代Ollama接口跨域無法逐字輸出問題詳解

 更新時(shí)間:2025年03月03日 10:28:32   作者:null_17  
這篇文章主要介紹了在本地部署DeepSeek模型,并通過Ollama管理,內(nèi)網(wǎng)穿透到公網(wǎng),再使用Nginx反向代理Ollama接口時(shí)遇到的跨域問題,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

場景

本地部署deepseek模型,用的Ollama管理,內(nèi)網(wǎng)穿透到公網(wǎng),在通過nginx反代ollama接口。

問題描述

  • 跨域問題
  • nginx轉(zhuǎn)發(fā)時(shí)請求頭中需要加入origin,并且origin還要和ollama接口同源(協(xié)議、ip、端口一致)。
proxy_set_header origin http://ip:11434;
  • 無法逐字輸出
  • 關(guān)鍵配置:禁用緩沖和緩存
proxy_buffering off;
proxy_cache off;
  • 確保流式傳輸?shù)膶?shí)時(shí)性
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 默認(rèn)端口是 11434
        proxy_pass  http://服務(wù)器IP:11434;  
        # 請求時(shí)的origin請求頭
        proxy_set_header origin http://服務(wù)器IP:11434;
        
        # 關(guān)閉 Nginx 的響應(yīng)緩沖,強(qiáng)制數(shù)據(jù)實(shí)時(shí)傳輸?shù)娇蛻舳?
        proxy_buffering off;
        # 使用 HTTP/1.1 以支持長連接,避免 HTTP/1.0 的短連接問題
        proxy_cache off;
        # 確保流式傳輸?shù)膶?shí)時(shí)性
        proxy_set_header Connection '';
        proxy_http_version 1.1;
        # 關(guān)閉 Nginx 的分塊編碼處理(根據(jù)實(shí)際情況調(diào)整)
        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;

        # 處理預(yù)檢請求 (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;
        }
    }
}

總結(jié) 

到此這篇關(guān)于Nginx反代Ollama接口跨域無法逐字輸出問題的文章就介紹到這了,更多相關(guān)Nginx反代Ollama無法逐字輸出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論