Next.js Discord

Discord Forum

Invalid URL in route handlers when running through apache rewrite

Answered
Dwarf Crocodile posted this in #help-forum
Open in Discord
Dwarf CrocodileOP
I have a NextJS app running through an openshift kubernetes cluster and then an apache server that rewrites the request to my preffered subdomain. Below you can see the setup where togethernet.my-url.com is my desired domain and web-web.apps.okd.my-url.com is the domain given by the okd server.

Here is my apache config below.

ServerName togethernet.my-url.com
RequestHeader unset X-forwarded-for

RewriteEngine on
RewriteRule .* https://web-web.apps.okd.my-url.com%{REQUEST_URI} [P,QSA,NE]

RequestHeader set X-FORWARDED-PROTO https
RequestHeader set X-Forwarded-Ssl on

SSLProxyEngine on
SSLEngine on
SSLOptions StrictRequire


The problem is that when i enter any route handlers on the okd domain everything works fine but when i access them through the rewrite on togethernet.my-url.com i get the error below. My desired effect is that the togethernet.my-url.com domain's route handlers would work the same as on the okd domain.

⨯ TypeError: Invalid URL
at new URL (node:internal/url:796:36)
at NextRequestAdapter.fromNodeNextRequest (/usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/web/spec-extension/adapters/next-request.js:91:23)
at NextRequestAdapter.fromBaseNextRequest (/usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/web/spec-extension/adapters/next-request.js:70:35)
at doRender (/usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:1316:73)
at cacheEntry.responseCache.get.routeKind (/usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:1539:34)
at ResponseCache.get (/usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/response-cache/index.js:49:26)
at NextNodeServer.renderToResponseWithComponentsImpl (/usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:1447:53)
at /usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:976:121
at NextTracerImpl.trace (/usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/lib/trace/tracer.js:104:20)
at NextNodeServer.renderToResponseWithComponents (/usr/src/app/node_modules/.pnpm/next@14.1.4_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:976:41) {
code: 'ERR_INVALID_URL',
input: '/auth/callback',
base: 'https, https://localhost:3001/auth/callback'
}
Answered by Toyger
https://github.com/vercel/next.js/issues/58914
which version of nextjs you have? looks like it's problem with multiple x-forwarded-proto
in your last end side reverse proxy you can strictly set
RequestHeader set X-Forwarded-Proto "https"
View full answer

20 Replies

please only bump at most once a day
Dwarf CrocodileOP
Oh sorry
Dwarf CrocodileOP
Maybe worth to note that it happens on all route handler not just specific ones and across projects
Proably an issue with the rewrite
Dwarf CrocodileOP
Also i've checked the code and i find it strange that the route handler redirects to localhost:3001 as the request is coming from another URL. It's also strange that the localhost url is invalid, i can't figure out if this is a bug or something i'm doing wrong on the server.
Dwarf CrocodileOP
bump
Toyger
why are you using apache rewrite with proxy? imo it's create more confusion. it's easier to use nginx with simple reverse proxy either better have ingress controller that map it correctly.
Dwarf CrocodileOP
I am required to use apache as the company uses apache unfortunately
I do have to get this to work though
@Dwarf Crocodile I do have to get this to work though
Toyger
then instead rewrite it's better to configure proxy_pass, it's more straightforward
Dwarf CrocodileOP
I now have a proxypass and it gives the same error
@Dwarf Crocodile I now have a proxypass and it gives the same error
Toyger
how are this url https://web-web.apps.okd.my-url.com configured? is it inside kubernetes, is it behind another apache/nginx ?
what if you are trying to use site directly from this url
Dwarf CrocodileOP
The web-web.apps domain's route handlers works as expected it is the url that is run through the apache proxypass/rewrite where the route handlers doesn't work
@Dwarf Crocodile The web-web.apps domain's route handlers works as expected it is the url that is run through the apache proxypass/rewrite where the route handlers doesn't work
Toyger
https://github.com/vercel/next.js/issues/58914
which version of nextjs you have? looks like it's problem with multiple x-forwarded-proto
in your last end side reverse proxy you can strictly set
RequestHeader set X-Forwarded-Proto "https"
Answer
Dwarf CrocodileOP
I only have one x-forwarded-proto in my conf though
I can try to remove one and see how if it works
Toyger
or you probably need to unset header completely.
because this one
base: 'https, https://localhost:3001/auth/callback'

should be
base: 'https://localhost:3001/auth/callback'
Dwarf CrocodileOP
Yeah i'll try to do that
That fixed it apparently okd sent its own x-forwarded-proto so the one in apache was raising an error
Thank you