Skip to the page

Nginx and API gateways · core

What Nginx does in front of your app, and how proxying fails

Nginx is a reverse proxy: it ends TLS, buffers slow clients, routes by host and path and balances load. Many production bugs come from DNS caching, certificate chains and proxy headers.

The notes

What you must know

  • A forward proxy acts for clients. A reverse proxy acts for servers, so clients see one address. Jobs: TLS termination, load balancing, routing by host and path, static files, compression, caching, size and rate limits, hiding internal errors.
  • Buffering: Nginx reads slow clients' requests and responses fully, so an app worker is busy only for the processing time. This protects synchronous servers from slow clients.
  • Pass the real client: Host, X-Forwarded-For ($proxy_add_x_forwarded_for) and X-Forwarded-Proto. Behind a load balancer, the app sees the balancer address unless you use the real IP module.
  • Nginx resolves a hostname in proxy_pass or an upstream block once, at start or reload. If the backend addresses change, Nginx keeps connecting to the old servers. curl looks up the name fresh, so it works from the shell.
  • Errors: connection refused gives 502; a timeout gives 504 after proxy_connect_timeout (60 seconds by default). The error log says upstream timed out (110: Connection timed out) while connecting to upstream, with the old IP.
  • Fixes for DNS: a resolver directive with a variable in proxy_pass, the resolve parameter on upstream servers (zone required; open-source support in recent versions), or reload Nginx after a deploy that changes addresses.
  • Certificate chain: ssl_certificate must point to the full chain (leaf plus intermediates), for example fullchain.pem. With the leaf only, desktop Chrome can fetch the missing intermediate, but Android, curl and Java clients fail.
  • Check and reload: nginx -t, then nginx -s reload (graceful). Check from outside with openssl s_client -connect host:443 -servername host -showcerts.

The red pen

Common traps: mistake → fix

  • Mistake: curl works so Nginx can reach the backend

    Fix: Nginx cached the old IP at start. curl resolves again every time.

  • Mistake: It works in Chrome so the certificate is fine

    Fix: Chrome fetches missing intermediates. Android and most server clients do not.

  • Mistake: 502 and 504 are the same

    Fix: 502 is a bad or refused upstream answer, 504 is a timeout.

Easy to hard

Questions interviewers ask, easy to hard

  1. What does a reverse proxy do that your app should not?
  2. Why does Nginx keep sending traffic to an old server after a deploy?
  3. What is the difference between 502 and 504?
  4. An Android app fails TLS after a certificate renewal, but Chrome works. Why?
  5. Which headers must the app receive when it runs behind Nginx and a load balancer?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview