lua-resty-upstream-healthcheck - Health-checker for Nginx upstream servers
- Name
- Status
- Synopsis
- Description
- Methods
- Installation
- TODO
- Community
- Bugs and Patches
- Author
- Copyright and License
- See Also
This library is still under early development but is already production ready.
http {
lua_package_path "/path/to/lua-resty-upstream-healthcheck/lib/?.lua;;";
# sample upstream block:
upstream foo.com {
server 127.0.0.1:12354;
server 127.0.0.1:12355;
server 127.0.0.1:12356 backup;
}
# the size depends on the number of servers in upstream {}:
lua_shared_dict healthcheck 1m;
lua_socket_log_errors off;
init_worker_by_lua '
local hc = require "resty.upstream.healthcheck"
local ok, err = hc.spawn_checker{
shm = "healthcheck", -- defined by "lua_shared_dict"
upstream = "foo.com", -- defined by "upstream"
type = "http",
-- if you put this Lua snippet in separate .lua file,
-- then you should write this instead: http_req = "GET /status HTTP/1.0\r\nHost: foo.com\r\n\r\n",
http_req = "GET /status HTTP/1.0\\r\\nHost: foo.com\\r\\n\\r\\n",
-- raw HTTP request for checking
interval = 2000, -- run the check cycle every 2 sec
timeout = 1000, -- 1 sec is the timeout for network operations
fall = 3, -- # of successive failures before turning a peer down
rise = 2, -- # of successive successes before turning a peer up
valid_statuses = {200, 302}, -- a list valid HTTP status code
concurrency = 10, -- concurrency level for test requests
}
if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
return
end
-- Just call hc.spawn_checker() for more times here if you have
-- more upstream groups to monitor. One call for one upstream group.
-- They can all share the same shm zone without conflicts but they
-- need a bigger shm zone for obvious reasons.
';
server {
...
# status page for all the peers:
location = /status {
access_log off;
allow 127.0.0.1;
deny all;
default_type text/plain;
content_by_lua '
local hc = require "resty.upstream.healthcheck"
ngx.say("Nginx Worker PID: ", ngx.worker.pid())
ngx.print(hc.status_page())
';
}
}
}
If you are using OpenResty 1.5.11.1 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:
You need to compile both the ngx_lua and ngx_lua_upstream modules into your Nginx.
The latest git master branch of ngx_lua is required.
You need to configure
the lua_package_path directive to
add the path of your lua-resty-upstream-healthcheck
source tree to ngx_lua's Lua module search path, as in
# nginx.conf
http {
lua_package_path "/path/to/lua-resty-upstream-healthcheck/lib/?.lua;;";
...
}
The openresty-en mailing list is for English speakers.
The openresty mailing list is for Chinese speakers.
Please report bugs or submit patches by
- creating a ticket on the GitHub Issue Tracker,
- or posting to the OpenResty community.
Yichun "agentzh" Zhang (章亦春) [email protected], CloudFlare Inc.
This module is licensed under the BSD license.
Copyright (C) 2014, by Yichun "agentzh" Zhang, CloudFlare Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
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.
THIS 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.
- the ngx_lua module: https://github.com/chaoslawful/lua-nginx-module
- the ngx_lua_upstream module: https://github.com/agentzh/lua-upstream-nginx-module
- OpenResty: http://openresty.org