Skip to main content

2026-11-09 · 3 min read

Why /blog/.env returned 500 instead of 404 — the working directory was System32

Next.jsDevOpsDebugging

Translated from the Chinese original: read it in Chinese

Google Search Console's "Server error (5xx)" report had a few entries I could not explain. In server.log I found a very regular batch of 500s:

GET /blog/robots.txt      500
GET /blog/wp-login.php    500
GET /blog/.env            500
GET /blog/.env?v=eKq6X    500
GET /blog/wp-json         500

All of them are vulnerability scanners guessing paths. Being scanned is normal; every public site gets it. What is not normal is the response code. These URLs do not exist, so they should return 404, not 500. Between 10/14 and 11/05 there were 37 of them, 12 of which were /blog/.env.

Only under /blog/

The same scanners hitting /wp-login.php and /.env at the root correctly got 404 (197 requests over the same period, all 404). /blog/wp-includes/wlwmanifest.xml was also a 404. Only the shape /blog/<one segment> returned 500.

That shape matches the article route app/blog/[slug]/page.tsx. wp-includes/wlwmanifest.xml has two segments, does not match [slug], and so is fine.

Why existing posts work and missing ones break

The article page has generateStaticParams, so every post is pre-rendered at build time. When a visitor opens a real post, Next.js returns the built HTML without running any code.

A slug that does not exist is different. By default Next.js "tries to render it on demand", which actually runs the page code, and the first thing that code does is read the post list:

const blogDir = path.join(process.cwd(), "content", "blog");

export function getAllPosts() {
  const files = fs.readdirSync(blogDir) // ← here
  ...
}

process.cwd() is supposed to be the project folder. It is during local development and during next build, so it never caused a problem.

On the live site, cwd is C:\Windows\System32

The production Node.js is started by Windows Task Scheduler at boot. A scheduled task has a "Start in" field, and I had left it empty. When it is empty, the process's working directory is C:\Windows\System32.

So readdirSync was reading C:\Windows\System32\content\blog. The folder does not exist → exception → the page blew up before it ever reached notFound() → 500.

Requesting an arbitrary /blog/nope on the live site did return 500.

I already knew the cwd was wrong

Funnily enough, I had hit this once before, on 7/17. The visitor statistics had to write data/visitor-stats.json, and I discovered then that process.cwd() pointed at System32. The fix was to pass the absolute path in through an environment variable in server.js:

// When Task Scheduler starts this process no working directory is set (process.cwd() is
// something like C:\Windows\System32, not the project root), so pass the absolute path to
// that API route through an environment variable instead of letting it guess with process.cwd().
process.env.VISITOR_STATS_FILE = path.join(dataDir, "visitor-stats.json");

Fixing one file at a time misses the next one. The blog came later, blog.ts naturally used process.cwd(), local tests all passed, until a scanner found it for me.

The fix: one line

Rather than patch file after file, I switch the working directory to the project folder when the server starts:

const projectDir = path.join(__dirname, "..");
process.chdir(projectDir);

Now all code that depends on process.cwd() is correct at once. After deploying, /blog/.env returned 404.

What I learned

  • "All the pre-rendered pages work" can hide the on-demand rendering path. Normal visitors never reach it; only people guessing URLs do.
  • Scanners are a free testing tool. They hit lots of URLs you would never think to test.
  • Fix environment differences at the source. Patching environment variables file by file only fixed the one file I had thought of; setting cwd when the process starts fixed "cwd is wrong" itself.

Type to search projects and posts too