Advanced health check for Openresty (Nginx) backend
At Quiq we are using Openresty as a replacement for the Nginx web server. This is an open-source Web Platform which extends Nginx with Lua scripting language.
I have faced an interesting case. Let’s say there are two upstream (backend) servers. One is main and used to serve traffic and another one is used by a separate “server” block for other purposes. Both backends are listening on different ports on localhost together with Openresty server. In order to send traffic to the main upstream, I need to know the health status of both upstreams. In case when either first or second is down, I need Openresty to return 503.
There is an advanced library for doing health checks https://github.com/openresty/lua-resty-upstream-healthcheck and while I figured out how to use this advanced library, I came to a more simple and elegant solution — use direct LUA in access_by_lua_block
So here I used core resty.http library and simple GET method to get the status of the server. The main backend must return HTTP 200 and “other” backend HTTP 204. As simple as that.
The final nginx.conf would look like this:
Worth to mention, Nginx itself has two built-in types of health checks for upstreams. Active and passive https://docs.nginx.com/nginx/admin-guide/load-balancer/http-health-check/ Passive health check is not quite what I needed and active health check is available only in Nginx Plus version