Engineering Notes
Production Debugging3 min readUpdated

When a Next.js Page Loads but Its Django API Call Fails

Tracing an internal API timeout and HTTP 400 through the browser, proxy, Docker network, and Django host validation.

Client Web PlatformNext.jsDjangoDockerNginxNetworkingDebugging
On this page

One production issue I investigated on a client web platform is a good example of why I do not trust the homepage to tell me whether a deployment is healthy.

The stack was Next.js, Django, Docker, and Nginx, with supporting database and Redis services. The public site returned a successful response, but some server-side requests from Next.js to the Django API were failing. The confusing part was that different tests seemed to give different answers.

Three results that looked contradictory

  • The public blog page returned HTTP 200.
  • A request made from inside the frontend container to http://backend:8000/api/... failed with a connection timeout.
  • Other tests got HTTP 400 from the backend, and the frontend logs reported Blog API returned 400.

These are three different situations, and I treated them separately:

ResultWhat it actually tells me
HTTP 200 for the pageThe public route responded. It says nothing about whether the page's data calls succeeded.
Connection timeoutThe client never established a connection within the expected time.
HTTP 400The request reached an HTTP service, and that service rejected it.

Following the request path

For this deployment, Nginx routed public traffic to the containers, while the frontend used Docker's internal network for its server-side API calls.

  1. Public browser
  2. HTTPS / Nginx
  3. Next.js container
  4. Internal Docker network: http://backend:8000/api
  5. Django container
  6. Database
Public traffic and the internal API call

The first job was to work out which layer had the problem: Nginx, Next.js, Docker networking, Django, or the database.

A public curl tells me about the public route. It cannot prove that the hostname backend is reachable from inside a container. So I ran the test from the frontend container itself:

# Example structure; use your actual container name and endpoint.
docker exec FRONTEND_CONTAINER node -e '
fetch("http://backend:8000/api/health/")
  .then(r => console.log(r.status))
  .catch(console.error)
'

For network problems, where a request is sent from matters as much as the URL it is sent to.

A separate problem: host validation

While checking the backend configuration, I found that Django's ALLOWED_HOSTS included the public domain and local hostnames, but not the internal service name backend.

That matters because a request sent to http://backend:8000 normally carries Host: backend:8000. If Django rejects that host, it returns HTTP 400 even when the container is reachable.

This was a credible cause of the 400 responses. It did not explain every timeout, though. A timeout needs its own investigation: Docker networks, service readiness, routing, DNS resolution, and connection behavior.

For the fix direction, I prefer adding the internal hostname explicitly over using a wildcard in ALLOWED_HOSTS. I also check trusted-proxy settings separately, instead of turning off host validation to make the error go away.

How I debug this now

I stopped treating it as one generic "the API is not working" problem and split the checks by layer:

  1. Public route: does Nginx return a response for the domain?
  2. Frontend runtime: which API base URL is Next.js actually using on the server?
  3. Container network: can the frontend container resolve and connect to backend?
  4. Django validation: does the request's host pass ALLOWED_HOSTS?
  5. API route: is the path valid, and what status does Django return?
  6. Page behavior: does the page render real data, or only a fallback?

Sometimes the most useful debugging tool is not another package or configuration change. It is a correctly placed request, sent from the right container. Test the network path your application actually uses, and do not confuse a page returning 200 with a healthy data flow. I learned a similar lesson about health checks during a media migration where the container was running but the files were missing.

Related reading