9

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.

m19v
  • 1,800
  • 4
  • 11
  • 26
  • Is this a static site or is there something else nginx is running? Are you only looking for the nginx health? And if not, what else? – BMitch Feb 03 '20 at 12:10
  • 1
    You can make a separate `location` block returning single string, for example `location /health { return 200 "OK\n"; }`, but if nginx stopped working, how can you make it return somehing? I thing the only thing you can do in this case is to process connection timeout as failure. – Ivan Shatsky Feb 03 '20 at 14:33
  • @IvanShatsky, how would I do that? could you explain a bit your answer. – m19v Feb 03 '20 at 14:41
  • @BMitch, it is just nginx server and there are files in home/http folder. Via browser i can see/list the files. Any suggestion accepted. – m19v Feb 03 '20 at 14:44
  • I already give you an example of `location` block that will return "OK" in response to `http://container.com/health` request. There are many ways to check if container responds to this request (curl or its libraries is the first thing that came to my mind), my idea is to assume that container is down if no response received during some period of time. – Ivan Shatsky Feb 03 '20 at 14:48
  • @IvanShatsky, I got it with location, i.e., a folder e.g. /health. But how or with what form /way i return 200 "OK\n". Or geberally said, what when i want to return as message e.g., "health": "true". – m19v Feb 04 '20 at 15:09
  • You can define any message you want in the `return` directive parameter. What you can't is to receive `"health": "false"` message from non-working nginx (since it isn't working :). I think you need some intermediate script for this, but it is out of the scope of this question. – Ivan Shatsky Feb 06 '20 at 04:26

2 Answers2

25

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:

dzakharov
  • 332
  • 4
  • 8
3

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.

Bioblaze Payne
  • 364
  • 3
  • 10