NextRequest.nextUrl hostname is localhost:3000 in production application
Answered
Northeast Congo Lion posted this in #help-forum
Northeast Congo LionOP
## Description
I have deployed the same [application](https://github.com/dimkl/aws-clerk-nextjs) with production settings in 3 different platforms:
- [Railway App](https://railway.app/) [application link](https://aws-clerk-nextjs-production.up.railway.app/)
- [AWS Amplify](https://docs.amplify.aws/) [application link](https://main.d38v5rl8fqcx2i.amplifyapp.com/)
- [Vercel](https://vercel.com/) [application link](https://aws-clerk-nextjs-rf48tgtwp-dimkl.vercel.app/)
## Reproduction steps:
In platforms other than Vercel, when I click the
## Middleware code snippet to redirect
## Extra information
By navigating through the logs I found out that the
I believe the issue occurs due to some header missing when the request is being proxied which is populated correctly in the Vercel platform.
Could you provide some insights about how I can make the NextJS application URLs use the correct hostname.
## Notes
- I also have a report from a customer that this also happens to any dockerized application using NextJS.
- I have checked the x-forwarded-host and it seems correct in all those cases but it's not being used.
I have deployed the same [application](https://github.com/dimkl/aws-clerk-nextjs) with production settings in 3 different platforms:
- [Railway App](https://railway.app/) [application link](https://aws-clerk-nextjs-production.up.railway.app/)
- [AWS Amplify](https://docs.amplify.aws/) [application link](https://main.d38v5rl8fqcx2i.amplifyapp.com/)
- [Vercel](https://vercel.com/) [application link](https://aws-clerk-nextjs-rf48tgtwp-dimkl.vercel.app/)
## Reproduction steps:
In platforms other than Vercel, when I click the
Protected page like (at the center of page) I get redirected to {application_origin}?redirect_url=http%3A%2F%2Flocalhost%3A7260%2Fprotected.## Middleware code snippet to redirect
const redirectToSignIn = ({ returnBackUrl }) => {
const signInUrl = '/sign-in';
const baseURL = new URL(returnBackUrl);
const url = new URL(signInUrl, baseURL.origin);
url.searchParams.set('redirect_url', returnBackUrl);
return NextResponse.redirect(url.toString());
}
if (unauthorizedGuard) {
const returnBackUrl = req.url; // http://localhost:3000/protected
return redirectToSignIn({ returnBackUrl });
}## Extra information
By navigating through the logs I found out that the
req.nextUrl and the req.url use the localhost:3000 as hostname instead of the domains being used to access the apps. In the Vercel case, those URLs are populated correctly using the correct hostname.I believe the issue occurs due to some header missing when the request is being proxied which is populated correctly in the Vercel platform.
Could you provide some insights about how I can make the NextJS application URLs use the correct hostname.
## Notes
- I also have a report from a customer that this also happens to any dockerized application using NextJS.
- I have checked the x-forwarded-host and it seems correct in all those cases but it's not being used.
4 Replies
Broad-snouted Caiman
@Northeast Congo Lion any resolution? I’m currently running into the same bug both in prod, as well as in a Docker container.
American
@Broad-snouted Caiman did you figure it out? ðŸ˜
Seeing the same issue on localhost
American
Btw, I solved it with
request.headers.get("Host")Answer