The server that I'm trying to proxy is an API entrance(parse-server), which means the main traffic should be in JSON.

And API error responses have their corresponding HTTP status codes and JSON messages.

The problem I'm having is that when the parse-server returns 404 not found, in its context which means the resource is not found, the nginx proxy will interpret that as a 502 bad gateway error and present that with a bunch of unrelated hint texts to my client, which messes up with the status code and api JSON format. I end up losing the correct status code and JSON feedback.

This is how the origin api server respond:

{"message":"Not Found","error":{}}

with http status code 404

Then the proxy interpret that into HTML codes:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
    <head>
        <title>502 Bad Gateway</title>
    </head>
    <body bgcolor="white">
        <h1>502 Bad Gateway</h1>
        <p>The proxy server received an invalid response from an upstream server. Sorry for the inconvenience.
            <br/>
Please report this message and include the following information to us.
            <br/>
Thank you very much!
...

The current nginx config that i'm using:

location /
{
    expires 0;
    proxy_pass https://parseapi.back4app.com;
    proxy_set_header Host parseapi.back4app.com;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

I'm wondering if there's any other option that I can play around to make the proxy return the error response with exactly the same status code and content as they are sent from the original API server.

This is how I expect the proxy would respond:

{"message":"Not Found","error":{}}

which is exactly the same as the origin api server sends

0 Answers