Next.js Discord

Discord Forum

request.nextUrl is always localhost on Dokku

Unanswered
Maxence Rose posted this in #help-forum
Open in Discord
Avatar
Hey 🙂

I search the solution for my issue, request.nextUrl in middleware is always localhost on Dokku (an Heroku like) :

2024-09-14T17:43:41.400604519Z app[web.1]: {
2024-09-14T17:43:41.400693155Z app[web.1]:   href: 'https://localhost:5000/',
2024-09-14T17:43:41.400702067Z app[web.1]:   origin: 'https://localhost:5000',
2024-09-14T17:43:41.400705929Z app[web.1]:   protocol: 'https:',
2024-09-14T17:43:41.400709117Z app[web.1]:   username: '',
2024-09-14T17:43:41.400712795Z app[web.1]:   password: '',
2024-09-14T17:43:41.400717456Z app[web.1]:   host: 'localhost:5000',
2024-09-14T17:43:41.400725654Z app[web.1]:   hostname: 'localhost',
2024-09-14T17:43:41.400735400Z app[web.1]:   port: '5000',
2024-09-14T17:43:41.400741075Z app[web.1]:   pathname: '/',
2024-09-14T17:43:41.400745655Z app[web.1]:   search: '',
2024-09-14T17:43:41.400750736Z app[web.1]:   searchParams: URLSearchParams {  },
2024-09-14T17:43:41.400756092Z app[web.1]:   hash: ''
2024-09-14T17:43:41.400760924Z app[web.1]: }


It's maybe Nginx but I don't find the solution...

Thanks 🙂

1 Reply

Avatar
My entires middleware.ts file :

import { NextResponse, type NextRequest } from "next/server"

import { getDomain } from "@/lib/utils"

export function middleware(request: NextRequest) {
  const url = request.nextUrl

  console.log(url)

  const { domain, subdomain } = getDomain(url.hostname)

  if (domain) {
    if (subdomain && subdomain !== process.env.LANDING_SUBDOMAIN) {
      return NextResponse.rewrite(
        new URL(`/${domain}/${subdomain}${url.pathname}${url.search}`, url)
      )
    } else {
      return NextResponse.rewrite(
        new URL(`/${domain}${url.pathname}${url.search}`, url)
      )
    }
  }
}

export const config = {
  matcher: [
    /*
     * Match all paths except for:
     * 1. /api routes
     * 2. /_next (Next.js internals)
     * 3. all root files inside /public (e.g. /favicon.ico)
     * 4. opengraph images (e.g. /[a-z0-9-_.]/[a-z0-9-_]/opengraph-image)
     */
    "/((?!api/|_next/|_static/|[\\w-]+\\.\\w+|[a-zA-Z0-9-_.]+/[a-zA-Z0-9-_]+/opengraph-image).*)",
  ],
}