The Mystery of the 404: Debugging Nginx and Next.js Routing Conflicts

Jee-eun Kang
Jee-eun Kang March 1, 2025

The Case of the Missing Page

As a frontend developer dipping my toes into DevOps, I encountered a puzzling scenario:

  • ✅ Feature worked perfectly in development
  • ✅ Local production builds worked fine
  • ❌ Production deployment returned 404 errors

Initial Investigation

  1. Verified the build: Rebuilt locally - still working
  2. Checked server logs: Requests weren’t reaching Next.js server
  3. Suspected Nginx: The common denominator between working/non-working environments

The Root Cause

The issue stemmed from:

  • Next.js route: /pseudo-route
  • API endpoint: /api/pseudo-route

A security rule in Nginx was blocking both due to similar path patterns.

Technical Deep Dive

Problematic Nginx Configuration

location pseudo-route/ {  # Overly broad match
   return 404;
}

The Solution

  1. Made the Nginx rule more specific:
location = /api/pseudo-route {  # Exact match only
   # API-specific rules
}
  1. Added proper routing for Next.js pages:
location / {
    proxy_pass http://nextjs_server;
    # Other proxy settings...
}

Key Takeaways

➡️ Be specific with Nginx location matching ➡️ Test routing in production-like environments ➡️ Check proxy passes when using application servers ➡️ Document special routes for future maintenance

This experience taught me the importance of understanding how frontend and infrastructure configurations interact in production environments.