Health check POST (convert GET to POST)

(folex) #1

My environment is as follows.

I have an HTTP-service, that can tell if it’s working or not. It does so by responding on a POST requests, i.e.

  1. I send curl -X POST --data '{ jsonrpc ..., "method":alive' }
  2. And response contains "alive": true

So, I have several instances of that HTTP service, and I want to load balance (fail over, actually) between them. So I came up with the following config:

http://balancer:1234 {
    proxy / http://instance1:1234 http://instance2:1234 {
        policy first
        fail_timeout 1m
        max_fails 3
        try_duration 5s
        health_check /
        health_check_port 1234
        health_check_contains ''"alive":true'
        transparent
    }
}

But it doesn’t work, as it sends HTTP GET, and receives HTTP OK with an empty body.

So I’m looking for a way to make this work. One idea I came up with is to define another caddy endpoint that will take a GET from health check, convert it to POST, send to a service, and return response body. In my imaginations, it could look something like that:

http://instance1:5678 {
    proxy / {
        verb POST
        content '{ jsonrpc ..., "method":alive' }'
    }
}

http://balancer:1234 {
    proxy / http://instance1:1234 http://instance2:1234 {
        ...
        health_check /
        health_check_port 5678
        health_check_contains ''"alive":true'
        transparent
    }
}

Is there any way to achieve that without writing plugins or extending Caddy? Thanks in advance.

(Matthew Fay) #2

G’day @folex, welcome to the Caddy community.

There’s nothing in core Caddy that would let you modify a verb.

The only thing I can think of to do that doesn’t involve playing with source code would be to download Caddy with the CGI plugin and have the healthcheck endpoint run a bash script or something that manually POSTs the endpoint and outputs the response body.

https://caddyserver.com/docs/http.cgi

(Matt Holt) #3

We’ll fix this in Caddy 2.