Skip to content
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

Proxy timeouts after 60 seconds #458

Closed
johanbook opened this issue Jul 16, 2020 · 12 comments
Closed

Proxy timeouts after 60 seconds #458

johanbook opened this issue Jul 16, 2020 · 12 comments

Comments

@johanbook
Copy link

johanbook commented Jul 16, 2020

Is this a bug report?

POST requests with form data seems to be timedout at 60 seconds. Happens in node version 13.x and 14.x but not 12.x. This might be a bug in node-http-proxy.

Steps to reproduce

  1. Create script testUpload.sh (or use scripts/testUpload.sh in supplied repo)
ENDPOINT=$1
FILE=large.file
RATELIMIT=10M

echo "Testing upload to $ENDPOINT"

if ! [ -f "$FILE" ]; then
  echo "Generating test file..."
  dd if=/dev/zero of=$FILE bs=1048576 count=2048
fi

echo "Testing upload at $RATELIMIT"

curl -X POST $ENDPOINT -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "file=@${FILE}" --limit-rate $RATELIMIT --progress-bar --verbose -o ${RATELIMIT}.out
  1. Run script as testUpload.sh URL
  2. Wait for it

After 60 seconds I recieve

...
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0

Simiar behavior is seen in browser.The request gets net::ERR_CONNECTION_RESET with error message TypeError: Failed to fetch in Chrome and TypeError: NetworkError when attempting to fetch resource. in Firefox.

Expected behavior

Request goes on until finished.

Actual behavior

Request vanishes. Our server got yielded client disconnected. Client never receives a response.

Setup

Frontend is created with Create-React-App with a separate API on port 5000. setupProxy.js is configured as

const { createProxyMiddleware } = require("http-proxy-middleware");

const TIMEOUT = 30*60*1000;
module.exports = (app) => {
  app.use(
    "/api",
    createProxyMiddleware({
      target: "http://127.0.0.1:5000",
      changeOrigin: true,
      pathRewrite: {
        "^/api": "",
      },
      proxyTimeout: TIMEOUT,
      timeout: TIMEOUT,
      onError: (err, req, res) => console.log(err)
    })
  );
};

client info

The app is run inside Docker container based on node:14.5.0-alpine. Same error is encountered when run an Ubuntu system. This is the case for node versions 13.x and 14.x but not 12.x.

target server info

A Python FastAPI server that allows file uploads on endpoint POST /v1/storage.

Reproducible Demo

Here is a minimum working example of the front end1l; https://github.com/johanbook/http-proxy-middleware-upload-bug. Bash script included.

@nanepallysaikumar
Copy link

Even we are also facing the same issue, but request is getting timed out for 30 seconds only.
Some one looking against this??
please help @chimurai

@nanepallysaikumar
Copy link

@johanbook Did u find any solution to fix this?

@johanbook
Copy link
Author

@nanepallysaikumar I downgraded my node version to 12.10 which fixed it

@nanepallysaikumar
Copy link

@johanbook But isn't the issue due to the http proxy middleware?.
How come node version is related to the time out issues?
Currently we are using the node version 10.15.0 but we are NOT using in our react application, it is just confined to our local machine.

´´´
const options = {
target: "/urltohit",
changeOrigin: true,
logLevel: 'debug',
pathRewrite: {
'^/api': '/'
},
timeout: 60000,
proxyTimeout: 60000,
onError: (err, req, res) => {
console.log('Error due to server down', err);
res.status(500).send({ error: 'Not Reachable!' });
}
};

const proxyData = proxy(options);

const app = express();
const port = process.env.NODE_PORT || 5000;
app.set('port', port);
app.use(function(req, res, next) {
res.setHeader('x-frame-options', 'DENY');
res.setHeader('X-XSS-Protection', '1; mode=block');
res.setHeader('X-Content-Type-Options', 'no-sniff');
next();
});
app.use(favicon(__dirname + '/build/favicon.ico'));
app.use(express.static(path.join(__dirname, 'build')));
app.use(log);
app.use('/api', proxyData);
app.get('*', function(req, res) {
return res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

// establish http server connection
app.listen(port, () => {
console.log(App running on port ${port});
});
´´´
Can you please give some insights how was it used by you? Currently its getting time out for 30 seconds

@johanbook
Copy link
Author

@nanepallysaikumar my guess is that it was a breaking change between node 12 and 13 that broke http-proxy-middleware.

Well, you are using it in your express server and the error might arise from server-side. However, if you already are using node 10 to run your express application it might be something else. Maybe try upgrading node to 12 and see if there is any difference

@Mousaka
Copy link

Mousaka commented Oct 29, 2020

I seem to have a similar problem using node 14.0.20. Did anyone discover anything except downgrading to 12?

@Mousaka
Copy link

Mousaka commented Oct 29, 2020

Found this line in my code base :D

router.use(express.json()); // http-proxy-middleware stops working if this middleware is added before it.

Removing/Moving that middleware fixed my issue. Hopefully, this PR solves problems like this in the future

@johanbook
Copy link
Author

@johanbook
Copy link
Author

After upgrading my Node version this is no longer a issue

@trisapeace
Copy link

Removing/Moving that middleware fixed my issue.

Thank you so much @Mousaka . This solved my issue! What a headache.

@sckimynwa
Copy link

sckimynwa commented Jun 10, 2022

Found this line in my code base :D

router.use(express.json()); // http-proxy-middleware stops working if this middleware is added before it.

Removing/Moving that middleware fixed my issue. Hopefully, this PR solves problems like this in the future

@Mousaka Thanks for your advice!
I had a problem that this proxy-middlware works properly in GET and DELETE methods,
but always PENDING-CANCELED in POST and PUT methods.

i think this is due to the body(payload) parsing. and when i moved middleware before the router.use(express.json()) It works like a charm. Thanks!

@zymotik
Copy link

zymotik commented Jan 23, 2023

I found this GitHub issue after all my POST requests to the proxied URL were timing out with errors from the upstream host. The timeout could be 30, 60 or 120 seconds depending on the host server technology and configuration. I assumed it was my http-proxy-middleware config, but it's actually because of a proxy middleware before mine that I can't control (in this case, a Google Cloud Function). To fix, I followed #492.

Simply:

const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware');

const proxy = createProxyMiddleware({
  //...usual config here
  onProxyReq: fixRequestBody,
});

// or
const proxy2 = createProxyMiddleware({
  //...usual config here
  onProxyReq: (proxyReq, req) => {
    // do what you want first, then
    fixRequestBody(proxyReq, req);
  },
});

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

6 participants