Skip to content

Instantly share code, notes, and snippets.

@RavenZZ
Created August 4, 2016 14:49
Show Gist options
  • Save RavenZZ/f8dd1175f5c7a1b756419de9579d5905 to your computer and use it in GitHub Desktop.
Save RavenZZ/f8dd1175f5c7a1b756419de9579d5905 to your computer and use it in GitHub Desktop.
openresty负载均衡下的特殊应用–特定请求转发所有后端主机
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("nlp")
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.bdoy)
end
end
end
end
end
}
}
location / {
proxy_pass http://nlp;
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment