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.
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:
location
configuration (mentioned above) to the end of server
block in the file /etc/nginx/sites-available/default
and restart nginx serverhttp://localhost/health
should return response {"status":"UP"}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With