Last active
November 29, 2020 13:34
-
-
Save sabretus/6002af0a9dd3a4401adafacaa67caa7f to your computer and use it in GitHub Desktop.
Advanced Openresty health check for multiple backends using LUA (full example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upstream other_backend { | |
server 127.0.0.1:8080; | |
} | |
upstream main_backend { | |
server 127.0.0.1:80; | |
} | |
server { | |
listen 443 ssl http2; | |
server_name example.com | |
# …some other settings… | |
location / { | |
proxy_pass http://main_backend; | |
} | |
location = /other { | |
proxy_pass http://other_backend; | |
} | |
location = /healthcheck { | |
access_by_lua_block { | |
local http = require "resty.http" | |
local h = http.new() | |
h:set_timeout(2 * 1000) | |
local url = "http://127.0.0.1:18086/ping" | |
local res, err = h:request_uri(url, {method = "GET"}) | |
if err or not res or res.status ~= 204 then | |
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE | |
ngx.exit(ngx.HTTP_OK) | |
end | |
url = "http://127.0.0.1:9096/ping" | |
res, err = h:request_uri(url, {method = "GET"}) | |
if err or not res or res.status ~= 200 then | |
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE | |
end | |
ngx.exit(ngx.HTTP_OK) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment