I get an issue with ExpressJS. I get an error message when uploading large files...
Error message :
Bad Gateway
The proxy server received an invalid response from an upstream server.
Apache/2.4.7 (Ubuntu) Server at xxxxxxxxx Port 80
Everything works when the connexion of the user is above 3G...
Is there any configuration to do on my Apache or about ExpressJS options ?
Edit #1 :
Sounds like the reason is a timeout happening on express server. Try increasing the timeout for the express connections.
const server = app.listen(8080);
server.timeout = 300000; // 5min = 5*60*1000
Related
What the title says. I ran into a bug where the issue was an express endpoint not ending the request connection which caused it to seemingly hang. I am still confused how the request and response flow looks like.
The Express http server object has a configurable timeout and after that timeout with no response on the http connection, the server will close the socket.
Similarly, most http clients at the other end (such as browsers) have some sort of timeout that will likely close the TCP socket if they've been waiting for a response for too long.
The http server timeout is built into the underlying http server object that Express uses and you can see how to configure its timeout here: Express.js Response Timeout. By default, the nodejs http server timeout is set to 0 which means "no timeout" is enforced.
So, if you have no server timeout and no client timeout, then the connection will just sit there indefinitely.
You can configure your own Express timeout with:
// set server timeout to 2 minutes
server.timeout = 1000 * 60 * 2;
See doc here.
Where server is the http server object created by either http.createServer() or app.listen().
Hello I'm trying to deploy an express app that uses Socket.io for communication with the node server it's hosted on.
First problem : the server couldn't find the socket.io node module
somehow... even though it gets resolved on localhost.
We then switched to loading the socket.io client side through the socket.io cdn.
But we are getting this being spammed in the console :
Failed to load resource: the server responded with a status of 404
(Not Found)
https://pictionar-e.herokuapp.com/socket.io/?EIO=3&transport=polling&t=Lbg7iEM
From this error is looks like socket.io is trying to communicate over polling ? But I don't get why.. heroku supports websockets. Could https cause things not to work anymore?
Server side we've got the socketio/express server setup like this :
app=express();
server=require("http").Server(app);
server.listen(port,function () {
console.log("Server started at port: "+port);
});
io=require("socket.io")(server);
On localhost everything works fine but when on heroku the socket.io doesn't work, while the express server does its thing perfectly fine...
Note: we are using the port that gets assigned automatically by the heroku environment
I have a problem with socket.io#^1.0. The setup is fine because it works locally, the server is correctly configured and when i try to connect to the server from my Angular APP it works fine with this:
io.connect("localhost:8080");
The connection is established and i can send and receive event. Now in the production environment, "locahost:8080" is replaced with the address of the server Launched:
io.connect("https://domain-name.com/api");
I know that the problem here is the /api, since socket.io is considering it as a namespace and it's trying to connect to it, in my network console I see 500 Internal server error with the address https://domain-name.com without the /api when i replace the request url to add the /api I get a 200 OK with type octet-stream.
So the question here is: how do I connect to the correct path without consideration of the namespace?
Thanks in advance for any help :)
I think you want to use the path option (documented here):
// client
var socket = io.connect('https://domain-name.com/', {
path : '/api/socket.io'
});
For some reason http-proxy causes socket.io based websocket connection reconnect after every 2 minutes. Before reconnection messages are working just fine between client and server. If I bypass proxy, the websocket connection works without reconnections. Proxy configuration is very basic and follows an example from nodejitsu.
var http = require('http'),
httpProxy = require('http-proxy');
var options = {
hostNameOnly: true,
router: {
'example.com/sockets/': '127.0.0.1:9001'
}
};
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(80);
I have also tried to change the timeout option in configuration but this does not have an affect to the reconnection problem.
timeout: 120000 // override the default 2 minute http socket timeout value in milliseconds
Software versions: Ubuntu 12.04 server, node.js 0.8.16, http-proxy 0.8.7, socket.io 0.8.7.
This works perfectly on dev Mac (10.8.3) and on Ubuntu desktop 12.04 (virtualbox) but not on server.
Set the timeout in options which you are passing to createServer.
options.timeout for socket timeout
and options.proxyTimeout to allow outgoing socket to timeout so that we could show an error page at the initial request.
I'm actually doing some load testing against an ExpressJS server, and I noticed that the response send by the server includes a "Connection: Keep-Alive" header. As far as I understand it, the connection will remain opened until the server or the client sends a "Connection: Close" header.
In some implementations, the "Connection: Keep-Alive" header comes up with a "Keep-Alive" header setting the connection timeout and the maximum number of consecutive requests send via this connection.
For example : "Keep-Alive: timeout=15, max=100"
Is there a way (and is it relevant) to set these parameters on an Express server ?
If not, do you know how ExpressJS handles this ?
Edit:
After some investigations, I found out that the default timeout is set in the node standard http library:
socket.setTimeout(2 * 60 * 1000); // 2 minute timeout
In order to change this:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("Hello World");
}).on('connection', function(socket) {
socket.setTimeout(10000);
}).listen(3000);
Anyway it still looks a little bit weird to me that the server doesn't send any hint to the client concerning its timeout.
Edit2:
Thanks to josh3736 for his comment.
setSocketKeepAlive is not related to HTTP keep-alive. It is a TCP-level option that allows you to detect that the other end of the connection has disappeared.
For Express 3:
var express = require('express');
var app = express();
var server = app.listen(5001);
server.on('connection', function(socket) {
console.log("A new connection was made by a client.");
socket.setTimeout(30 * 1000);
// 30 second timeout. Change this as you see fit.
});
To set keepAliveTimeout on the express server do:
var express = require('express');
var app = express();
var server = app.listen(5001);
server.keepAliveTimeout = 30000;
For Node.js 10.15.2 and newer with express, only server.keepAliveTimeout was not enough. We also need to configure server.headersTimeout longer than server.keepAliveTimeout.
server.keepAliveTimeout = 30000;
// Ensure all inactive connections are terminated by the ALB, by setting this a few seconds higher than the ALB idle timeout
server.headersTimeout = 31000;
// Ensure the headersTimeout is set higher than the keepAliveTimeout due to this nodejs regression bug: https://github.com/nodejs/node/issues/27363
Update
Since this issue Regression issue with keep alive connections has been closed. We could just set keepAliveTimeout on the latest node.js version.
One more thing, If your node.js server is deployed under AWS ELB and encounters 502 error code occasionally.
Clients -> AWS ELB -> Node Server
AWS ELB has 60 seconds of connection idle timeout by default, and per doc
We also recommend that you configure the idle timeout of your application to be larger than the idle timeout configured for the load balancer
Config the value of keepAliveTimeout to greater than 60 seconds could be one option to eliminate this issue.