Skip to content

Commit 4b45d85

Browse files
committed
0 parents  commit 4b45d85

25 files changed

+1546
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.defaultFormatter": "esbenp.prettier-vscode"
4+
}

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Next Rewrites Test
2+
3+
This repository is intended to verify that the host header is properly passed when reverse proxying another web server in a Next.js project.
4+
5+
This repository consists of three projects:
6+
7+
- `edge-middleware` - This project uses the [edge middleware feature](https://nextjs.org/docs/app/building-your-application/routing/middleware) to return the content from `localhost:4000/foo` when accessed via `localhost:3000/foo`.
8+
- `nextjs-rewrites` - This project accomplishes the same task as the above using the [rewrites feature of the Next.js config.](https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites)
9+
- `rewrite-target` - This project provides an API at `localhost:4000/foo` that returns whatever host header it receives.
10+
11+
## How to run
12+
13+
```sh
14+
pnpm install
15+
pnpm dev:edge-middleware # or pnpm dev:nextjs-rewrites
16+
open http://localhost:3000/foo
17+
```

edge-middleware/.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
.yarn/install-state.gz
8+
9+
# testing
10+
/coverage
11+
12+
# next.js
13+
/.next/
14+
/out/
15+
16+
# production
17+
/build
18+
19+
# misc
20+
.DS_Store
21+
*.pem
22+
23+
# debug
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts

edge-middleware/app/layout.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as React from "react";
2+
3+
export default function Layout({ children }: React.PropsWithChildren) {
4+
return (
5+
<html>
6+
<body>{children}</body>
7+
</html>
8+
);
9+
}

edge-middleware/app/page.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return null;
3+
}

edge-middleware/middleware.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
3+
export const config = {
4+
matcher: ["/foo"],
5+
};
6+
7+
export default function middleware({ nextUrl, headers }: NextRequest) {
8+
const url = new URL(nextUrl);
9+
url.hostname = "localhost";
10+
url.port = "4000";
11+
12+
// localhost:3000. Seems this header is ignored.
13+
console.log(headers.get("host"));
14+
15+
return NextResponse.rewrite(url, { headers });
16+
}

edge-middleware/next.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {};
3+
4+
export default nextConfig;

edge-middleware/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "edge-middleware",
3+
"scripts": {
4+
"dev": "next dev",
5+
"build": "next build",
6+
"start": "next start",
7+
"lint": "next lint"
8+
},
9+
"dependencies": {
10+
"react": "^18",
11+
"react-dom": "^18",
12+
"next": "14.2.2"
13+
},
14+
"devDependencies": {
15+
"typescript": "^5",
16+
"@types/node": "^20",
17+
"@types/react": "^18",
18+
"@types/react-dom": "^18"
19+
}
20+
}

0 commit comments

Comments
 (0)