Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple healthcheck endpoint in nginx server container

I have a docker container running with nginx server.

I want to provide a rest-interface/endpoint to check the health of the server and container. E.g. GET http://container.com/health/ which delivers "true"/OK or "false"/NOK.

What is the simplest and quickist solution or best practice?

P.S. The server serves as a file browser, i.e., with enabled Directory Index Listing.

like image 295
1c0DevPi Avatar asked Sep 12 '25 15:09

1c0DevPi


2 Answers

The following configuration worked for me with nginx version: nginx/1.14.0 (Ubuntu):

    location = /health {
            access_log off;
            add_header 'Content-Type' 'application/json';
            return 200 '{"status":"UP"}';
    }

To test it:

  • Install Nginx locally, example on Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04
  • Add location configuration (mentioned above) to the end of server block in the file /etc/nginx/sites-available/default and restart nginx server
  • Accessing http://localhost/health should return response {"status":"UP"}
like image 81
dzakharov Avatar answered Sep 15 '25 15:09

dzakharov


Add

server {
    // ....

    location /health {
        access_log off;
        add_header 'Content-Type' 'text/plain';
        return 200 "healthy\n";
    }
}

or if you need JSON support

server {
    // ....

    location /health {
        access_log off;
        add_header 'Content-Type' 'application/json';
        return 200 '{"status":"Healthy"}';
    }
}

NOTE: Any status 200 or above, and below 400 is considered Alive/Success. So it does not matter what the Body itself is. It just matters what the return status_code is.

Also if you need to proxy to a remote backend service.

server {
    // ....

    location / {
       proxy_pass http://backend;
       health_check interval=10 fails=3 passes=2;
    }
}

This way you can let your backend service handle the actual Status itself. Since just cause the container is ready and nginx is ready, does not always mean the backend service has connected to its dependent services such as database, etc.

like image 22
Bioblaze Payne Avatar answered Sep 15 '25 15:09

Bioblaze Payne