Skip to main content
GoodFirstPicks
DashboardIssuesReposLeaderboard

GoodFirstPicks by Leaveitblank © 2026

CreatorRequest a RepoPrivacy PolicyTerms of Service
Rewrite does not set the correct x-forwarded-host in Vercel | GoodFirstPicks

Rewrite does not set the correct x-forwarded-host in Vercel

vercel/next.js 3 comments 1mo ago
View on GitHub
mediumopenScope: somewhat clearSkill match: maybeNext.jsTypeScriptReact

Why this is a good first issue

Middleware rewrite incorrectly sets x-forwarded-host header.

AI Summary

The issue reports that Next.js middleware rewrites incorrectly set the x-forwarded-host header when performing URL rewrites. The expected behavior is to preserve the original host in this header, but it's currently being overwritten with the destination host. This requires investigation into the middleware rewrite logic and header handling.

Issue Description

Link to the code that reproduces this issue

https://github.com/hector/middleware-web

To Reproduce

The basic code to trigger this bug is simply to use a rewrite in the middleware of Next.js like this:

return NextResponse.rewrite(url, { request: req });

If you click on this link https://middleware-web.vercel.app/?rewrite=https://request-headers-web.vercel.app/ you are reproducing this issue.

This is what is happening:

  • We are accessing the website https://middleware-web.vercel.app/, which will do a rewrite in the middleware for the url written in the query param "rewrite"
  • We are pointing the rewrite to https://request-headers-web.vercel.app/ which is a website that simply prints the request headers it receives

Code of https://middleware-web.vercel.app/ is public in this repo https://github.com/hector/middleware-web Code of https://request-headers-web.vercel.app/ is public in this repo https://github.com/hector/request-headers-web

Current vs. Expected behavior

I would expect to receive the following headers in https://request-headers-web.vercel.app/ server:

  • host: request-headers-web.vercel.app
  • x-forwarded-host: middleware-web.vercel.app

This is what I is actually received (it is printed in the page and can be easily seen):

  • host: request-headers-web.vercel.app
  • x-forwarded-host: request-headers-web.vercel.app

The header x-forwarded-host is not set to the original host before the rewrite. MDN Reference here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.6.0: Mon Apr 22 20:51:27 PDT 2024; root:xnu-8796.141.3.705.2~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 20.13.1
  npm: 10.5.2
  Yarn: 1.22.22
  pnpm: 9.3.0
Relevant Packages:
  next: 15.0.0-canary.54 // L

GitHub Labels

bugMiddleware

Want to work on this?

Claim this issue to let others know you're working on it. You'll earn 20 points when you complete it!

Risk Flags

  • requires understanding of Vercel infrastructure
  • header manipulation
Loading labels...

Details

Points20 pts
Difficultymedium
Scopesomewhat clear
Skill Matchmaybe
Test Focusedno