Don't start Next.js with -H: redirect loops and EPROTO behind a proxy
Three things broke in production at once: localized paths such as /en/projects redirected to themselves forever, NextResponse.rewrite calls in middleware returned 500 with EPROTO, and the admin panel hung silently after a successful login.
All three had one cause: the -H 127.0.0.1 flag in the pm2 command.
What the flag changes
Give Next a bind address and it treats that address as its own origin — http://localhost:4000 — and stops honouring the Host header of the incoming request.
Harmless on its own. Behind a reverse proxy it cascades.
1. next-intl loops
next-intl resolves the locale from the URL and redirects when it has to. Once the app believes it lives at localhost:4000, a request for https://yoursite.com/en/projects looks like it arrived at an origin it does not recognise, so it issues a redirect. The browser follows it, the same conclusion is reached again, and you get ERR_TOO_MANY_REDIRECTS.
2. Rewrites fail with EPROTO
If nginx forwards X-Forwarded-Proto: https, Next sees the scheme as https while its own origin is localhost:4000. A rewrite target then reads as an external TLS destination, and the app waits for a TLS handshake while talking plain HTTP to itself:
Error: write EPROTO ... SSL routines:ssl3_get_record:wrong version number
The confusing part: the error points at certificates, and certificates have nothing to do with it.
3. Auth breaks
NextAuth builds callback URLs from the origin. A wrong origin means returning to the wrong address after login — which presents as "the password works but I can't get into the panel".
The fix
Drop the bind flag:
# wrong
pm2 start ./node_modules/next/dist/bin/next -- start -p 4000 -H 127.0.0.1
# right
pm2 start ./node_modules/next/dist/bin/next -- start -p 4000
Without it Next respects the Host header and all three problems disappear together.
"But I need the port closed"
That is the usual reason for reaching for -H 127.0.0.1. Close the port at the firewall instead of at the application:
ufw allow 80,443/tcp
# 4000 is never opened
The app listens on every interface, but only the ports nginx uses are reachable from outside. Same protection, no side effects.
How to spot it
Check the bind flag first if you see any of these:
- Localized paths loop while the root path works
- Paths involving a rewrite or redirect return 500 while static paths are fine
EPROTOorwrong version numberin the logs, though nginx terminates TLS- Login appears to succeed, then the session vanishes
The nginx access log settles it: POST /api/auth/callback 200 followed immediately by GET /admin 302 means the problem is in front of your app, not inside it.