Intermittent connection refused with cURL on localhost only

After updating my droplet running Ubuntu 17.10 a week ago I have been experiencing intermittent “Connection refused” errors when making requests to my API using cURL.

After spending hours narrowing down the cause of this I am no further ahead.

I have an API setup running the Slim framework. When requesting the API using cURL from any other machine on the internet – whether through Postman or cURL on the command line – I get the expected results: a 200 response with JSON body. Always.

This also works a little over half the time when making the request with cURL from the same server the API service is running on (my website is running on the same server). After about an hour and a bit, I start getting “Connection refused” errors. Requests from any other machine on the internet still work. Requests from the same server return “Connection refused”. This happens for both ports 80 and 443.

Using only HTTP/1.1 on the API vhost (I have HTTP/2 on other vhosts) does not fix the issue. Restarting Apache does not fix the issue. Restarting MySQL does not fix the issue. Rebooting the droplet does not fix the issue. No changes to the firewall have been made.

The only thing that fixes the issue is time. If I just wait, and DO NOTHING, the problem resolves itself and there are no more “Connection refused” errors until another hour or so goes by and they appear again.

What could possibly be happening here? Why do requests from any other client on the internet always work but requests from the same server intermittently fail?

I am at the end of my debugging rope and have no idea what to try next. This seems to have started occurring after updating and rebooting my droplet. It had been running problem-free for months prior to that.

Any suggestions welcome!

For thoroughness, this is the output from cuRL when failing:

*   Trying
* connect to port 443 failed: Connection refused
* Failed to connect to port 443: Connection refused
* Closing connection 0
curl: (7) Failed to connect to port 443: Connection refused

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

I’ll answer my own question for anyone else having the same issue.

After days of trying to figure out what the issue was, a Stack Overflow answer on an unrelated topic pointed me in the right direction. I knew that when getting the connection refused error cURL was having difficultly resolving the domain only when being called on the same server for some reason (still don’t know why).

So, adding a record to /etc/cloud/templates/hosts.debian.tmpl pointing the misbehaving domain to seems to have done the trick. Of all the domains/subdomains I have there is only one having this issue. Again, I still have no idea why but at least the connection refused errors have ceased.

Thanks a lot for your post.

I was having kind of the same issue although mine wasn’t intermittent. We probably had different issues but your post lead me into investigating domain.

In my case the problem was an entry on /etc/hosts pointing my service domain to After removing this entry connections from the same server started working properly.

Leaving this here so it may help somebody else in the future.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel