Skip to content

ECONNRESET when POSTing to a proxy through connect server #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
robsquires opened this issue Oct 11, 2013 · 7 comments
Closed

ECONNRESET when POSTing to a proxy through connect server #496

robsquires opened this issue Oct 11, 2013 · 7 comments

Comments

@robsquires
Copy link

We have written a proxy to authenticate http requests to several servers, following the recommended design pattern:
https://github.com/nodejitsu/node-http-proxy#proxy-requests-within-another-http-server

We are using connect middleware within this proxy to process cookie authentication and relay this internally to the servers via queryString parameters. The middleware in use is:

connect.urlencoded
connect.multipart
connect.cookieParser
connect.session
connect.csrf

Everything works ok for GET requests.

I'm having trouble with POST requests. Originally the requests were just hanging but I was able to resolve that using the httpProxy.buffer(req).

During a POST response the request is forwarded to the backend, the backend responds and then the proxy throws { [Error: socket hang up] code: 'ECONNRESET' }.

I've done some digging around.

  1. This does not happen with node 0.10.x (but we're proxying websockets as well so need 0.8)
  2. This doesn't happen when we proxy without connect middleware processing the request

Can anyone offer any advice?

connect : ~2.9.0
http-proxy : ~0.8.7
node : 0.8.24

Many thanks

@robsquires
Copy link
Author

The connect.session appears to be the culprit...not sure why :(

@robsquires
Copy link
Author

This is being caused by utils.pause(req); within connect.session.

https://github.com/senchalabs/connect/blob/master/lib/middleware/session.js#L306

Any ideas why this might be messing up the response when handled by the proxy?

@robsquires
Copy link
Author

I think I've got to the bottom of this. I was buffering the request object further up the middleware chain than the connect session. The session calls pause.resume after the rest of the middleware, including the proxy, leading to further data events being emitted. I discovered this by logging inside the data listener in the proxy.

By re-ordering my middleware, buffering after the session everything seems to work OK.

Does anyone have any advice on this solution?

thanks

@matt2000
Copy link

FWIW, I'm having the same symptom (POSTs fail when proxied, everything else works fine), but I'm not buffering.

@fidian
Copy link

fidian commented Jul 24, 2014

This looks like it is related to issue #180 and #326 though those use an older version of http-proxy. It certainly would be nice if http-proxy was changed to work without having to reorder the middleware. :-)

@djmccormick
Copy link

I'm also having this issue.

@jcrugzz
Copy link
Contributor

jcrugzz commented Mar 5, 2015

@djmccormick I will be closing this as it is an old issue but the answer has to do with having any middleware that buffers the request data when attempting to proxy. The solution is to create a separate a proxy middleware to handle the intended routes before hitting that middlware OR create a separate router altogether that has its own middleware chain that does not parse the request.

Hope this helps. If you cannot figure it out, open a new issue with a complete reproducible code sample

@jcrugzz jcrugzz closed this as completed Mar 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants