openresty负载均衡下的特殊应用–特定请求转发所有后端主机

一般的负载均衡是将请求转发给其中的一个后端主机,而我们想要实现的是将特定请求转发给所有的后端主机。

这种场景什么时候会用呢,比如:更新缓存,刷新配置等。

需要用的openresty模块

https://github.com/openresty/lua-upstream-nginx-module    

https://github.com/pintsized/lua-resty-http

配置文件大致如下:

upstream weblist {
    server 192.168.1.2:80 weight=1;
    server 192.168.1.3:80 weight=1;
}

server {
        listen    8080 default_server;
        server_name  _;
        location /cache {
                default_type text/html;
                content_by_lua_block {
                        local http = require "resty.http"
                        local httpc = http.new()
                        local upstream = require "ngx.upstream"
                        local get_servers = upstream.get_servers
                        local srvs, err = get_servers("weblist")
                        if not srvs then
                                ngx.say("failed to get servers in upstream ", u)
                        else
                                for _, srv in ipairs(srvs) do
                                        for k, v in pairs(srv) do
                                                        if k== "addr" then
                                                              ngx.req.read_body()
                                                              local res,err
                                                              res, err = httpc:request_uri("http://"..v.."/cache" , {
                                                                                                method = ngx.var.request_method,
                                                                                                body = ngx.var.request_body
                                                                                                })
                                                               
                                                              if res.status == ngx.HTTP_OK then
                                                                  ngx.say(res.body)
                                                              end
                                                        end
                                        end
                                end
                        end
                }
        }

        location / {
                proxy_pass http://weblist;
                proxy_redirect          off;
                client_max_body_size    100m;
                client_body_buffer_size 128k;
                proxy_ignore_client_abort on;
                proxy_connect_timeout   900;
                proxy_send_timeout      900;
                proxy_read_timeout      900;
                proxy_buffer_size       4k;
                proxy_buffers           4 32k;
                proxy_busy_buffers_size 64k;
                proxy_temp_file_write_size 64k;
                proxy_next_upstream http_500 http_502 http_503 http_504;
        }
 }

这只是一个简单的实现思路和方法 ,并没有解决一些细节问题(比如:请求的参数处理,响应的合并处理)。

此条目发表在 好文推荐, 网站开发, 网站架构 分类目录,贴了 标签。将固定链接加入收藏夹。

评论功能已关闭。