Skip to content

Instantly share code, notes, and snippets.

@vdel26
Last active February 20, 2016 00:18
Show Gist options
  • Save vdel26/658344df055249eb9cb4 to your computer and use it in GitHub Desktop.
Save vdel26/658344df055249eb9cb4 to your computer and use it in GitHub Desktop.
Bypass 3scale in case it is unreachable
http {
# add this near the top of the HTTP section
lua_shared_dict healthcheck 1m;
lua_socket_log_errors off;
# healthchecks are HTTP only
upstream threescale_healthcheck {
server su1.3scale.net:80;
}
# healthcheck worker
init_worker_by_lua '
local hc = require "resty.upstream.healthcheck"
local ok, err = hc.spawn_checker{
shm = "healthcheck",
upstream = "threescale_healthcheck",
type = "http",
http_req = "GET /status HTTP/1.0\\r\\nHost: su1.3scale.net\\r\\n\\r\\n",
interval = 2000, -- run the check cycle every 2 sec
timeout = 5000, -- 5 sec is the timeout for network operations
fall = 2, -- # 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 = 1, -- concurrency level for test requests
}
if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
return
end
';
location = /out_of_band_authrep_action {
# wrap the code in the content_by_lua in a conditional
# like in the example below
content_by_lua '
if ngx.var.bypass ~= "true" then
-- previous content of the "content_by_lua"
end
';
}
}
-- Modifications to the nginx.lua file
--------------------------------------
-- require ngx.upstream module, included in openresty
local upstream = require "ngx.upstream"
-----------------------------
-- healthcheck helper
local function is_3scale_up()
local peers, err = upstream.get_primary_peers("threescale_healthcheck")
local peer = peers[1]
if peer.down then
ngx.log(ngx.WARN, "3scale is unreachable now")
return false
else
ngx.log(ngx.INFO, "3scale is UP")
return true
end
end
-----------------------------
-- do a search for the following line "if is_known ~= 200 then"
-- and add the following piece of code below every ocurrence
if not is_3scale_up() then
ngx.var.bypass = "true"
return
end
@vdel26
Copy link
Author

vdel26 commented Feb 18, 2016

How to modify your configuration files to use this snippet:

In the nginx.conf:

  • add the following new lines to the http section
  • modify the out_of_band location or locations to wrap their contents in a conditional like here

In the nginx.lua:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment