Debugging HTTP with curl
Most server traffic is HTTP, and most "the site is broken" tickets end in a curl command run on or near the server. curl & wget covered downloading; here curl is a diagnostic instrument: talking to one specific backend, pretending to be a given host name, timing each phase, and reading the status codes a reverse proxy produces.
The setup
Every session in this lesson starts the same small stack on loopback: a Python app on port 9000, and Nginx on port 8080 with two virtual hosts, a redirect and a route to a backend that is down. (The next section explains the Nginx config in detail.)
mkdir -p app && echo '{"status":"ok"}' > app/health
python3 -m http.server 9000 --bind 127.0.0.1 --directory app > app.log 2>&1 &
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log error.log;
events {}
http {
access_log off;
server {
listen 127.0.0.1:8080;
server_name shop.example.test;
location / { proxy_pass http://127.0.0.1:9000; }
location /old { return 301 /health; }
location /orders { proxy_pass http://127.0.0.1:9001; } # nothing listens on 9001
}
server {
listen 127.0.0.1:8080 default_server;
server_name _;
return 404 "unknown site\n";
}
}
EOF
for i in $(seq 50); do curl -s 127.0.0.1:9000/health > /dev/null && break; sleep 0.1; done
nginx -p "$PWD" -c nginx.conf -e stderr
H="Host: shop.example.test"
echo "--- 1. without the right Host header, the default server answers:"
curl -s http://127.0.0.1:8080/health
echo "--- 2. with it:"
curl -s -H "$H" http://127.0.0.1:8080/health
echo "--- 3. --resolve: use the real URL, but connect to this address:"
curl -s --resolve shop.example.test:8080:127.0.0.1 http://shop.example.test:8080/health
echo "--- 4. status codes only:"
for path in /health /old /missing /orders; do
curl -s -o /dev/null -H "$H" -w "%{http_code} $path\n" "http://127.0.0.1:8080$path"
done
echo "--- 5. following the redirect:"
curl -s -L --resolve shop.example.test:8080:127.0.0.1 -w " (redirects followed: %{num_redirects}, ended at %{url_effective})\n" http://shop.example.test:8080/old
echo "--- 6. why 502? nginx's error log says:"
grep -o 'connect() failed.*upstream: "[^"]*"' error.log
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null
kill %1--- 1. without the right Host header, the default server answers:
unknown site
--- 2. with it:
{"status":"ok"}
--- 3. --resolve: use the real URL, but connect to this address:
{"status":"ok"}
--- 4. status codes only:
200 /health
301 /old
404 /missing
502 /orders
--- 5. following the redirect:
{"status":"ok"}
(redirects followed: 1, ended at http://shop.example.test:8080/health)
--- 6. why 502? nginx's error log says:
connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: shop.example.test, request: "GET /orders HTTP/1.1", upstream: "http://127.0.0.1:9001/orders"
What the session teaches
- Virtual hosts are chosen by the
Hostheader. Testinghttp://127.0.0.1/hits whatever server is the default, which is often not the site you mean. Use-H "Host: …"for plain HTTP. --resolve host:port:addressis better: the URL stays exactly what users type, so the Host header, and for HTTPS the SNI name and certificate check, are all correct, while the connection goes to the server you choose. That's how you test a new server before switching DNS, or one backend behind a load balancer.- Status codes tell you who failed (table below).
-Lfollows redirects;-w '%{num_redirects}'and%{url_effective}show where you ended up. The redirect here points to the full URLhttp://shop.example.test:8080/health, which is why the session follows it with--resolve: a hand-setHostheader would not lead curl to the right address. Redirect loops (http→https→http) show up ascurl: (47) Maximum (50) redirects followed.- The proxy's error log names the upstream and the reason: here "Connection refused" to
127.0.0.1:9001, i.e. the backend process isn't running.
| Code | From a reverse proxy, usually means |
|---|---|
| 301/302/307/308 | a redirect rule (check Location: with curl -sI) |
| 400 | malformed request; with TLS, often "plain HTTP sent to an HTTPS port" |
| 403 | the proxy refused (allow/deny rule, file permissions on static files) |
| 404 | no such route or file, or the wrong virtual host answered |
| 413 | request body larger than client_max_body_size (Nginx default: 1 MB) |
| 502 Bad Gateway | the proxy couldn't talk to the backend: refused, reset, or an invalid response |
| 503 | no healthy backend, or rate limiting |
| 504 Gateway Timeout | the backend accepted the connection but didn't answer in time (proxy_read_timeout, default 60 s) |
Timing each phase
-w can print curl's timers: DNS lookup, TCP connect, TLS handshake, time to first byte and total. They show whether slowness is the network, TLS or the application (sample session against a real site; the numbers are seconds since the start):
$ curl -s -o /dev/null -w 'dns %{time_namelookup} connect %{time_connect} tls %{time_appconnect} ttfb %{time_starttransfer} total %{time_total}\n' https://shop.example.com/api/products
dns 0.012 connect 0.031 tls 0.074 ttfb 1.912 total 1.954DNS, TCP and TLS together took 74 ms; the remaining 1.8 s before the first byte is the application (or the proxy waiting for it). Put the format in a file (-w @timing.txt) and keep it around.
Headers, verbose mode and HTTP/2
| Need | Command |
|---|---|
| response headers only | curl -sI URL (a HEAD request; some apps answer HEAD differently, so curl -s -D - -o /dev/null URL uses GET) |
| the whole conversation | curl -v URL: > lines are sent, < received, * curl's own notes (resolved address, TLS version, certificate) |
| force a protocol | --http1.1, --http2, --http3 (if curl was built with it) |
| send JSON | curl -s -X POST -H 'Content-Type: application/json' -d '{"sku":42}' URL |
| ignore the proxy from the environment | --noproxy '*' (an https_proxy variable on the server explains many surprises) |
| fail scripts on HTTP errors | curl -fsS URL (-f makes 4xx/5xx a non-zero exit code; --fail-with-body keeps the body) |
Test yourself
Users get 502 Bad Gateway from Nginx. Where do you look first?
- The browser cache
- Nginx's error log for the upstream address and reason, then whether the backend process is running and listening
- DNS
- The TLS certificate
Why is --resolve shop.example.com:443:10.0.0.21 better than -H 'Host: shop.example.com' for testing a new HTTPS server?
- It is shorter
- The URL keeps the real name, so SNI and the certificate check use it too, while the connection goes to the chosen address
- It disables TLS
- It follows redirects
What is the difference between 502 and 504 at a reverse proxy?
- None
- 502: the backend couldn't be reached or answered invalidly; 504: it was reached but didn't answer before the proxy's timeout
- 502 is a client error
- 504 means DNS failed