openrestry 後端服務upstream 健康檢查配置

openrestry 後端服務upstream 健康檢查配置

 http {
 # 在http模塊中增加以下配置
        lua_package_path "/usr/local/openresty/lualib/resty/?.lua;/usr/local/openresty/lualib/resty/upstream/?.lua;;";
        lua_shared_dict healthcheck 1m;
        lua_socket_log_errors off;
        init_worker_by_lua_block {
        local hc = require "resty.upstream.healthcheck"
        local ok, err = hc.spawn_checker{
            shm = "healthcheck",
            upstream = "xx-tomcat",
            type = "http",
            http_req = "GET / HTTP/1.0\r\nHost: xx-tomcat\r\n\r\n",
            interval = 2000,
            timeout = 1000,
            fall = 3,
            rise = 2,
            valid_statuses = {200, 302},
            concurrency = 10,
        }

        if not ok then
            ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
            return
        end
        }
 }

# 在server中增加一個location
    location /ngx_stat {
        allow 103.101.204.84;
        deny all;
        access_log off;
        default_type text/plain;
        content_by_lua_block {
            local hc = require "resty.upstream.healthcheck"
            ngx.say("Nginx Worker PID: ", ngx.worker.pid())
            ngx.print(hc.status_page())
        }
    }

配置完成後重新加載配置文件使之生效,瀏覽器訪問http://test.0xsyshell.com/ngx_stat
效果如下:

Nginx Worker PID: 18561
Upstream xx-tomcat
    Primary Peers
        172.31.16.14:8080 up
        172.31.16.15:8080 up
    Backup Peers
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章