Production hardening for Next.js on Vercel
Hidden 5xx errors behind bot traffic, firewall rules at the edge, a middleware fallback, and the checks that keep logs and bills clean. What we review, what we change, and how we deliver it.
by Zechim
A public site gets scanned all day. Bots probe for WordPress, PHP files, environment files and admin panels that do not exist on your site. On a well configured site those requests cost nothing. On a site that is not, they return errors, trigger function invocations, fill the logs, and hide real problems.
This post describes the production hardening we do for Next.js on Vercel and what we check first.
What we review
1. Errors hiding behind bot traffic
We read the logs for 5xx responses on paths that should be a plain 404. A request for a path that does not exist should return a clean 404 or 403 and nothing else. A 500 means the request reached application code in a way nobody planned, and each one is a paid function invocation.
A common cause: a middleware matcher that skips any path containing a dot, so scanner paths bypass the middleware, reach a dynamic catch-all route, and fail against a layout that was prerendered only for the real locales.
2. Rules at the edge
The firewall can reject a request before it reaches any function. We configure rules in three groups, all with Deny so the response is a fast 403 with no downstream cost:
| Group | Match examples |
|---|---|
| Script extensions used in probes | paths ending in .php, .aspx, .asp, .jsp, .cgi, .env |
| CMS and admin probes | paths starting with /wp-, /xmlrpc, /phpmyadmin, /adminer, /cpanel |
| Hidden file probes | paths starting with /.git, /.aws, /.ssh, /.svn, /.DS_Store |
We use Deny and not Challenge. No bot solves the challenge, and no legitimate visitor requests those paths.
3. A fallback in the code
If a firewall rule is off or the project moves hosts, the application still needs to answer correctly. We add a short check in the middleware that returns a plain 404 for the same patterns before the rest of the logic runs, and extend the matcher so those paths reach it:
const SCAN_PATH_PATTERNS = [
/\.php$/i,
/\.aspx?$/i,
/\.jsp$/i,
/\.cgi$/i,
/^\/wp-/i,
/^\/xmlrpc(\.|$)/i,
/^\/phpmyadmin(\/|$)/i,
/^\/\.(env|git|aws|ssh|svn|hg|DS_Store)(\b|\/|$)/i,
];
export default function middleware(req: NextRequest) {
if (SCAN_PATH_PATTERNS.some((re) => re.test(req.nextUrl.pathname))) {
return new NextResponse("Not Found", { status: 404 });
}
return intlMiddleware(req);
}
Two layers give you resilience if one is disabled, portability if you change hosts, and separate visibility: firewall blocks appear in the firewall activity, and middleware blocks appear in function logs.
4. Crawlers that should get in
A rule that stops scanner bots can block useful crawlers too. We verify that search and AI crawlers still reach your pages after the rules are in place.
What we do not do
- We do not try to block every pattern. An exhaustive list becomes a maintenance burden and starts to break legitimate paths. We cover the common ones and let the rest take the normal 404.
- We do not alert on every block. Bot scans are the normal state of the public internet. We log counts by pattern per hour, or nothing.
- We do not add a third party firewall by default. For a Next.js site on Vercel, the built in firewall plus the middleware fallback covers the common cases. A separate WAF pays off when you need per IP rate limits, geo rules or DDoS protection.
How we deliver it
- 2 week Discovery. We review logs, the middleware, the routing and the firewall settings, and return findings ranked by risk and cost.
- 4 to 8 week Pilot. We apply the changes, verify each with a request that failed before, and measure the error and invocation counts before and after.
- Optional ongoing mode. A Fractional AI Lead keeps reviewing the platform as the application grows.
The configuration, the code and the documentation are yours.
What you need to start
- Access to the Vercel project (logs and firewall settings) and the repository.
- A list of the real paths and locales your site serves.
If your Next.js site is in production and you have not looked at the bot traffic in your logs lately, worth 30 minutes to look at it together.