Grafana对接Nginx代理的Pyroscope返回405 Not Allowed问题求助
环境配置
- 主机A:
Pyroscope运行在localhost:4040,由Nginx反向代理,nginx.conf配置片段如下:location /pyroscope { proxy_pass http://localhost:4040; # dav_methods PUT; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } - 主机B:
运行Grafana服务器,需向Pyroscope发送请求
问题描述
我是不是漏了什么配置?操作哪里出错了?还能做哪些测试?搞不懂的是用curl测试明明正常...
我尝试添加数据源的结果如下:
UI界面操作
我把Pyroscope作为数据源添加到Grafana,地址填的是http://ip/pyroscope,点击“保存并测试”时返回:405 not allowed
命令行curl测试
curl http://ip/pyroscope StatusCode : 200 StatusDescription : OK Content : <!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><meta name="viewport" content="width=device-width"/><meta name="theme-color" ... RawContent : HTTP/1.1 200 OK Connection: keep-alive Vary: Accept-Encoding Content-Length: 498 Content-Type: text/html Date: Wed, 17 Jan 2024 11:53:48 GMT Server: nginx/1.22.0 <!doctype html><html lang="en... Forms : {} Headers : {[Connection, keep-alive], [Vary, Accept-Encoding], [Content-Length, 498], [Content-Type, text/html]...} Images : {} InputFields : {} Links : {} ParsedHtml : mshtml.HTMLDocumentClass RawContentLength : 498
问题排查与解决
405错误核心原因
Grafana添加Pyroscope数据源时,会发送POST请求到/pyroscope/api/v1/query这类API端点,但当前Nginx配置的路径映射有问题:proxy_pass http://localhost:4040;会把/pyroscope前缀直接附加到Pyroscope的请求路径前,导致实际请求变成http://localhost:4040/pyroscope/api/...,和Pyroscope的API路由不匹配,从而返回405不允许的请求方法。Nginx配置修正
修改proxy_pass路径,末尾加上斜杠,让Nginx把/pyroscope后的路径直接追加到Pyroscope的根路径后:
location /pyroscope { proxy_pass http://localhost:4040/; # dav_methods PUT; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
修改后重启Nginx生效。
- 额外测试步骤
- 模拟Grafana请求:用curl发送POST请求测试API端点,比如
curl -X POST http://ip/pyroscope/api/v1/query -d '{"query": "process_cpu:rate1m"}',检查是否返回正常响应。 - 查看日志:检查主机A上Pyroscope的日志,确认是否收到Grafana的请求及路径是否正确;同时查看Nginx的
access.log和error.log,定位请求的方法、路径和错误信息。 - 跳过代理测试:在主机B直接访问
http://主机A_IP:4040/api/v1/query,如果正常,说明问题确实出在Nginx代理配置。
内容的提问来源于stack exchange,提问作者protogia
相关产品推荐
相关产品推荐

