I've got an Apache webserver proxying some requests to a NodeJS backend. When the user uploads a invalid file, my server terminates the request with an error message. While this works correctly when calling the NodeJS backend directly, a 502 Bad Gateway error ("The proxy server received an invalid response from an upstream server. ") is given when proxied through Apache.
The NodeJS termination (req = request, res = response)
req.pause()
res.statusCode = 400
res.setHeader('Content-Type', 'text/plain');
res.end("Invalid Request: " + e.message);
req.destroy(["Invalid Request: " + e.message])
I'd like to immediately end the upload (to save time & bandwidth), and send an error message to the client, so simply pausing the request doesn't work for me - it leaves the client hanging. Not setting "Connection: close" or destroying the socket causes upload to continue, even after res.end is called with the error message.
I am seeing an "http error before end of send, stop sending" error in Apache, so it's pretty clear that Apache is treating this as a backend server fault, rather than a legitimate error message to be returned to the client. How do I fix this? I've done quite a bit of searching, but haven't found a clear answer.
The current proxy code (not much):
ProxyPass /node http://127.0.0.1:3000/node
More code can be provided upon request.