Nginx proxied API + Next's RSC fetch not working
Unanswered
Kawakawa posted this in #help-forum
KawakawaOP
Hiya all - my API and site are both under an Nginx server acting as a reverse proxy - so the server is just on localhost, and my backend API is proxied to be on localhost/api. However, when using Next 13 and RSCs - the data doesn’t fetch and I get either a “failed to pass URL†error or an ECONNRESET when trying to fetch the data on e.g. “localhost/api/productsâ€
Would anybody be able to provide any advice?
Would anybody be able to provide any advice?
12 Replies
European sprat
You're going to need to make some minimal reproduction repo and environment that can be replicated or hope someone else has the issue and has fixed it
@European sprat You're going to need to make some minimal reproduction repo and environment that can be replicated or hope someone else has the issue and has fixed it
KawakawaOP
I know what the issue is - I just don't know the best way to go about fixing it.
Basically - the backend and the frontend are containerised, and under an Nginx proxy.
When requesting data from the backend in client components - it has to go through the proxied URL as it's the only exposed way to communicate with the backend.
When requesting data from the backed in server components, however - it cannot go through the proxied URL as the server is behind the proxy. Therefore, to communicate with the server - I just need to make the request to e.g.
So, my real question is - how do I dynamically switch the request url based off of if the request is being made in RSCs or in client components?
Basically - the backend and the frontend are containerised, and under an Nginx proxy.
When requesting data from the backend in client components - it has to go through the proxied URL as it's the only exposed way to communicate with the backend.
When requesting data from the backed in server components, however - it cannot go through the proxied URL as the server is behind the proxy. Therefore, to communicate with the server - I just need to make the request to e.g.
http://server:3001/api.So, my real question is - how do I dynamically switch the request url based off of if the request is being made in RSCs or in client components?
Would re-writes be of any use here etc?
If any of that makes sense 😅
European sprat
I just use an environment variable. Is the backend API which the server component interacts with nextjs route handler? If so, just make the query directly and don't call the API
@European sprat I just use an environment variable. Is the backend API which the server component interacts with nextjs route handler? If so, just make the query directly and don't call the API
KawakawaOP
No, I don't use the internal NextJS api. So, the route /api is just my server but proxied with Nginx e.g.
http://server:3001 (url for accessing the server internally, not in client components or externally)So, when in client components - I need to use the following base url to fetch data from the API:
http://localhost/api/, but in server components I need to do http://server:3001/api/If that makes sense
I just don't know how to dynamically switch it based on if the request is being made in a RSC or a client component
European sprat
Can't you just have it hardcoded in the RSCs?
@European sprat Can't you just have it hardcoded in the RSCs?
KawakawaOP
I could - but I put my big fetch requests in functions for a cleaner codebase to then use in either client or server components.
So it'd be nice if I could have something like rewrites for this exact purpose, but how I don't know.