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 22.214.171.124... * TCP_NODELAY set * connect to 126.96.36.199 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
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.×