Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 504 GATEWAY_TIMEOUT NodeJs

I am getting 504 GATEWAY_TIMEOUT http response after 60s of page loading.

It is not an actual page that is being loaded, than rather a process that is being executed. I am expecting it to take longer than 60s and I've tried to increase the timeout value, but it didn't help.

I am using express framework for routing and I host the job on EB (AWS Elastic Beanstalk). Since I have increased all timeout values that I could possibly find on EB and Load Balancers in AWS console, I assume it must be the app itself that has timeout set to 60s. However, I might be wrong.

My code:

/* GET home page. */
router.get('/main',function(req, res, next) {
req.connection.setTimeout(600000);
        mainProcess(res);
        //res.send("mainProcess() called");
    });

UPDATE:

Besides this, I've tried a different approach. I added this code to the app.js:

var connectTimeout = require('connect-timeout');
var longTimeout = connectTimeout({ time: 600000 });
app.use(longTimeout);

Didn't help either.

UPDATE2: I have also tried increasing the timeout in /bin/www like this:

var server = http.createServer(app);
server.timeout=600000;

UPDATE3: I have noticed that the timeout is related to the nginx configuration. As my logs say: upstream timed out (110: Connection timed out) while reading response header However, I can't figure out a way to edit nginx config on Elastic beanstalk. I did some research, but it all seems non standard to me and too rigid for such simple thing.

like image 861
Ondrej Tokar Avatar asked Jul 14 '16 12:07

Ondrej Tokar


People also ask

How does node js handle 504 Gateway Timeout?

I added this code to the app. js : var connectTimeout = require('connect-timeout'); var longTimeout = connectTimeout({ time: 600000 }); app. use(longTimeout);

Why it is showing 504 Gateway Timeout?

The HyperText Transfer Protocol (HTTP) 504 Gateway Timeout server error response code indicates that the server, while acting as a gateway or proxy, did not get a response in time from the upstream server that it needed in order to complete the request.

Is a 504 error my fault?

They are no fault of the client. Your request is good, but the server can not generate the requested resource. The 504 Gateway Timeout error indicates that your web server didn't get a response on time from another server that it was accessing while trying to load the page.


2 Answers

From your Update3 information, I think you should config your nginx configure file, like:

server {
    listen  80;
    server_name     *.*;
    location / {
            proxy_pass http://192.168.0.100:8001;
            proxy_connect_timeout 60s;
            proxy_read_timeout 5400s;
            proxy_send_timeout 5400s;
            proxy_set_header host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_redirect default;
    }
}

proxy_read_timeout and proxy_send_timeout is related to your problem.

like image 56
Ken Avatar answered Oct 11 '22 16:10

Ken


In your .ebextensions config file, add the following code:

container_commands:
  change_proxy_timeout:
    command: |
      sed -i '/\s*location \/ {/c \
              location / { \
                  proxy_connect_timeout       300;\
                  proxy_send_timeout          300;\
                  proxy_read_timeout          300;\
                  send_timeout                300;\
              ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
like image 38
Guilherme Cunha Eloi Santos Avatar answered Oct 11 '22 16:10

Guilherme Cunha Eloi Santos