Skip to main content

[Next.js] Why are requests to my Sentry tunnel route failing?

Issue

My Next.js app is not sending events to Sentry. In the browser Network tab, requests to my Sentry tunnel path (often /monitoring or another tunnelRoute value) fail with status codes such as 401, 403, or 500.

Applies To

  • All SaaS Customers (tunnelRoute does not work with self-hosted Sentry)

  • @sentry/nextjs

  • tunnelRoute in withSentryConfig

  • Error Monitoring

Symptoms

  • Browser Network tab shows failed requests to the tunnel path

  • Client events never appear in Sentry

  • Failures can show 401, 403, 500, or similar status codes

Resolution

tunnelRoute sends Sentry traffic through a path on your Next.js app so ad blockers are less likely to drop it. If middleware or your host blocks that path, events never leave the browser.

  1. Confirm the path in withSentryConfig (for example tunnelRoute: "/monitoring" or tunnelRoute: "/sentry-tunnel"). Use a fixed string path, not tunnelRoute: true, if you need to exclude it from middleware.

  2. If you use Next.js middleware (middleware.ts, or proxy.ts on Next.js 16+), exclude the tunnel path from the matcher so middleware does not run on it:

export const config = {
  matcher: [
    "/((?!monitoring|_next/static|_next/image|favicon.ico).*)",
  ],
};

Replace monitoring with your tunnel path segment (for example sentry-tunnel). See the Next.js middleware matcher documentation (not built or maintained by Sentry).

  1. If the matcher change does not apply, delete the .next folder (and any other build cache) and rebuild.

  2. If you use Turbopack and middleware still intercepts the tunnel, keep a fixed tunnelRoute string and the negative matcher above. See the tunnelRoute build option.

  3. If the failure only happens in production (or never on localhost) and middleware is already excluding the route, check hosting or platform rules that block or rewrite that path. Contact your hosting platform administrator for those settings.

See Tunneling in the Next.js manual setup for the recommended fixed-route pattern. The tunnel is implemented with Next.js rewrites. See the Next.js rewrites documentation (not built or maintained by Sentry).

Did this answer your question?