惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
S
Securelist
T
Tor Project blog
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 热门话题
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tenable Blog
博客园_首页
TaoSecurity Blog
TaoSecurity Blog
Attack and Defense Labs
Attack and Defense Labs
Project Zero
Project Zero
The Hacker News
The Hacker News
M
MIT News - Artificial intelligence
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
F
Full Disclosure
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
N
Netflix TechBlog - Medium
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
GbyAI
GbyAI
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
博客园 - 叶小钗
Webroot Blog
Webroot Blog
Hacker News: Ask HN
Hacker News: Ask HN

博客园 - 三瑞

python3 在vscode 中调试 OpenClaw 能检测到浏览器,却弹不出窗口?Ubuntu 24.04 问题全复盘与终极解决 问题现象 idea 安装cline openclaw 允许局域网能访问 VirtualBox 共享文件夹配置指南(Ubuntu 24.04 不重启方案) ubuntu24.04 安装 vscode Ubuntu 24.04 安装 KVM 完整指南 Ubuntu 启动卡顿 2 分钟?一条命令解决 Linux下Ollama + AMD ROCm GPU加速安装实操指南 Ubuntu 24.04 挂载第二块磁盘并扩展 LVM 系统盘 openclaw qqbot 反复提示 ubuntu 终端代理设置 干货|xrdp 无人值守+同屏稳定配置(Ubuntu 22.04/24.04 实测可用) openclaw 使用不同的模型 AMD GPU (RX 7900 XTX) 使用情况查看 Ubuntu 24.04 自带GNOME RDP远程连不上?一招解决xrdp残留冲突问题 Ubuntu 24.04 磁盘空间管理:从查看到 LVM 动态扩容完整指南 避坑指南完整版:OpenClaw 连接 Ollama 详细实战教程 干货|Ubuntu 24.04 + AMD 7900 XTX 24G:Ollama 纯 Vulkan 加速部署(免 ROCm) Windows 连接 Ubuntu XRDP 远程桌面 QQ机器人接入OpenClaw完整指南:从零开始打造你的智能助手 VirtualBox U盘识别问题完美解决指南 ——记一次从入门到放弃再到入门的折腾历程 Ubuntu 系统 root 密码忘记怎么办?一招教你轻松重置 VirtualBox Ubuntu 虚拟机安装增强功能完整指南 HTTP 错误 500.21 - Internal Server Error 处理程序“BlockViewHandler”在其模块列表中有一个错误模块“ManagedPipelineHandler” 达梦数据库(DM)通过数据库类型生成修改字段类型的语句
Nginx+Bearer Key 保护 Ollama + OpenClaw 远程连接
三瑞 · 2026-05-10 · via 博客园 - 三瑞

一、场景与架构

  • Ollama 默认仅监听 127.0.0.1:11434,无密码,公网暴露极危险。
  • 方案:Nginx 做反向代理 + Bearer Token 鉴权,对外用域名 / 路径访问,密钥保护。
  • 最终效果:http://你的域名/ollama → 鉴权 → 转发到本地 Ollama;OpenClaw 带 Key 远程连接。

二、Nginx 配置(带 Bearer 鉴权)

1)配置文件(/etc/nginx/conf.d/ollama.conf)

server {
    listen 80;
    server_name 你的域名;  # 换成你的域名或公网IP

    # 核心:/ollama/ 路径反向代理+鉴权
    location /ollama/ {
        # 1. 预检请求放行(跨域)
        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 Authorization,Content-Type;
            return 204;
        }

        # 2. Bearer Key 校验(关键!)
        set $auth $http_authorization;
        if ($auth != "Bearer ollamakey") {  # 密钥:ollamakey,可自定义
            add_header WWW-Authenticate 'Bearer realm="Ollama API"';
            return 401;  # 鉴权失败返回401
        }

        # 3. 反向代理到本地 Ollama
        proxy_pass http://127.0.0.1:11434/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;
    }
}
    #if ($http_authorization != "Bearer E4613") {
    #    return 401 Unauthorized;
    #}

    # 反向代理到 11434
    #location /ollama/ {
        
        # 反向代理
        #proxy_pass http://127.0.0.1:11434/;
        #proxy_set_header Host $host;
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_http_version 1.1;
        #proxy_set_header Authorization "";  # 清除转发时的 Authorization

        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header X-Forwarded-Proto $scheme;
    #}

    # 放行模型列表API
    location = /ollama/api/tags {
        proxy_pass http://127.0.0.1:11434/api/tags;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location = /ollama/api/show {
        proxy_pass http://127.0.0.1:11434/api/show;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # 其他所有 /ollama/ 下的请求都需要鉴权
    location /ollama/ {
        # 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 Authorization,Content-Type;
            return 204;
        }
        
        # 鉴权
        if ($http_authorization != "Bearer E43") {
            add_header WWW-Authenticate 'Bearer realm="Ollama API"';
            return 401;
        }
        
        proxy_pass http://127.0.0.1:11434/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;
        proxy_set_header Authorization "";
    }
  • 密钥自定义:把 ollamakey 换成你的强密码,例如 Bearer E46xxxxA3

2)生效与重启

nginx -t          # 检查配置,必须显示 success
systemctl restart nginx

三、本地验证(先测通 Nginx+Ollama)

1)查 Ollama 模型列表(确认服务正常)

2)带 Key 测试接口(关键验证)

curl -i \
  -H "Authorization: Bearer ollamakey" \
  http://你的域名/ollama/api/tags
  • 正常:返回 200 + 模型 JSON 列表。
  • 401:Key 写错或 Nginx 配置没生效。
  • 404:路径错(必须 /ollama/api/tags)。

四、OpenClaw 对接配置(远程连接)

1)模型提供商选择

  • 选:Ollama(不要选 OpenAI)。

2)3 个核心参数(照抄)

配置项填写内容
Base URL http://你的域名/ollama(无 /v1,末尾无斜杠)
API Key ollamakey(Bearer 后的值)
自定义请求头 Authorization: Bearer ollamakey(头名 / 值分开填)

3)模型名

填本地存在的模型,如 qwen3:7bgemma4:e4b(用 ollama list 查看)。

4)测试连接

  • 点「测试」,返回成功即可对话。
  • 常见失败:Base URL 带端口、多写 /v1、请求头没带对。

五、排错速查(最常见 3 点)

  1. 401 Unauthorized:
    • 请求头格式错:少空格(Bearerollamakey)、多引号、大小写错误。
  2. 404 Not Found:
    • Base URL 错:写成 http://域名:11434/ollama/v1
  3. OpenClaw 连不上:
    • 没加自定义请求头,或头名写错(必须 Authorization)。

六、总结

  • Nginx 用 location /ollama/ + Bearer 鉴权,安全暴露 Ollama。
  • 验证用 curl -H "Authorization: Bearer 密钥" 地址,通了再配 OpenClaw。
  • OpenClaw 核心:Base URL 填 /ollama、Key 填密钥、头带 Authorization

要不要我把这套配置整理成一份可直接复制的 Nginx 配置文件和 OpenClaw 导入模板,你直接粘贴就能用?