Phoenix http health checks

I have a Phoenix app that uses the 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")

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

  def call(conn, _), do: conn

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

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

Hope that helps.