網(wǎng)站系統(tǒng)與網(wǎng)站源碼的關系上海今天發(fā)生的重大新聞
場景
本地部署deepseek模型,用的Ollama管理,內(nèi)網(wǎng)穿透到公網(wǎng),在通過nginx反代ollama接口。
問題描述
- 跨域問題
- nginx轉發(fā)時請求頭中需要加入origin,并且origin還要和ollama接口同源(協(xié)議、ip、端口一致)。
proxy_set_header origin http://ip:11434;
- 無法逐字輸出
- 關鍵配置:禁用緩沖和緩存
proxy_buffering off;
proxy_cache off;
- 確保流式傳輸?shù)膶崟r性
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 APIlocation /api/ {# Ollama 默認端口是 11434proxy_pass http://服務器IP:11434; # 請求時的origin請求頭proxy_set_header origin http://服務器IP:11434;# 關閉 Nginx 的響應緩沖,強制數(shù)據(jù)實時傳輸?shù)娇蛻舳?/span>proxy_buffering off;# 使用 HTTP/1.1 以支持長連接,避免 HTTP/1.0 的短連接問題proxy_cache off;# 確保流式傳輸?shù)膶崟r性proxy_set_header Connection '';proxy_http_version 1.1;# 關閉 Nginx 的分塊編碼處理(根據(jù)實際情況調(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;# 處理預檢請求 (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;}}
}