Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ngx.balancer 如何设置重试状态码 #445

Open
yuqiquan opened this issue Nov 10, 2023 · 3 comments
Open

ngx.balancer 如何设置重试状态码 #445

yuqiquan opened this issue Nov 10, 2023 · 3 comments

Comments

@yuqiquan
Copy link

想要使用 ngx.balancer 设置在 404 的时候进行重试,ngx.balancer 支持吗

@tzssangglass
Copy link

I think you can learn form:

lua-resty-core/t/balancer.t

Lines 179 to 230 in 99603e5

=== TEST 5: get last peer failure status (404)
--- skip_nginx: 4: < 1.7.5
--- http_config
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404;
proxy_next_upstream_tries 10;
upstream backend {
server 0.0.0.1;
balancer_by_lua_block {
local b = require "ngx.balancer"
local state, status = b.get_last_failure()
print("last peer failure: ", state, " ", status)
if not ngx.ctx.tries then
ngx.ctx.tries = 0
end
if ngx.ctx.tries < 2 then
local ok, err = b.set_more_tries(1)
if not ok then
return error("failed to set more tries: ", err)
elseif err then
ngx.log(ngx.WARN, "set more tries: ", err)
end
end
ngx.ctx.tries = ngx.ctx.tries + 1
assert(b.set_current_peer("127.0.0.1", tonumber(ngx.var.server_port)))
}
}
--- config
location = /t {
proxy_pass http://backend/back;
}
location = /back {
return 404;
}
--- request
GET /t
--- response_body_like: 404 Not Found
--- error_code: 404
--- grep_error_log eval: qr{last peer failure: \S+ \S+}
--- grep_error_log_out
last peer failure: nil nil
last peer failure: next 404
last peer failure: next 404
--- no_error_log
[warn]

  1. use the get_last_failure to get 404
  2. use set_more_tries to set more retry

just an idea.

@yuqiquan
Copy link
Author

ngx.balancer的重试机制好像只有在机器故障,或者是超时的时候才进行重试

如果机器是正常的,只是返回的状态码异常,是不会触发重试机制的,这个时候 local state, status = b.get_last_failure() 返回的结果是nil,nil

最关键的是如何触发这个重试机制

@tzssangglass
Copy link

最关键的是如何触发这个重试机制

proxy_next_upstream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants