Phoenix http health checks

I have a Phoenix app that uses the fly.io tls middleware.

My endpoint is configured to redirect http requests to https with :

force_ssl: [rewrite_on: [:x_forwarded_proto]

This means that every http health checks (http://localhost:8080/healthz) are being redirected to https://[public_domain_name]/healthz.

How can we fix this?

What we do in this situation is move the health check logic into a plug and inject it before Plug.SSL

In our Endpoint we have something like (note order is important)

...

plug FooWeb.Plugs.HeathCheck

plug Plug.SSL,
    rewrite_on: [:x_forwarded_proto],
    exclude: ["localhost"]

...

Where a simple HealthCheck plug looks something like…

defmodule FooWeb.Plugs.HeathCheck do
  @behaviour Plug

  import Plug.Conn

  def init(opts), do: opts

  def call(%{request_path: "/_healthy"} = conn, _) do
    serve_pass!(conn, "HEALTHY")
  end

  def call(%{request_path: "/_ready"} = conn, _) do
    serve_pass!(conn, "READY")
  end

  def call(conn, _), do: conn

  def serve_pass!(conn, msg) do
    conn |> send_resp(200, msg) |> halt()
  end
end

This also gives you a nice place to check other health related things.

Hope that helps.

4 Likes