openresty+lua 动态更新upstram里的server (上)----配置upstream和health_check

环境描述:

192.168.0.16 启动一个Apache,访问80端口 返回 192.168.0.16 esbrunning

192.168.0.17 启动一个Apache,访问80端口 返回192.168.0.17 esbrunning

                   启动一个openresty,访问8000端口轮询转发到192.168.016和192.168.0.17的80端口

步骤一:

    (1)192.168.0.16和192.168.0.17启动一个Apache,配置好80端口

    (2)192.168.0.17安装一个openresty,配置server的端口是8000

 listen       8000;
 server_name  192.168.0.17;
user   nobody;
worker_processes  3;

步骤二:

    (1)修改nginx.conf的http模块,添加upstream

    upstream  tomcatproxy {
        server   192.168.0.16:80 weight=1;
        server   192.168.0.17:80 weight=1;
    }

  (2)修改nginx.conf的http模块,添加upstream的检查模块

        lua_shared_dict healthcheck 1m;
        init_worker_by_lua_block {
        local hc = require "resty.upstream.healthcheck"

        local ok, err = hc.spawn_checker{
            shm = "healthcheck",  -- defined by "lua_shared_dict"
            upstream = "tomcatproxy", -- defined by "upstream"
            type = "http",

            http_req = "GET /index.html HTTP/1.0\r\nconnection: keep-alive\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
    }

(3)修改nginx.conf的server模块

    location / {
            proxy_pass http://tomcatproxy/;
            proxy_set_header     x-forward-for  $proxy_add_x_forwarded_for;
               }

(4)重启openresty

步骤三:

    (1)测试一下转发功能

     

openresty+lua 动态更新upstram里的server (上)----配置upstream和health_check_第1张图片

请到下篇进行lua脚本配置,https://blog.csdn.net/u014686399/article/details/80226161

如果有不清楚的请到 630300475qq群。

你可能感兴趣的:(openresty)