Nginx反代Ollama接口跨域無法逐字輸出問題詳解
場景
本地部署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)文章
解決Nginx + PHP(FastCGI)遇到的502 Bad Gateway錯(cuò)誤
昨日,有朋友問我,他將Web服務(wù)器換成Nginx 0.6.31 + PHP 4.4.7(FastCGI)后,有時(shí)候訪問會(huì)出現(xiàn)“502 Bad Gateway”錯(cuò)誤,如何解決。2009-10-10Nginx之為已安裝nginx動(dòng)態(tài)添加模塊的方法
本篇文章主要介紹了Nginx之為已安裝nginx動(dòng)態(tài)添加模塊的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Nginx反向代理proxy_cache_path directive is not allowed錯(cuò)誤解決方法
這篇文章主要介紹了Nginx反向代理proxy_cache_path directive is not allowed錯(cuò)誤解決方法,需要的朋友可以參考下2015-04-04nginx重寫rewrite基礎(chǔ)及實(shí)例分享
這篇文章主要介紹了nginx重寫rewrite基礎(chǔ)及實(shí)例分享,需要的朋友可以參考下2016-04-04