1

I have a very simple config:

server {
    listen 80;
    server_name: example.fr;

    location / {
        proxy_pass http://localhost:8000;
    }
}

However when I restart my backend (for example Node.JS restarted on change by nodemon), even if the backend starts like in 2 seconds, Nginx returns a 502 for 10s and this shows in the logs:

2022/05/10 16:10:48 [error] 2013398#2013398: *2241 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://127.0.0.1:8000/", host: "example.fr"
2022/05/10 16:10:48 [error] 2013398#2013398: *2241 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://[::1]:8000/", host: "example.fr"
2022/05/10 16:10:49 [error] 2013398#2013398: *2244 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"
2022/05/10 16:10:50 [error] 2013398#2013398: *2245 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"
2022/05/10 16:10:51 [error] 2013398#2013398: *2246 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"
2022/05/10 16:10:52 [error] 2013398#2013398: *2247 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"
2022/05/10 16:10:53 [error] 2013398#2013398: *2248 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"
2022/05/10 16:10:54 [error] 2013398#2013398: *2249 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"
2022/05/10 16:10:55 [error] 2013398#2013398: *2250 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"
2022/05/10 16:10:57 [error] 2013398#2013398: *2251 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"
2022/05/10 16:10:58 [error] 2013398#2013398: *2252 no live upstreams while connecting to upstream, client: 127.0.0.1, server: example.fr, request: "HEAD / HTTP/2.0", upstream: "http://localhost/", host: "example.fr"

I've seen that for the upstream directive there are some controls (max_fails and fail_timeout (http://nginx.org/en/docs/http/ngx_http_upstream_module.html#max_fails)) but this only seems to apply to upstream and I cannot find equivalent options for plain proxy_pass.

Any idea?

Antoine
  • 3,880
  • 2
  • 26
  • 44

1 Answers1

2

Short answer: Replace localhost with 127.0.0.1.

Quoting user @rogerdpack for the long answer:

The really tricky part is that if you specify proxy_pass to "localhost" and your box happens to also have ipv6 and ipv4 "versions of localhost" on it at the same time (most boxes do by default), it will count as if you had a "list" of multiple servers in your server group, which means you can get into the situation above of having it return "502 for 10s" even though you list only one server.

See here "If a domain name resolves to several addresses, all of them will be used in a round-robin fashion." One workaround is to declare it as proxy_pass http://127.0.0.1:8000; (its ipv4 address) to avoid it being both ipv6 and ipv4. Then it counts as "only a single server" behavior.

(answering my own question because the answers explaining the problem were buried or the question wasn't really explicit about the particular symptoms I had)

Antoine
  • 3,880
  • 2
  • 26
  • 44