…ですが、とっても困るのがバックエンドサーバーで50xエラーが出ることがあるんですよね。困りますね。ということで、NGiNX_luaでリトライ処理するサンプルコードを書いてみました!
upstream backend {
server [proxy先URL];
}
server {
listen 80 default;
server_name 127.0.0.1 localhost;
location / {
content_by_lua '
local STR = "/check"..ngx.var.uri
local res = ngx.location.capture(STR)
if res.status == ngx.HTTP_OK then
ngx.say(res.body)
else
ngx.sleep(1)
res = ngx.location.capture("/check")
ngx.say(res.body)
end
';
}
location /check {
internal;
rewrite /check/(.*) /$1 break;
proxy_pass http://backend/;
}
}