为Ollama添加API-key,解决风险问题
本文最后更新于80 天前,其中的信息可能已经过时,如有错误请发送邮件到2371964121@qq.com

一、环境配置

1、进行ollama安装

# 下载官方安装脚本
curl -L https://ollama.ai/install.sh | sh

# 启动服务并设置开机自启
sudo systemctl enable --now ollama

2、安装nginx

sudo apt update && sudo apt install nginx -y
sudo systemctl enable --now nginx

二、服务验证(可选)

1、验证Ollama运行

curl http://127.0.0.1:11434
# 预期返回:Ollama is running

2、验证Nginx运行

curl -I http://localhost

三、🚀 极简三步配置

【这里可以先把ollama默认启动端口也进行修改掉,避免直接扫描11434】

【注意,ollama启动时候host设置为127.0.0.1,不要设置为0.0.0.0,否则公网仍可访问】

1. 创建Nginx配置文件(直接覆盖原有配置)

sudo tee /etc/nginx/conf.d/ollama.conf <<'EOF'
server {
    listen 11434;

    location / {
        if ($http_authorization != "Bearer funhpc-666") {  # ← 这里改成你的密码
            return 403;
        }
        proxy_pass http://127.0.0.1:11434;
    }
}
EOF
sudo nginx -t && sudo systemctl reload nginx

2. 生效配置

sudo nginx -t && sudo systemctl reload nginx
nginx -t && systemctl reload nginx

3. 测试访问

# 正确请求(记得替换密码)
curl -H "Authorization: Bearer my-secret-key-123" http://localhost:9180
# 错误测试
curl http://localhost:9180  # 应显示403错误

四、测试请求

1、Crul测试案例

curl -H "Authorization: Bearer my-secret-key-123" \
  -X POST http://https://riq853u87qswxqntalk4090.funhpc.com:30499/api/generate \
  -d '{
    "model": "deepseek-r1:1.5b",
    "prompt": "为什么天空是蓝色的?",
    "stream": false
  }'

2、OpenAI调用效果

from openai import OpenAI
client = OpenAI(api_key="my-secret-key-123", base_url="https://riq853u87qswxqntalk4090.funhpc.com:30499/v1")

messages = [
    {"role": "system", "content": "You are a helpful assistant"},
    {"role": "user", "content": "最厉害的一个奥特曼是谁?"}
]

# 调用Chat Completions接口(必填model和messages)
response = client.chat.completions.create(
    model="deepseek-r1:1.5b",  # 需确认后端是否支持该模型名称
    messages=messages,
    stream=True)
# print(response)
reasoning_content = ""
content = ""

for chunk in response:
    delta = chunk.choices[0].delta
    # print(chunk)
    if hasattr(delta, "reasoning_content") and delta.reasoning_content:
        reasoning_content += delta.reasoning_content
        print(f"{delta.reasoning_content}", end="", flush=True)
    if hasattr(delta, "content") and delta.content:
        content += delta.content
        print(f"{delta.content}", end="", flush=True)

评论

  1. 博主
    Windows Edge
    3 月前
    2025-3-29 0:32:59

    |´・ω・)ノ

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
下一篇