0

I have the following nginx config which purpose is to check connectivity to upstream:

location @error {
    echo "problems";
}
location /test {
    proxy_pass http://2.2.2.2:2222/; # invalid upstream
    proxy_intercept_errors off;
    error_page 502 =200 @error;
}
location /test2 {
    content_by_lua_block {
        local res = ngx.location.capture("/test")
        ngx.log(ngx.STDERR, res.body)
        ngx.log(ngx.STDERR, res.status)
        ngx.say(res.body)
    }
}

When accessing /test, I get: in the response body "problems" from @error as expected.

But when accessing /test2, I get an empty response body.

error.log:

 *1 connect() failed (101: Network is unreachable) while connecting to upstream, client: ::1, server: , request: "GET /test2 HTTP/1.1", subrequest: "/test", upstream: "http://2.2.2.2:2222/", host: "localhost"
content_by_lua(default:41):3:  while sending to client, client: ::1, server: , request: "GET /test2 HTTP/1.1", host: "localhost"
content_by_lua(default:41):4: 502 while sending to client, client: ::1, server: , request: "GET /test2 HTTP/1.1", host: "localhost"

Why didn't error_page catch 502 in this case?

p.s. This question is related to How to check that the ability to connect to upstream, ignoring 5xx? but is not a copy of it

deevroman
  • 99
  • 1
  • 2
  • 14

0 Answers0