Middleware to support reverse proxying via a defined URI path. #59904
Unanswered
sohmc
asked this question in
Show and tell
Replies: 2 comments 1 reply
-
@sohmc Did you find a solution? |
Beta Was this translation helpful? Give feedback.
1 reply
-
hard-code solution example: function resolveURL(request: NextRequest) {
const protp = request.headers.get("X-Forwarded-Proto");
const host = request.headers.get("X-Forwarded-Host");
const port = request.headers.get("X-Forwarded-Port");
const url = new URL(request.url);
if (protp && host && port) {
url.protocol = proto;
url.host = host;
url.port = port;
url.hostname = `${host}:${port}`;
}
return url.toString()
} worked at |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I wrote a react app that is being served from next.js and everything works as expected. I've containerized the entire app by following the instructions (more or less) and now have a container that functions as expected. However, like most apps, it needs to sit behind a reverse proxy so that I can access it using one URL.
The way I have my reverse proxy set up is a base URL of
https://secure.example.com
with/app
paths that go to different programs.When setting up my next app (the
reactjs
location), I've run into a bit of trouble. When I go tohttps://secure.example.com/reactjs/
, the page loads but the related CSS, javascript files, etc. don't. Upon inspection of the loading html, these files are being requested from/_next/
. My browser attempts to loadhttps://secure.example.com/_next/...
but hits a 404, which is expected since nothing is being served from that location.I would have expected others to have the same issue but did not find a functioning solution. The closest discussion I found was #32216 which would resolve my issue.
My initial solution was to add an additional nginx rule:
While this solution works, it only allows one next app to be hosted. Ideally, I would much prefer a solution where the rendered html is properly updated to either have resource paths be relative (i.e.
_next/someResource.js
vs/_next/someResource.js
) or at least provide an option to add a request path.Being pretty new to reactjs and nextjs, I wasn't sure if there was a feature or configuration switch that would allow this operation. The closest discussion I've found is #32216.
So instead, I updated my middleware and basePath and was able to successfully remove the additional
location
context.My nginx config:
next.conf.js:
middleware.js:
This allows files to be served properly and without an additional location context within nginx. The only other issue I have with this is that
basePath
MUST match the location URI path being used by nginx. In my case, I don't suspect this to be an issue but if I were creating an app that was going to be widely used, this may pose a bit of a challenge. I would much prefer that this path be able to be set via an environment variable but my attempts to do so were met with errors stating thatbasePath
must either be empty or set.Hopefully this behavior will be added out of the box in the near future.
Beta Was this translation helpful? Give feedback.
All reactions