Skip to the content

Lesson 182 of 296

Proxy (formerly middleware)

What you will learn

  • What proxy does and when it runs
  • The rename from middleware to proxy in Next.js 16
  • Redirects, rewrites and headers
  • Protecting routes, and why proxy alone is not enough
  • The matcher config
On this page

What is proxy?

Proxy is code that runs before a request is handled: before the page renders or the route handler runs. It can redirect, rewrite (serve a different route without changing the URL), set headers and cookies, or respond directly.

It lives in one file at the project root (or in src): proxy.ts.

Name change: until Next.js 15 this file was called middleware.ts and the function middleware. In Next.js 16 it was renamed to proxy.ts (with a function named proxy) to make its role, a network boundary in front of the app, clearer. Proxy runs on the Node.js runtime. middleware.ts still works for the Edge runtime but is deprecated. Interviewers may use either name.

Example

TSX
import { NextResponse, type NextRequest } from "next/server";

export function proxy(request: NextRequest) {
  const { pathname } = request.nextUrl;
  const session = request.cookies.get("session")?.value;

  if (pathname.startsWith("/dashboard") && !session) {
    const loginUrl = new URL("/login", request.url);
    loginUrl.searchParams.set("next", pathname);
    return NextResponse.redirect(loginUrl);
  }

  if (pathname === "/old-pricing") {
    return NextResponse.redirect(new URL("/pricing", request.url), 308);
  }

  const response = NextResponse.next();
  response.headers.set("X-Frame-Options", "DENY");
  return response;
}

export const config = {
  matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"]
};
  • NextResponse.next() continues to the normal route.
  • NextResponse.redirect(url, status): the browser goes to a new URL (307 temporary by default, 308 permanent).
  • NextResponse.rewrite(url): serves another route while the address bar keeps the original URL (for example A/B tests or serving per-country content).
  • The matcher limits which paths run proxy. Excluding static files avoids slowing them down.

Common uses

UseExample
Optimistic auth redirectsSend users without a session cookie to /login
RedirectsOld URLs to new URLs, enforcing a trailing slash
InternationalisationRedirect / to /en-GB based on the Accept-Language header
A/B testingRewrite to variant A or B based on a cookie
Security headersAdd headers to every response (CSP, Lesson 264)
Bot or geo filteringBlock or redirect certain requests

Proxy is not your security layer

Proxy should only do quick, optimistic checks, such as "is there a session cookie at all?". It should not query the database on every request (it runs for many requests, so it must be fast).

Real authorisation must happen close to the data: in the data access layer, server actions and route handlers (Lessons 178 and 181). Reasons:

  • A matcher mistake can leave routes unprotected.
  • Server actions are called by POST to the page URL, and a cookie existing does not mean it is valid or that the user may access this record.
  • In March 2025 a serious vulnerability (CVE-2025-29927) let attackers skip Next.js middleware completely with a special header. Apps that relied only on middleware for authentication were exposed; apps that also checked in the data layer were safe.

This is a strong interview point: defence in depth (several layers of checks).

How to say it in an interview

"Proxy, called middleware before Next.js 16, runs before a request reaches a route. It can redirect, rewrite, set headers and cookies or respond directly, and a matcher limits which paths it runs on. I use it for fast optimistic checks like redirecting users without a session cookie, redirects, locale detection, A/B tests and security headers. I never rely on it alone for security; real authentication and authorisation happen in the data access layer, server actions and route handlers, which is why the 2025 middleware bypass vulnerability only hurt apps without that second layer."

Key words

WordMeaning
ProxyCode that runs before a request reaches a route (proxy.ts)
MiddlewareThe old name for proxy (middleware.ts)
RedirectSending the browser to a different URL
RewriteServing a different route without changing the URL
MatcherThe list of paths proxy runs on
Defence in depthSeveral independent layers of security checks

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.