Intermediate · 15 min · practise every command in the built-in terminal

🎯 Mission Briefing: 502 Bad Gateway

The shop's page loads, but the product list stays empty: every request to /api/ ends in 502 Bad Gateway. Last night the shop's API, shop-api, was upgraded to version 2.0. A support ticket is waiting for you in ~/ticket-5120.txt.

How the shop works

browser ──► nginx :80 ─┬─► /         files from /var/www/shop
                       └─► /api/…    passed on to shop-api (proxy_pass)

nginx doesn't make the product list itself. Requests for /api/ it passes on to the app behind it — nginx is a reverse proxy here. 502 Bad Gateway means nginx itself is fine, but it got no answer from the app.

Investigation toolkit

curl -I http://localhost/api/products     # What does the API answer?
sudo tail /var/log/nginx/error.log        # nginx says why, and where it tried to connect
systemctl status shop-api                 # Is the app running at all?
sudo ss -tlnp                             # Which port does it really listen on?
cat /etc/nginx/sites-available/shop       # Where does nginx send /api/?

Fix and verify

Make nginx and the app agree on the port: point proxy_pass at the port the app uses now, or set the old port back in the app's configuration.

sudo nginx -t && sudo systemctl reload nginx    # after a change to nginx's configuration
sudo systemctl restart shop-api                 # after a change to the app's configuration
curl http://localhost/api/products

💡 Connection refused in the error log means nothing listens where nginx tried to connect. Compare the upstream: address in the log with what ss -tlnp shows.

Practice tasks

  1. See what the API answers
  2. Find nginx's reason in its error log
  3. Find the port shop-api really listens on
  4. Make nginx and the API agree on the port
  5. Check that the products load again

Commands in this lesson

Missions