当前位置: 首页 > news >正文

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

场景

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


问题描述

  1. 跨域问题
  • nginx转发时请求头中需要加入origin,并且origin还要和ollama接口同源(协议、ip、端口一致)。
proxy_set_header origin http://ip:11434;
  1. 无法逐字输出
  • 关键配置:禁用缓冲和缓存
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 APIlocation /api/ {# Ollama 默认端口是 11434proxy_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;}}
}

http://www.lryc.cn/news/533709.html

相关文章:

  • 大学资产管理系统中的下载功能设计与实现
  • 股指入门:股指期货是什么意思?在哪里可以做股指期货交易?
  • < OS 有关 > 利用 google-drive-ocamlfuse 工具,在 Ubuntu 24 系统上 加载 Google DRIVE 网盘
  • Golang的引用类型和指针
  • 51单片机之冯·诺依曼结构
  • 32. C 语言 安全函数( _s 尾缀)
  • Android T(13) 源码分析 — BufferQueue 的分析
  • Vite+TS项目中配置路径别名
  • 看盘细节系列 篇二:集合竞价的9点18分大单打到3%以下或以上,9点19分撤单
  • Java继承简介
  • redis之哨兵集群搭建
  • 保姆级AI开发环境搭建
  • Arduino 型号的对比
  • Kafka系列之:定位topic只能保存最新数据的原因
  • AtCoder Beginner Contest 391(A~E题题解)
  • mysql mvcc 锁 关系
  • 安卓手机基于 Termux 安装 AList 并设置开机自启的详细教程
  • LeetCode:503.下一个更大元素II
  • 实验5 配置OSPFv2验证
  • 第二节 docker基础之---镜像构建及挂载
  • 论文阅读:MGMAE : Motion Guided Masking for Video Masked Autoencoding
  • 记录一下 在Mac下用pyinstallter 打包 Django项目
  • 【漫话机器学习系列】084.偏差和方差的权衡(Bias-Variance Tradeoff)
  • deepseek本地部署-linux
  • 解决使用python提取word文档中所有的图片时图片丢失的问题
  • 【Spring相关知识】Spring应用如何优雅使用消息队列
  • 人工智能:从概念到未来
  • CUDA Graph
  • 1343. 大小为 K 且平均值大于等于阈值的子数组数目
  • IDEA+DeepSeek让Java开发起飞