grafana 简单 json 数据源反向代理的正确配置

proper config for reverse proxy for grafana simple json data source

我想在反向代理后面放置一些服务,简单的服务工作。
尝试访问 grafana 简单 json 数据源(POST + json 有效负载)时出现 502 问题。
Grafana 本身不支持反向代理。

当前的 haproxy 配置:

frontend FRONT.AWS.WEB.PROXY
    mode http
    bind *:8080
    timeout client 1m
    option  httplog
    acl IS_RELE path_beg /release
    acl IS_GRAF path_beg /grafana
    use_backend BACK.AWS.WEB.ARTIFACTS if IS_RELE
    use_backend BACK.AWS.WEB.RTGRAF if IS_GRAF

backend BACK.AWS.WEB.ARTIFACTS
    mode http
    http-request set-path /
    http-response replace-value X-Application-Context (.*)(\release).*$ 
    server AWS.WEB.ARTIFACTS *:5581/ maxconn 1000 check port 5581

backend BACK.AWS.WEB.RTGRAF
    mode http
    #option forwardfor
    #balance source
    #option httpclose
    #option httpchk HEAD / HTTP/1.0
    http-request set-path /
    http-response replace-value X-Application-Context (.*)(\grafana).*$ 
    server AWS.WEB.RTGRAF *:5582/ maxconn 1000 check port 5582                 

grafana 中的数据源配置:

http://192.168.56.101:8080/grafana/

这是没有 haproxy 的工作请求:

curl -d '{"requestId":"Q119","timezone":"utc".....lters":[]}' -H 'Content-Type: application/json' http://localhost:8080/query

好评:

[{"columns":[{"text":"sym","type":"string"}, {"text":"time","type":""}, .... .... {"text":"mode","type":"string"}, {"text":"proto","type":"string"}],"rows":[],"type":"table"}]

但是 使用 haproxy:

 curl -d .... http://localhost:8080/grafana/query

502 响应:

<h1>502 Bad Gateway</h1>
The server returned an invalid or incomplete response. 

但只是为了确认,服务本身有效:

curl http://localhost:8080/grafana/?2+1

回复:

<html><head><style>a{text-decoration:none}a:link{color:024C7E}a:visited{color:024C7E}a:active{color:958600}body{font:10pt verdana;text-align:justify}</style></head><body><pre>3

Haproxy 日志:

127.0.0.1:42362 [16/Sep/2020:21:57:14.430] FRONT.AWS.WEB.PROXY BACK.AWS.WEB.RTGRAF/AWS.WEB.RTGRAF 0/0/0/3/3 200 274 - - ---- 1/1/0/0/0 0/0 "GET /grafana/?2+1 HTTP/1.1"                            
127.0.0.1:42418 [16/Sep/2020:21:57:32.038] FRONT.AWS.WEB.PROXY BACK.AWS.WEB.RTGRAF/AWS.WEB.RTGRAF 0/0/0/-1/0 502 214 - - PH-- 1/1/0/0/0 0/0 "POST /grafana/query HTTP/1.1"

grafana 日志:

INFO[09-16|22:23:02] Request Completed logger=context userId=1 orgId=1 uname=admin method=POST path=/api/datasources/proxy/2/query status=502 remote_addr=192.168.56.1 time_ms=6 size=107 referer="http://192.168.56.101:3000/d/aQPWEJFmz/system-status?orgId=1&refresh=10s"

找到问题了,以后的任何人,那就是你!
..................................................... ..有爱心的古代开发者

  1. 使用简单的调试请求 nc
  2. 你会发现,一开始请求的路径是错误的HTTP GET

因此需要重写:

backend BACK.AWS.WEB.ARTIFACTS                                                     
   mode http                                                                       
   http-request set-uri %[url,regsub(^/release/,/,)]                               
   http-response replace-value X-Application-Context (.*)(\release).*$           
   server AWS.WEB.ARTIFACTS *:5581/ maxconn 1000 check port 5581                   
backend BACK.AWS.WEB.RTGRAF                                                        
   mode http                                                                       
   http-response replace-value X-Application-Context (.*)(\grafana).*$           
   server AWS.WEB.RTGRAF *:5582/ maxconn 1000 check port 5582