# /usr/local/openresty/nginx/sbin/nginx -V
nginx version: openresty/1.11.2.5
..
b) In the case of round-robin mechanism, I observed that, the switch over to the next upstream server happens if I include b.set_more_tries(10) before b.set_current_peer() in the balancer_by_lua block.
upstream backend_rr {
server 0.0.0.1;
balancer_by_lua_block {
local b = require "ngx.balancer"
local rr_up = package.loaded.my_rr_up
local server = rr_up:find()
-- Looks like this is needed for switchover to next upstream in case of error
assert(b.set_more_tries(10))
assert(b.set_current_peer(server))
}
}
When I add this set_more_tries() call, if the upstream server 192.168.1.6:8722 gets chosen for serving a request and if NGINX is unable to connect to this server, another server (192.168.1.6:8720 or 192.168.1.6:8721) gets selected, and a 200 OK response is correspondingly obtained. Also, when the server is brought up, it seamlessly gets chosen for the next request.
c) In the case of chash mechanism, the following errors are observed.
c.1) If a similar b.set_more_tries() call is added in the balancer_by_lua in the upstream block, the request corresponding to the server which is down, will take NGINX into some kind of a loop.No response is obtained. The error log file grows up very rapidly.
upstream backend_chash {
server 0.0.0.1;
balancer_by_lua_block {
local b = require "ngx.balancer"
local chash_up = package.loaded.my_chash_up
local servers = package.loaded.my_servers
-- we can balancer by any key here
-- local id = chash_up:find(ngx.var.arg_key)
local id = chash_up:find(ngx.var.request_uri)
local server = servers[id]
-- Looks like this is needed for switchover to next upstream in case of error
assert(b.set_more_tries(10))
assert(b.set_current_peer(server))
}
}
c.2) However, if I also include a proxy_next_upstream_tries 10; (in addition to b.set_more_tries() in lua) also in the location block, a 502 error is immediately obtained.
server {
listen 80;
server_name bbc.com www.bbc.com;
location / {
proxy_pass http://backend_chash;
#proxy_pass http://backend_rr;
proxy_next_upstream error timeout invalid_header http_500 http_502;
proxy_next_upstream_tries 10;
}
}
In round-robin, the b.set_more_tries() helped to handle the connection error and retry successfully with another server. However, with chash, this is not happening.
Thanks
Rajesh
On Monday, October 23, 2017 at 5:11:55 PM UTC+5:30, keeplea...@gmail.com wrote:
Hi,
The servers in my list are:
When all the servers are up and running, both chash and round-robin methods are working correctly.
However, when I take one of the servers down (say the last one,
192.168.1.6:8722), I find that OpenResty reports a connect-failed for that server, and a 502 Bad gateway response is provided.
The config snippets (almost a copy of the synopsis of the above mentioned link, except for the server list) are as follows:
init_by_lua_block {
local resty_chash = require "resty.chash"
local resty_roundrobin = require "resty.roundrobin"
local server_list = {
["192.168.1.6:8720"] = 1,
["192.168.1.6:8721"] = 1,
["192.168.1.6:8722"] = 1
}
-- XX: we can do the following steps to keep consistency with nginx chash
local str_null = string.char(0)
local servers, nodes = {}, {}
for serv, weight in pairs(server_list) do
-- XX: we can just use serv as id when we doesn't need keep consistency with nginx chash
local id = string.gsub(serv, ":", str_null)
servers[id] = serv
nodes[id] = weight
end
local chash_up = resty_chash:new(nodes)
package.loaded.my_chash_up = chash_up
package.loaded.my_servers = servers
local rr_up = resty_roundrobin:new(server_list)
package.loaded.my_rr_up = rr_up
}
upstream backend_chash {
server 0.0.0.1;
balancer_by_lua_block {
local b = require "ngx.balancer"
local chash_up = package.loaded.my_chash_up
local servers = package.loaded.my_servers
-- we can balancer by any key here
local id = chash_up:find(ngx.var.request_uri)
local server = servers[id]
assert(b.set_current_peer(server))
}
}
upstream backend_rr {
server 0.0.0.1;
balancer_by_lua_block {
local b = require "ngx.balancer"
local rr_up = package.loaded.my_rr_up
local server = rr_up:find()
assert(b.set_current_peer(server))
}
}
I am wondering why OpenResty/NGINX is unable to try the request with one of the other two servers which are up and running? Afaik, native NGINX tries to select one of the other remaining upstream servers when it is unable to connect to a specific server.
Could someone please explain difference between "balancer_by_lua" way of selecting an upstream and the native NGINX way?
I have also enabled proxy_next_upstream directive here to handle error (which I believe encompasses connection error).
Thanks
Rajesh