Fixing Nginx Docker error “16440 upstream prematurely closed connection”
Here at Bobcares, we help Website owners, SaaS providers and Hosting companies run highly reliable web services.
One way we achieve that is by having server experts monitor production servers 24/7 and quickly resolving any issue that can affect uptime.
Recently, engineers at our 24×7 monitoring service noticed that a site hosted by our customer in a Docker server was offline.
We quickly logged in to the Docker host system and looked at the log files. This error showed up from the Nginx Gateway Docker container:
16440 upstream prematurely closed connection while reading response header from upstream, client:
What does this error mean?
Every site hosted in this Docker server used an Nginx gateway to act as a fast reverse proxy.
So, all visits to any site will first come to the Nginx container, and then to the website’s Docker container as shown here:
The part of the error that says “upstream prematurely closed connection” means that the Nginx container was unable to get website content from the back-end website.
So, it is clear that the Website’s Docker container has a faulty web server.
How we fixed “16440 upstream prematurely closed connection”
Getting at the root cause
Once we determined that the web server in the website’s Docker container isn’t working, we opened a terminal and looked at its log file.
We saw this error:
[10498834.773328] Memory cgroup out of memory: Kill process 19597 (apache2) score 171 or sacrifice child
[10498834.779985] Killed process 19597 (apache2) total-vm:326776kB, anon-rss:76648kB, file-rss:10400kB, shmem-rss:64kB
It means that the Docker container is killing the web server processes due to excess memory usage.
Determining how it happened
How did the website that worked fine till now suddenly start using up more memory?
We looked at the website’s file changes, and found that it was recently upgraded with additional plugins.
Additionally, we saw an increasing trend in the website traffic as well.
So, that means each visit to the site requires executing more scripts (there by taking more memory per connection), and there are a higher number of connections than 1 month back.
At this point, we had 2 solution alternatives:
- Remove the new plugins.
- Increase the memory allowance to the Docker container.
Considering that the website’s traffic is on an increasing trend, removing the plugins will only be a stopgap measure.
So, we contacted the website owner (on behalf of our customer, the Docker web host) and recommended to buy additional memory.
Resolving the issue
The website owner approved the upgrade and we increased the Docker memory by adjusting the “mem_limit” parameter in the Docker Compose configuration file.
Once the container was recreated, the webserver started working fine again, and the site came back online.
Remember: There are other reasons for this error
While the issue presented here occurred in a Docker platform, it can happen in any system that runs Nginx web server.
Also, memory limit is one among the many reasons why a Docker container might fail.
So, if you are facing this error, and is finding it difficult to fix this error, we can fix it for you in a few minutes.