AlxVallejo
AlxVallejo

Reputation: 3219

Configure keep-alive for EC2 instance

I have a Django app running on a python instance with Nginx as the webserver.

I'm getting a 60 second timeout for one of my operations. According to the docs, you want to increase the load balancer's idle timeout above the default 60 seconds (https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html)

It also says:

we recommend that you enable the HTTP keep-alive option for your EC2 instances. You can enable HTTP keep-alive in the web server settings for your EC2 instances

Well I'm not sure how to do this or where that setting is. Can anyone point me to where the keep-alive option is?

Upvotes: 12

Views: 17912

Answers (5)

Asha
Asha

Reputation: 21

If you're using load balancer, check if the keep-alive timeout setting on your EC2 instances is greater than the idle timeout setting of the load balancer.

Upvotes: 1

Adem Tepe
Adem Tepe

Reputation: 768

I have had the same problem several times for months: “504 Gateway Timeout: The gateway did not receive a timely response from the upstream server or application.” Yeah, ideally maybe no request should take more than 60 seconds but sometimes we need it. Here are what I did:

  1. Increased “Environment > Configuration > Updates, monitoring, and logging > Platform software > Max execution time” like we do in local development environment, where it is sufficient! But guess what? It wasn’t sufficient for AWS Elastic Beanstalk.
  2. So I started to search for extra information to catch what I missed and I encountered this: https://aws.amazon.com/tr/blogs/aws/elb-idle-timeout-control/ So, a new term was added to my terminology: Idle Timeout (for Load Balancers). I found this months ago but tried and again… failed. I really couldn’t understand why after changing these two settings, my requests still get cut at 60 seconds. How annoying!
  3. So, this time, I thought I could find better and more up-to date information on AWS Documentation and I found this: https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/config-idle-timeout.html And this time, a new term drew my attention: “HTTP keep-alive”. Good, but where to find it and how to set it? Not told. It is what I was searching and I came here under this question.

The previous answers gave me clues for finding the answer and here is my solution:

  1. I found the Apache configuration file “/etc/httpd/conf/httpd.conf”
  2. I increased Timeout and KeepAliveTimeout values: enter image description here This article may also be helpful: https://www.fastcomet.com/kb/enable-keep-alive-apache
  3. I tried the request again but didn’t work. I went to Elastic Beanstalk environment and did “Actions > Restart app server(s)” (excitement, heart goes bump bump) and… it worked! It’s good to see the green :) enter image description here
  4. Now I have a new question: is it still going to work if I make a new deployment or if there is a new instance? So, I try that: The values changed back! :( enter image description here

Upvotes: 2

babu endla
babu endla

Reputation: 11

for apache2 cd /etc/apache2/ vi apache2.conf find out keep alive option there

Upvotes: 1

Laxmana Polisetti
Laxmana Polisetti

Reputation: 11

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
**keepalive_timeout   65;**
types_hash_max_size 2048;

Upvotes: 1

Shane Nayler
Shane Nayler

Reputation: 307

I'm running into the same issue now and have come to the conclusion that it's just a poorly worded bit of docco and as Mark B suggests in his comment, it's referring to setting the Keep-Alive header from your nginx/apache webserver config rather than on the ec2 instance itself.

Upvotes: 5

Related Questions