brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a location with return 200 gives nginx its own health check URL

Nginx
~/tips/tip-nginx-healthz-return-200

return 200 "ok\n";

Adds a tiny endpoint that answers ok without touching any backend or file.

A load balancer, uptime monitor or Docker healthcheck needs a URL that tells it whether nginx itself is alive. Pointing it at the home page mixes in the backend and fills the log.

location = /healthz {
    access_log off;
    default_type text/plain;
    return 200 "ok\n";
}

location = matches only that exact path and is checked first, so it is cheap. return answers straight from nginx, with no file and no proxy. default_type makes the response text/plain instead of the default binary type, and access_log off keeps a check every few seconds out of your logs. curl -i then shows 200, Content-Type: text/plain and ok.

For a Docker healthcheck on the official alpine image, curl is already inside:

test: ["CMD", "curl", "-fs", "http://localhost/healthz"]

Know what it proves: nginx runs and answers. It says nothing about the app behind it. If you want the check to fail when the backend is down, proxy /healthz to the backend’s own health route instead. Put it in the server block that a request to localhost lands in, or the check hits your default server and gets a 404. I’d also keep it off the public internet with allow and deny lines if it exposes anything more than ok.

Further reading: return, location

Going deeper: Keep a domain pointed at your home with Cloudflare DDNS on Unraid

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link