Next.js route handler is returning localhost when deployed on Railway.

farreldarianPRO

8 months ago

Hi team,

I have been deploying several Next.js apps on Railway, and so far it works fine. However, I have encountered an unusual error in this particular project. In the route handler, the URL from the request object returns "localhost" instead of the configured domain.

Here's how I tested:
route.ts
```

export const dynamic = 'force-dynamic'

export async function GET(request: NextRequest) {

console.debug(

JSON.stringify(

{

url: request.url,

nextUrl: request.nextUrl,

},

null,

2

)

)

const session = await auth().getSession()

if (session == null) {

const url = new URL(Routes.auth.login({}), request.url)

console.debug(`Redirecting to ${url.toString()}`)

return NextResponse.redirect(url)

}

...rest of code...
}

```

Logs:
```
{

"url": "https://localhost:8080/";,

"nextUrl": "https://localhost:8080/";

}

Redirecting to https://localhost:8080/user/john-doe
```

5 Replies

farreldarianPRO

8 months ago

Link to project:
https://railway.app/project/bf45f30b-226a-48ed-b949-ff535767440c/service/bae9177d-ca15-4705-970c-e334b7ab09ef


8 months ago

This looks to be an issue with application code, so you'll have to see if anybody else has a similar issue


Status changed to Awaiting User Response railway[bot] 8 months ago


farreldarianPRO

8 months ago

This looks to be an issue with application code, so you'll have to see if anybody else has a similar issue

I had the same thought, but when I deployed to Vercel, everything worked as expected. Note that the code for redirection is the same as in the documentation. I’m curious if this issue might be related to Nixpack or the edge proxy.

docs: https://nextjs.org/docs/app/api-reference/functions/next-response#redirect


farreldarianPRO

8 months ago

I have created a reproduction repository for this issue, which includes only two commits: one for initializing Next.js and another for adding redirection routes. Feel free to check it out:

Repo: https://github.com/fdarian/railway-redirection-repro/tree/main

Here's how you can test:
1. /next-redirect [code] which uses redirect() — redirects correctly to /destination

In this instance, it appears to be functioning correctly now; however, it did not work before (before several redeploy attempts) and still failing in my other project.

2. /route-redirect [code] which uses the Response.redirect(new URL(...)) — redirects localhost:8080/destination instead of the public domain

Comparison to other platform:
1. Vercel: /next-redirect, /route-redirect


jake

This looks to be an issue with application code, so you'll have to see if anybody else has a similar issue

crawfordleedsPRO

11 days ago

I'm having a similar error with NextResponse.redirect in middleware.

This middleware on railway I redirects me to localhost:8080/refresh.

```ts
const url = req.nextUrl.clone(); url.pathname = '/refresh'; url.searchParams.set('redirect_uri', req.nextUrl.pathname);
return NextResponse.redirect(url);
```

I also tried req.url and had the same issue:

```ts
const url = new URL( /refresh?redirect_uri=${encodeURIComponent(req.url)}, req.url ); return NextResponse.redirect(url);
```


Next.js route handler is returning localhost when deployed on Railway. - Railway Help Station