question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hostname of request object in middleware is always 'localhost'

See original GitHub issue

Verify 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

image

(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

  1. npx create-next-app@latest --typescript
  2. 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;
    
  3. edit local /etc/hosts
    127.0.0.1 test.domain.ai
    
  4. access test.domain.ai from any web browser

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
hanneslcommented, Jun 14, 2022

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.

2reactions
balazsorban44commented, Jun 20, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found