If you have been debugging the ‘upstream timed out (110: Connection timed out) while reading response header from upstream‘ error, then don’t panic. I have got a solution for you.
Just like you, I googled around several sites and found variety of hacks that were working around the problem but not really solving the root cause. I hate hacks as it doesn’t fix the problem, just hides the real problem. Moreover, hacks will come around to haunt you at a very critical time in life and stress you like HELL!
Alright, enough of preface…
The problem you are facing is that one of your script is not responding within 30 secs (or the timeout you set) in ‘fastcgi_read_timeout’ (nginx.conf). If you expect your script to process longer than 30secs or timeout, then just increase the time to match what you think should be fair value.
However, in my case, I know that the server script should finish its processing in less than 10 seconds. So I keep the timeout at 30 seconds. I got this error. After searching and digging, found out that one culprit script which was taking longer to execute in certain situation and bringing the entire site to halt. The culprit was an API call to external server. So I recommend from my experience now, that don’t ever have an external API call as if the external server is having issue, then you will have an issue and crash your site.
Very Bad Idea… Hence, never ever ever call an external script from the script that servers webpages. Instead use client php when needed.
Now your problem… How will you solve it? Just think for places in your code where the php script will go beyond its alloted timeout. And that’s where you will find the problem.
You need to find out that script which is taking longer than your configured timeout. Once you have found that script, just fix the root cause and voilla, you won’t see the dreaded 110 connection timeout error.