grafan反向代理的正确配置一个简单的json数据源



我想在反向代理后面放一些服务,简单的服务可以工作
502问题发生在尝试访问grafana简单json数据源(POST+json负载(时
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).*$ 1
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).*$ 1
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).*$ 1          
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).*$ 1          
server AWS.WEB.RTGRAF *:5582/ maxconn 1000 check port 5582                      

最新更新