How to resolve a web “Connection timed out” error

“Connection timed out” is an error that occurs as a result of a script exceeding the maximum timeout value.  If a client connection does not receive a response from the server after approximately 30 to 60 seconds the load balancer will close the connection and the client will immediately receive the error message.  In most cases, the script will continue to execute until it reaches completion, throws an error, or times out on the server, but the client will not see the page load as expected and will instead receive this error.  

If you’re seeing timeouts intermittently I would recommend auditing your code, because that likely means it could use optimization or needs to be broken down into smaller pieces.  For instance, if you were inserting several records into a database, try inserting a few at a time and then sending back a response to the client’s browser. 
For longer running scripts in PHP you can use the ‘exec’ function to run scripts in the background and have the script write its status to a database or a file.  Then you could use AJAX to display a loading bar and check the script’s status.  Once it’s completed you can then remove the loading bar and proceed to a completion page.  This is just an example, but the concept is good for anything.  
Another option is to run the script as a cron job using PHP or Perl instead of HTTP, which circumvents the load balancer’s timeout.  The bottom line when it comes to code causing this error is that data must be sent back to the client’s browser to keep the connection alive.  So regardless of what sort of processing a page is doing it must return data frequently or the load balancer will assume the connection has been dropped and will forcefully terminate it.  
Another common cause is that your site is trying to load files that don’t exist (404 errors).  This dramatically slows a site down and in rare cases can cause a timeout.  More commonly a site that is loading data from an external location can see load issues causing a timeout.  
For instance, if your site relies on Google Analytics, Authorize.net, or PayPal and for some reason these corresponding services go down or begin responding slowly, your site will see a performance hit that in some cases can cause the page not to load or to load intermittently. 

 Keep in mind that all sorts of plugins for content management systems like WordPress and Joomla use third-party services behind the scenes, such as bit.ly and Twitter.  It’s also a possibility that the script simply uses a heavy amount of resources.  If this is a PHP site you may need to try increasing the memory limit-which defaults to 32MB-to something more appropriate for your site, such as 64M or even 128M in extreme cases.  



Leave a Reply