ollama 添加api key验证,nginx转发https
以上就兼容好了openai的api请求,本地将外网的8092转发到本地443端口,最终请求的url为。使用nginx,使用https进行访问ollama,并添加api-key的校验。2. 自己申请https需要的key,修改nginx配置,增加如下配置。其中这部分是对apikey进行验证,不匹配则返回。api key填写nginx 配置里的key。1. 安装nginx。
·
使用nginx,使用https进行访问ollama,并添加api-key的校验
1. 安装nginx
sudo apt install nginx
2. 自己申请https需要的key,修改nginx配置,增加如下配置
server {
listen 443 ssl;
server_name xx.xxx.com;
#ssl证书配置
ssl_certificate /home/wei/pem/fullchain.pem;
ssl_certificate_key /home/wei/pem/privkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 反代到 Ollama API
location / {
# Ollama 默认端口是 11434
proxy_pass http://localhost:11434;
# 请求时的origin请求头
proxy_set_header origin http://localhost: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;
set $auth_header $http_authorization;
if ($auth_header != "Bearer sk-595b92eba33c4c1b91f08314abf21234") {
add_header 'WWW-Authenticate' 'Bearer realm="Access to the API"';
return 401;
}
# 处理预检请求 (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;
}
}
}
其中这部分是对apikey进行验证,不匹配则返回
set $auth_header $http_authorization;
if ($auth_header != "Bearer sk-595b92eba33c4c1b91f08314abf21234") {
add_header 'WWW-Authenticate' 'Bearer realm="Access to the API"';
return 401;
}
以上就兼容好了openai的api请求,本地将外网的8092转发到本地443端口,最终请求的url为
https://xx.xxx.com:8092/v1/
api key填写nginx 配置里的key
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)