Intermittent connection refused with cURL on localhost only

January 28, 2018 4.1k views
Networking API Development DNS Ubuntu

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 159.203.11.91...
* TCP_NODELAY set
* connect to 159.203.11.91 port 443 failed: Connection refused
* Failed to connect to api.chrisjones.io port 443: Connection refused
* Closing connection 0
curl: (7) Failed to connect to api.chrisjones.io port 443: Connection refused
2 Answers
chrisjonesio January 30, 2018
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 127.0.0.1 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 127.0.0.1. After removing this entry connections from the same server started working properly.

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

Have another answer? Share your knowledge.