#Nuxt3, Nitro, h3, proxy, 502
1 messages · Page 1 of 1 (latest)
Based on the information in the knowledge sources, your 502 Bad Gateway error with a message like "Proxy request failed: Bad Gateway ... [cause]: fetch failed ... [cause]: redirect count exceeded ..." when using a Nuxt 3 + Nitro + h3 proxy setup can be related to several factors:
-
Proxy Implementation: The recommended approach for proxying in Nuxt 3 is to use a Nitro server API endpoint as a proxy, as demonstrated in Alexander Lichter's repository and video. This approach is generally robust, but issues can arise depending on how requests and headers are handled between your Nuxt app and the target API. If the example API works but your real API does not, the issue is likely with the specifics of your API or how the proxy is configured for it, not with the general Nuxt/Nitro proxy mechanism itself YouTube: The BEST way to proxy your API in Nuxt.
-
Redirect Count Exceeded: The error message "[cause]: redirect count exceeded" suggests that the proxy request is being redirected too many times. This can happen if:
- The target API is redirecting requests (e.g., from HTTP to HTTPS, or to a login page).
- The proxy endpoint is misconfigured and is somehow redirecting to itself, causing an infinite loop.
-
Headers and Connection Issues: There are known issues with proxying in Nuxt/Nitro, especially with newer Node.js versions, where certain headers (like
connection: closeorkeep-alive) can cause problems. For example, in one case, setting theconnectionheader to'keep-alive'resolved proxy issues in development mode with Node 19+ Stack Overflow: Nuxt SSR DEV mode: proxyRequest does not work with node 19+ (works with 18).
- API Differences: If your API requires authentication, special headers, or does not accept certain forwarded headers, it may respond with redirects or errors that the proxy cannot handle. Compare the working example API and your real API for differences in required headers, authentication, or CORS settings.
What you can try:
- Check if your API is redirecting requests (e.g., from HTTP to HTTPS). Try accessing the API directly from the server where your Nuxt app runs.
- Inspect and log the headers sent and received by your proxy endpoint. Make sure you are not forwarding headers that could cause issues (like
host,connection, etc.). - If using Node 19+, try explicitly setting the
connectionheader to'keep-alive'in your proxy handler. - Ensure your proxy endpoint is not accidentally redirecting to itself (e.g., by matching its own route).
Example fix for connection header:
export default defineEventHandler(async (event) => {
// ... your proxy logic
event.node.req.headers['connection'] = 'keep-alive';
// continue with proxyRequest or fetch
});
Stack Overflow: Nuxt SSR DEV mode: proxyRequest does not work with node 19+ (works with 18)
If these steps do not resolve the issue, try to reproduce the problem with a minimal setup and compare the network requests/responses between the working and failing cases. This can help isolate whether the problem is with the Nuxt/Nitro proxy setup or with the specifics of your API.
If you need more targeted help, please provide the relevant proxy handler code and any custom headers or authentication your API requires.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@amber kite Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. One moment! :books:
To fix the "redirect count exceeded" error when proxying with Nuxt 3 and Nitro, you should first check if your target API is redirecting requests—commonly from HTTP to HTTPS or to a login page. This is a typical cause of too many redirects, which results in the error you're seeing.
Here are steps you can take, based on the knowledge sources:
-
Test the API Directly: Try accessing your API endpoint directly (outside of Nuxt/Nitro) using a tool like Postman or curl. If you see a redirect (e.g., from HTTP to HTTPS), you need to update your proxy target to use the correct protocol (usually HTTPS).
-
Check for Authentication Redirects: If your API requires authentication and you are not providing the necessary credentials or tokens, the API might redirect you to a login page, causing repeated redirects. Make sure your proxy forwards any required authentication headers.
-
Compare with a Working Example: Since the example API from Alexander Lichter's repository works, compare its endpoint and protocol with your real API. Ensure you are not accidentally proxying to an HTTP endpoint when the API expects HTTPS, or vice versa.
-
Avoid Proxy Loops: Double-check that your proxy endpoint is not redirecting to itself, which can also cause infinite redirects.
The knowledge sources specifically mention that redirect loops often happen due to protocol mismatches (HTTP vs. HTTPS) or authentication requirements that are not met, leading to repeated redirects to a login page or similar endpoint. Fixing the protocol or providing the correct authentication should resolve the issue Stack Overflow: Nuxt SSR DEV mode: proxyRequest does not work with node 19+ (works with 18).
If you continue to have issues, log the response headers from the API to see where the redirect is pointing, and adjust your proxy configuration accordingly.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@amber kite Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. One moment! :books:
Based on the knowledge sources provided, there is no explicit, detailed information about the internal implementation of the proxyRequest() function or the underlying proxy() function in h3js, nor about specific bugs or redirect handling within these functions.
However, the sources do confirm that proxyRequest() is commonly used in Nuxt 3 server API endpoints to forward requests to another backend, and issues such as "redirect count exceeded" can occur if the target API is redirecting (for example, from HTTP to HTTPS or to a login page). The error is typically a result of the behavior of the target API or the way the proxy is configured, rather than a confirmed bug in proxyRequest() or h3's proxy() itself. Adjusting headers (like setting connection: 'keep-alive') or ensuring the correct protocol is used can resolve such issues, as seen in the provided troubleshooting steps and user experiences Stack Overflow: Nuxt SSR DEV mode: proxyRequest does not work with node 19+ (works with 18).
In summary:
The knowledge sources do not provide enough information to definitively state whether the issue is within proxyRequest(), h3's proxy(), or is solely due to external API behavior. The redirect loop is most often caused by the target API's response or misconfiguration, not a confirmed bug in these proxy functions.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@amber kite Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. One moment! :books: