You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose部署Loki后无法从浏览器访问API的问题咨询

Loki API访问问题排查与解答

问题背景

通过Docker Compose部署grafana-loki后,尝试调用API推送日志时,浏览器访问http://loki:3100/loki/等地址出现“站点无法访问”或404错误,尝试过loki、127.0.0.1、localhost、本机IP以及/loki/api、loki/v1等地址组合均无效。容器运行状态正常,最终通过curl成功调用API,但仍有疑问:浏览器无法访问是否因为Loki没有暴露类似Swagger的可视化API界面?

部署配置

Docker Compose中Loki的配置如下:

loki:
    container_name:  olala-telemetry-loki
    image: grafana/loki:main-125e168
    command: [ "-config.file=/etc/loki/local-config.yaml" ]
    volumes:
      - ./otel-config/loki.yml:/etc/loki/local-config.yaml
    ports:
      - 3100:3100

容器运行状态

VS Code容器视图显示Loki正常运行,控制台日志输出:

olala-telemetry-loki            | level=info ts=2023-07-23T19:00:12.881124937Z caller=table_manager.go:136 index-store=boltdb-shipper-2020-10-24 msg="uploading tables"
olala-telemetry-loki            | level=info ts=2023-07-23T19:00:12.881339039Z caller=table_manager.go:227 index-store=boltdb-shipper-2020-10-24 msg="syncing tables"
olala-telemetry-loki            | ts=2023-07-23T19:00:12.881378688Z caller=spanlogger.go:85 level=info msg="building index list cache"
olala-telemetry-loki            | ts=2023-07-23T19:00:12.881396643Z caller=spanlogger.go:85 level=info msg="index list cache built" duration=10.888µs

已验证的有效API调用方式

通过curl成功推送日志的命令:

curl -v -H "Content-Type: application/json" -X POST -s "http://localhost:3100/loki/api/v1/push" --data-raw "{\"streams\": [{ \"stream\": { \"foo\": \"bar2\" }, \"values\": [ [ \"1687280319000000000\", \"fizzbuzz\" ] ] }]}"

问题解答

  • 浏览器访问失败原因:Loki默认没有提供类似Swagger的可视化API文档界面,所有API均为纯接口形式,仅支持特定的HTTP请求方法(如推送日志需POST)。直接在浏览器访问会以GET请求方式发起,而多数Loki API端点不支持GET,或路径不符合API规范,因此返回404或无法访问。
  • 前期API调用失败的可能原因:
    • 路径错误:Loki的API固定前缀为/loki/api/v1/,比如推送日志的正确路径是/loki/api/v1/push,之前尝试的/loki/、/loki/api等路径均不符合API规范。
    • 请求方法错误:推送日志接口仅接受POST请求,浏览器默认的GET请求不被支持。
  • Postman访问方式:
    1. 选择POST请求,URL填入http://localhost:3100/loki/api/v1/push
    2. 设置请求头:Content-Type: application/json
    3. 切换到Body标签页,选择raw格式,输入JSON格式的日志数据:
      {"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1687280319000000000", "fizzbuzz" ] ] }]}
      
    4. 发送请求即可完成日志推送。

内容的提问来源于stack exchange,提问作者Art

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:06:20