Hostname of request object in middleware is always 'localhost'
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
Binaries:
Node: 14.19.1
npm: 8.12.1
Yarn: 1.22.18
pnpm: N/A
Relevant packages:
next: 12.1.7-canary.31
react: 18.1.0
react-dom: 18.1.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
(I edited local /etc/hosts
file to reproduce the issue, but the same issue happens even when I deploy an app and connect a domain to that.)
Whatever domain is being used, request
object, which is an input of a middleware function, always has "localhost"
as its hostname.
I use middleware to store all the access logs to an ES storage. Because request.url
or request.nextUrl
always has ‘“localhost”’ as its hostname, I cannot distinguish stage.something.com
from something.com
or dev.something.com
.
Expected Behavior
It should has the actual URL information. (in the example above, hostname should be test.domain.ai
).
To Reproduce
npx create-next-app@latest --typescript
- add middleware function
/* eslint-disable @next/next/no-server-import-in-page */ import { NextResponse } from "next/server"; import type { NextMiddleware } from "next/server"; const middleware: NextMiddleware = (request) => { const response = NextResponse.next(); const { url, nextUrl } = request; const { host, hostname } = nextUrl; console.log({ url, host, hostname }); return response; }; export default middleware;
- edit local
/etc/hosts
127.0.0.1 test.domain.ai
- access
test.domain.ai
from any web browser
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Get hostname of current request in node.js Express
This is always accurate. It returns the machine's hostname, which is what the title of the question asked. (The text of this one...
Read more >API Reference - Express 4.x
Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option. This parser accepts any...
Read more >HTTPS | Node.js v19.3.0 Documentation
If one needs to upload a file with a POST request, then write to the ClientRequest object. const https = require('node:https'); const options...
Read more >Server Options - Vite
Server Options #. server.host #. Type: string | boolean; Default: 'localhost'. Specify which IP addresses the server should listen on.
Read more >Configure endpoints for the ASP.NET Core Kestrel web server
If no ports are specified, Kestrel binds to http://localhost:5000 . ... are given 5 seconds to complete processing requests and shut down.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @balazsorban44 but for my use case I depend on being able to get the actual hostname that the request uses. It’s often the case that a website can be reached through more than one hostname, and for me that information is important.
My use case aside, I think most people would expect a request object to reflect the actual request URL, not a URL that is constructed from the application config.
Understandable, but this is to increase security when hosting outside of Vercel, as you may understand. Closing with the above suggestion https://github.com/vercel/next.js/issues/37536#issuecomment-1157000990.
Again, use it with care, only if you trust your proxy.