Rate limiting in Next.js 16: proxy.ts, Redis and the IP that really belongs to the client
Rate limiting in Next.js 16 without Vercel: where it goes, why an in-memory counter falls short and how to read the real client IP behind a proxy.
This blog belongs to CREA.MBA, a Next.js boilerplate with auth, payments and an admin panel already set up. See what's included
A login form without a rate limit accepts as many password guesses as anyone cares to send. A sign-up endpoint without one fills up with fake accounts. And a route that calls an AI model can turn a four-line script into an invoice.
Rate limiting in Next.js is usually explained with Upstash and Vercel. Here we build it for an app running on your own server (Docker, Coolify, a VPS), which is where the interesting problems show up: the counter that lives in memory, the reset on every deploy and the X-Forwarded-For header that anyone can forge. The code comes from the rate limiter in the CREA.MBA boilerplate.
Where it goes: proxy.ts for the broad strokes, the handler for the specifics
In Next.js 16 the old middleware.ts is called proxy.ts, and one difference matters here: the proxy always runs on the Node.js runtime. The version 16 docs are explicit: the edge runtime isn't supported in proxy and can't be configured. If you're coming from an Edge middleware that could only talk to Redis over HTTP, you can now use a regular TCP client like ioredis.
The split that works best has two layers:
- In
proxy.ts, a coarse limit per route type: auth, API, webhooks and pages, each with its own quota. - In the route handler or server action, fine-grained limits that depend on your business: three form submissions per hour, fifty messages to the AI assistant per user per day.
The proxy sees every request but knows nothing about your domain. The handler knows who the user is and what they're doing, so it can limit by userId instead of by IP.
Different quotas for different routes
A single limit for the whole app doesn't work. A listing page can fire dozens of link prefetches in a second, and a login endpoint shouldn't accept even a fraction of that. This is how the boilerplate splits requests in proxy.ts:
function getRateLimitConfig(request: NextRequest) {
const { pathname } = request.nextUrl;
if (SENSITIVE_AUTH_PREFIXES.some((p) => pathname.startsWith(p))) {
return { limit: RATE_LIMITS.AUTH_SENSITIVE, prefix: 'auth-sensitive' };
}
if (pathname.startsWith('/api/auth')) return { limit: RATE_LIMITS.AUTH_GENERAL, prefix: 'auth' };
if (pathname.startsWith('/api/webhooks'))
return { limit: RATE_LIMITS.WEBHOOK, prefix: 'webhook' };
if (pathname.startsWith('/api/')) return { limit: RATE_LIMITS.API, prefix: 'api' };
if (request.headers.get('sec-fetch-dest') === 'empty') {
return { limit: RATE_LIMITS.NAVIGATION, prefix: 'navigation' };
}
return { limit: RATE_LIMITS.PAGE, prefix: 'page' };
}The defaults are 10 attempts every 15 minutes for sign-in, sign-up and password reset; 100 requests per minute for the API; 300 per minute for page loads and 1,000 for client-side navigations.
That last one deserves an explanation. When a user navigates inside the app, Next.js fetches the next route's data with a fetch (and does the same for prefetching visible ``s). If those requests share a quota with page loads, a listing with lots of links burns through the limit and starts returning 429 to real visitors and crawlers. Next.js strips its internal RSC headers before the proxy runs, so the boilerplate tells them apart by the header the browser sets: a page load arrives with Sec-Fetch-Dest: document and a fetch() with empty.
And the matcher keeps static assets out of the count:
export const config = {
matcher: ['/((?!_next|.*\\..*).*)'],
};Without a matcher, the Next.js docs warn that the proxy also runs for _next/static, optimized images and everything in public/.
The in-memory counter and its two problems
The simplest implementation is an in-memory Map with each client's timestamps, a sliding window:
async check(key: string, config: RateLimitConfig): Promise {
const now = Date.now();
const windowStart = now - config.windowSeconds * 1000;
const records = (requestStore.get(key) ?? []).filter((r) => r.timestamp > windowStart);
const success = records.length < config.maxRequests;
if (success) {
records.push({ timestamp: now });
requestStore.set(key, records);
}
// ...
}Notice that rejected requests aren't recorded. If they were, someone who keeps trying during the block would extend it forever.
For a single server, this works. But it has two limitations that matter in production:
- Each process keeps its own count. With two instances behind a load balancer, the effective limit is twice what you configured.
- Every deploy resets it. For someone guessing passwords, waiting for your next deploy is a free counter reset.
Redis (or Valkey) to share the counter
The fix is moving the counter to a shared store. With a Redis sorted set, the sliding window becomes a Lua script that runs atomically on the server, so two instances can't read the same value before either of them writes it:
local now = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local limit = tonumber(ARGV[3])
local member = ARGV[4]
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now - window)
local used = redis.call('ZCARD', KEYS[1])
local allowed = 0
if used < limit then
redis.call('ZADD', KEYS[1], now, member)
allowed = 1
end
redis.call('PEXPIRE', KEYS[1], window)
return { allowed, used }With ioredis you register it as a custom command with defineCommand, and each check is a single call. One detail: the sorted set member has to be unique even if two requests land in the same millisecond, which is why the boilerplate builds it from the timestamp, an instance id and a counter.
In Coolify, adding Redis or Valkey is a two-click resource that gives you the internal REDIS_URL. If the variable isn't set, the limiter keeps using memory, with no errors and no connections to anywhere.
If Redis goes down, the app stays up
A rate limiter that takes your app down when its store fails is worse than no limiter. Three choices in the boilerplate's Redis client point that way:
{
lazyConnect: true,
enableOfflineQueue: false, // without this, requests sit waiting
maxRetriesPerRequest: 1,
connectTimeout: 1000,
commandTimeout: 1000,
}With enableOfflineQueue: false, a command sent while the connection is down fails immediately instead of queuing until Redis comes back (with the user's request hanging along with it). When it fails, the Redis strategy falls back to memory for a few seconds, logs a single line and tries again. While Redis is down, each instance counts on its own, which beats not counting at all.
The client IP: don't trust the first value
To limit by IP you need to know which IP it is. Behind Traefik, nginx or Cloudflare, the connection IP is the proxy's, and the client's comes in X-Forwarded-For. The code floating around does this:
// ❌ The first value is set by the client
const ip = request.headers.get('x-forwarded-for')?.split(',')[0];The problem is that proxies append their entry to the end of the header. The first value is whatever the client sent, and the client can send anything. Put a different made-up IP on each request and you get a fresh quota every time, and the login limit stops doing its job.
The right way is to count from the right as many entries as you have trusted proxies in front:
function getClientIp(request: NextRequest): string {
const hops = Number(process.env.TRUSTED_PROXY_HOPS ?? 1);
const chain = (request.headers.get('x-forwarded-for') ?? '')
.split(',')
.map((entry) => entry.trim())
.filter(Boolean);
if (hops > 0 && chain.length > 0) {
const trusted = chain[Math.max(0, chain.length - hops)];
if (trusted) return trusted;
}
return request.headers.get('x-real-ip')?.trim() || chain.at(-1) || 'unknown';
}With a single Traefik in front (the usual Coolify setup), TRUSTED_PROXY_HOPS=1 and the right IP is the last one. If you also put Cloudflare in front, that's two hops. Overshoot and you're back to reading a value the client controls; undershoot and all your users share the proxy's IP and block each other. It's worth checking once with an endpoint that returns what it sees.
Better Auth faces the same dilemma with its own rate limiter, which is on in production and keeps its counters in memory by default. Its docs recommend listing in advanced.ipAddress.ipAddressHeaders only the header your proxy sets, or declaring trustedProxies if your proxies append to X-Forwarded-For. If you use both limiters, configure them with the same logic. We covered it in more detail in the production setup for Better Auth in Next.js 16.
Per-user limits inside the handler
For operations that cost money or can be abused with a valid account, the limit goes per user. The same limiter works; here's how it looks in an AI chat endpoint:
export const POST = createAuthenticatedApiHandler({
schema: chatSchema,
handler: async (input, req, user) => {
await requireRateLimit(req, RATE_LIMITS.API, { userId: user.id, prefix: 'ai-chat' });
// ...
},
});requireRateLimit throws a TooManyRequestsError that the wrapper turns into a 429 response. (The Retry-After travels inside the error; if you want it as a header too, like in the proxy, it's one line in the error handler.) The key ai-chat:user: doesn't depend on the IP, so switching networks or using a VPN doesn't buy a fresh quota.
What to return when the limit is hit
A 429 with headers the client can use:
return NextResponse.json(
{ success: false, error: { code: 'RATE_LIMIT_EXCEEDED', message: config.message } },
{
status: 429,
headers: {
'X-RateLimit-Limit': result.limit.toString(),
'X-RateLimit-Remaining': result.remaining.toString(),
'X-RateLimit-Reset': result.reset.toString(),
'Retry-After': Math.max(0, result.reset - Math.floor(Date.now() / 1000)).toString(),
},
},
);Retry-After in seconds tells a well-behaved client how long to wait. On the login form, instead of a generic error, you can show "Too many attempts, try again in a few minutes".
How to check it works
A curl loop against the login endpoint is enough to see the 429:
for i in $(seq 1 12); do
curl -s -o /dev/null -w "%{http_code}\n" -X POST \
-H "Origin: http://localhost:3000" -H "Content-Type: application/json" \
-d '{"email":"a@b.com","password":"x"}' \
http://localhost:3000/api/auth/sign-in/email
doneThe first requests return a credentials error and, from the eleventh on, a 429 (or earlier, if Better Auth's limiter kicks in, which is stricter on /sign-in/email in production). For the forged IP case, run the loop again with -H "X-Forwarded-For: 1.2.3.$i" against the deployed app: the 429 must still arrive.
Takeaways
- In Next.js 16 the proxy runs on Node.js; you can use
ioredisdirectly. - Coarse limits per route type in
proxy.ts, per-user limits in the handler. - An in-memory counter is fine with one instance; with two, or with frequent deploys, use Redis or Valkey.
- Read the client IP from the right of
X-Forwarded-For, counting the proxies you control.
In CREA.MBA this limiter comes built in: per-route-type quotas in the proxy, optional Redis or Valkey via REDIS_URL and the IP resolved with TRUSTED_PROXY_HOPS. If you're interested in the rest of API security, in users only see their own data we explain why the proxy shouldn't be where you decide who can read what.
Have you ever spotted a burst against your login in the logs? How did you deal with it?
Enjoyed this article?
Subscribe for more tutorials and tips on building products with AI