{"id":13608415,"url":"https://github.com/openresty/lua-resty-upstream-healthcheck","last_synced_at":"2025-04-12T17:31:30.471Z","repository":{"id":13578212,"uuid":"16270774","full_name":"openresty/lua-resty-upstream-healthcheck","owner":"openresty","description":"Health Checker for Nginx Upstream Servers in Pure Lua","archived":false,"fork":false,"pushed_at":"2023-12-27T02:37:00.000Z","size":92,"stargazers_count":504,"open_issues_count":47,"forks_count":138,"subscribers_count":43,"default_branch":"master","last_synced_at":"2024-02-13T20:28:04.538Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openresty.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-01-27T06:04:16.000Z","updated_at":"2024-01-30T08:04:42.000Z","dependencies_parsed_at":"2024-01-06T22:29:55.837Z","dependency_job_id":"97c9a866-750c-4c89-b460-9af1ebffceab","html_url":"https://github.com/openresty/lua-resty-upstream-healthcheck","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-upstream-healthcheck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-upstream-healthcheck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-upstream-healthcheck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-upstream-healthcheck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openresty","download_url":"https://codeload.github.com/openresty/lua-resty-upstream-healthcheck/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248605081,"owners_count":21132106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T19:01:27.077Z","updated_at":"2025-04-12T17:31:25.461Z","avatar_url":"https://github.com/openresty.png","language":"Lua","funding_links":[],"categories":["Lua","Libraries","Third Modules","Rust Modules"],"sub_categories":["C Modules","Lua Modules"],"readme":"Name\n====\n\nlua-resty-upstream-healthcheck - Health-checker for Nginx upstream servers\n\nTable of Contents\n=================\n\n- [Name](#name)\n- [Table of Contents](#table-of-contents)\n- [Status](#status)\n- [Synopsis](#synopsis)\n- [Description](#description)\n- [Methods](#methods)\n  - [spawn_checker](#spawn_checker)\n  - [status_page](#status_page)\n- [Multiple Upstreams](#multiple-upstreams)\n- [Installation](#installation)\n- [TODO](#todo)\n- [Community](#community)\n  - [Contributing](#contributing)\n  - [English Mailing List](#english-mailing-list)\n  - [Chinese Mailing List](#chinese-mailing-list)\n- [Bugs and Patches](#bugs-and-patches)\n- [Author](#author)\n- [Copyright and License](#copyright-and-license)\n- [See Also](#see-also)\n\nStatus\n======\n\nThis library is still under early development but is already production ready.\n\nSynopsis\n========\n\n```nginx\nhttp {\n    lua_package_path \"/path/to/lua-resty-upstream-healthcheck/lib/?.lua;;\";\n\n    # sample upstream block:\n    upstream foo.com {\n        server 127.0.0.1:12354;\n        server 127.0.0.1:12355;\n        server 127.0.0.1:12356 backup;\n    }\n\n    # the size depends on the number of servers in upstream {}:\n    lua_shared_dict healthcheck 1m;\n\n    lua_socket_log_errors off;\n\n    init_worker_by_lua_block {\n        local hc = require \"resty.upstream.healthcheck\"\n\n        local ok, err = hc.spawn_checker{\n            shm = \"healthcheck\",  -- defined by \"lua_shared_dict\"\n            upstream = \"foo.com\", -- defined by \"upstream\"\n            type = \"http\", -- support \"http\" and \"https\"\n\n            http_req = \"GET /status HTTP/1.0\\r\\nHost: foo.com\\r\\n\\r\\n\",\n                    -- raw HTTP request for checking\n\n            port = nil,  -- the check port, it can be different than the original backend server port, default means the same as the original backend server\n            interval = 2000,  -- run the check cycle every 2 sec\n            timeout = 1000,   -- 1 sec is the timeout for network operations\n            fall = 3,  -- # of successive failures before turning a peer down\n            rise = 2,  -- # of successive successes before turning a peer up\n            valid_statuses = {200, 302},  -- a list valid HTTP status code\n            concurrency = 10,  -- concurrency level for test requests\n            -- ssl_verify = true, -- https type only, verify ssl certificate or not, default true\n            -- host = foo.com, -- https type only, host name in ssl handshake, default nil\n        }\n        if not ok then\n            ngx.log(ngx.ERR, \"failed to spawn health checker: \", err)\n            return\n        end\n\n        -- Just call hc.spawn_checker() for more times here if you have\n        -- more upstream groups to monitor. One call for one upstream group.\n        -- They can all share the same shm zone without conflicts but they\n        -- need a bigger shm zone for obvious reasons.\n    }\n\n    server {\n        ...\n\n        # status page for all the peers:\n        location = /status {\n            access_log off;\n            allow 127.0.0.1;\n            deny all;\n\n            default_type text/plain;\n            content_by_lua_block {\n                local hc = require \"resty.upstream.healthcheck\"\n                ngx.say(\"Nginx Worker PID: \", ngx.worker.pid())\n                ngx.print(hc.status_page())\n            }\n        }\n\n\t# status page for all the peers (prometheus format):\n        location = /metrics {\n            access_log off;\n            default_type text/plain;\n            content_by_lua_block {\n                local hc = require \"resty.upstream.healthcheck\"\n                st , err = hc.prometheus_status_page()\n                if not st then\n                    ngx.say(err)\n                    return\n                end\n                ngx.print(st)\n            }\n        }\n    }\n}\n```\n\nDescription\n===========\n\nThis library performs healthcheck for server peers defined in NGINX `upstream` groups specified by names.\n\n[Back to TOC](#table-of-contents)\n\nMethods\n=======\n\nspawn_checker\n-------------\n**syntax:** `ok, err = healthcheck.spawn_checker(options)`\n\n**context:** *init_worker_by_lua\u0026#42;*\n\nSpawns background timer-based \"light threads\" to perform periodic healthchecks on\nthe specified NGINX upstream group with the specified shm storage.\n\nThe healthchecker does not need any client traffic to function. The checks are performed actively\nand periodically.\n\nThis method call is asynchronous and returns immediately.\n\nReturns true on success, or `nil` and a string describing an error otherwise.\n\n[Back to TOC](#table-of-contents)\n\nstatus_page\n-----------\n**syntax:** `str, err = healthcheck.status_page()`\n\n**context:** *any*\n\nGenerates a detailed status report for all the upstreams defined in the current NGINX server.\n\nOne typical output is\n\n```\nUpstream foo.com\n    Primary Peers\n        127.0.0.1:12354 UP\n        127.0.0.1:12355 DOWN\n    Backup Peers\n        127.0.0.1:12356 UP\n\nUpstream bar.com\n    Primary Peers\n        127.0.0.1:12354 UP\n        127.0.0.1:12355 DOWN\n        127.0.0.1:12357 DOWN\n    Backup Peers\n        127.0.0.1:12356 UP\n```\n\nIf an upstream has no health checkers, then it will be marked by `(NO checkers)`, as in\n\n```\nUpstream foo.com (NO checkers)\n    Primary Peers\n        127.0.0.1:12354 UP\n        127.0.0.1:12355 UP\n    Backup Peers\n        127.0.0.1:12356 UP\n```\n\nIf you indeed have spawned a healthchecker in `init_worker_by_lua*`, then you should really\ncheck out the NGINX error log file to see if there is any fatal errors aborting the healthchecker threads.\n\n[Back to TOC](#table-of-contents)\n\nMultiple Upstreams\n==================\n\nOne can perform healthchecks on multiple `upstream` groups by calling the [spawn_checker](#spawn_checker) method\nmultiple times in the `init_worker_by_lua*` handler. For example,\n\n```nginx\nupstream foo {\n    ...\n}\n\nupstream bar {\n    ...\n}\n\nlua_shared_dict healthcheck 1m;\n\nlua_socket_log_errors off;\n\ninit_worker_by_lua_block {\n    local hc = require \"resty.upstream.healthcheck\"\n\n    local ok, err = hc.spawn_checker{\n        shm = \"healthcheck\",\n        upstream = \"foo\",\n        ...\n    }\n\n    ...\n\n    ok, err = hc.spawn_checker{\n        shm = \"healthcheck\",\n        upstream = \"bar\",\n        ...\n    }\n}\n```\n\nDifferent upstreams' healthcheckers use different keys (by always prefixing the keys with the\nupstream name), so sharing a single `lua_shared_dict` among multiple checkers should not have\nany issues at all. But you need to compensate the size of the shared dict for multiple users (i.e., multiple checkers).\nIf you have many upstreams (thousands or even more), then it is more optimal to use separate shm zones\nfor each (group) of the upstreams.\n\n[Back to TOC](#table-of-contents)\n\nInstallation\n============\n\nIf you are using [OpenResty](http://openresty.org) 1.9.3.2 or later, then you should already have this library (and all of its dependencies) installed by default (and this is also the recommended way of using this library). Otherwise continue reading:\n\nYou need to compile both the [ngx_lua](https://github.com/openresty/lua-nginx-module) and [ngx_lua_upstream](https://github.com/openresty/lua-upstream-nginx-module) modules into your Nginx.\n\nThe latest git master branch of [ngx_lua](https://github.com/openresty/lua-nginx-module) is required.\n\nYou need to configure\nthe [lua_package_path](https://github.com/openresty/lua-nginx-module#lua_package_path) directive to\nadd the path of your `lua-resty-upstream-healthcheck` source tree to [ngx_lua](https://github.com/openresty/lua-nginx-module)'s Lua module search path, as in\n\n```nginx\n# nginx.conf\nhttp {\n    lua_package_path \"/path/to/lua-resty-upstream-healthcheck/lib/?.lua;;\";\n    ...\n}\n```\n\n[Back to TOC](#table-of-contents)\n\nTODO\n====\n\n[Back to TOC](#table-of-contents)\n\nCommunity\n=========\n\n[Back to TOC](#table-of-contents)\n\nContributing\n--------------------\n\nUse `make lint` to lint the code before you open a PR. This uses the widely used [LuaFormatter](https://github.com/Koihik/LuaFormatter).\n\nThe code style is described in the [`.lua-format`](.lua-format) file.\\\nIf you are using VS Code, you can install the wrapper for that formatter by clicking [here](vscode:extension/Koihik.vscode-lua-format).\n\n[Back to TOC](#table-of-contents)\n\nEnglish Mailing List\n--------------------\n\nThe [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers.\n\n[Back to TOC](#table-of-contents)\n\nChinese Mailing List\n--------------------\n\nThe [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers.\n\n[Back to TOC](#table-of-contents)\n\nBugs and Patches\n================\n\nPlease report bugs or submit patches by\n\n1. creating a ticket on the [GitHub Issue Tracker](http://github.com/openresty/lua-resty-upstream-healthcheck/issues),\n1. or posting to the [OpenResty community](#community).\n\n[Back to TOC](#table-of-contents)\n\nAuthor\n======\n\nYichun \"agentzh\" Zhang (章亦春) \u003cagentzh@gmail.com\u003e, OpenResty Inc.\n\n[Back to TOC](#table-of-contents)\n\nCopyright and License\n=====================\n\nThis module is licensed under the BSD license.\n\nCopyright (C) 2014-2017, by Yichun \"agentzh\" Zhang, OpenResty Inc.\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n[Back to TOC](#table-of-contents)\n\nSee Also\n========\n* the ngx_lua module: https://github.com/openresty/lua-nginx-module\n* the ngx_lua_upstream module: https://github.com/openresty/lua-upstream-nginx-module\n* OpenResty: http://openresty.org\n\n[Back to TOC](#table-of-contents)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-resty-upstream-healthcheck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenresty%2Flua-resty-upstream-healthcheck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-resty-upstream-healthcheck/lists"}