WordPress Site Health

cURL error 28: Connection timed out

One of my work WordPress sites is hosted internally and requires an upstream proxy to access the internet. I’ve had issues with the CRON jobs failing and the cURL error 28 message appearing, but never managed to figure out the issue until now. WordPress 5.1 brought in a new feature in the Tools menu called…

One of my work WordPress sites is hosted internally and requires an upstream proxy to access the internet. I’ve had issues with the CRON jobs failing and the cURL error 28 message appearing, but never managed to figure out the issue until now.

WordPress 5.1 brought in a new feature in the Tools menu called Site Health. The aim of this tool is to check your WordPress installation for recommended settings and for any faults. It will give an overall site score as well as listing any issues detected under appropriate headings. In some cases, it will also display information about how to resolve the issue.

I had previously configured the wp-config.php file with the appropriate information detailing the address and port of the upstream proxy.

define(‘WP_PROXY_HOST’, ‘proxy.serveraddress.internal’);
define(‘WP_PROXY_PORT’, ‘8080’);
define(‘WP_PROXY_USERNAME’, ”);
define(‘WP_PROXY_PASSWORD’, ”);
define(‘WP_PROXY_BYPASS_HOSTS’, ‘localhost’);

wp-config.php – example proxy configuration.

With a proxy server configured, plugins could be downloaded and installed, and WordPress updates get installed. However, despite this, the WordPress Health Check was showing errors for:

The REST API encountered an error

The REST API is one way WordPress, and other applications, communicate with the server. 
One example is the block editor screen, which relies on this to 
display, and save, your posts and pages.

The REST API request failed due to an error.
Error: [] cURL error 28: Resolving timed out after 10000 milliseconds

Your site could not complete a loopback request

Loopback requests are used to run scheduled events, and are also
used by the built-in editors for themes and plugins to verify
code stability.

The loopback request to your site failed, this means features
relying on them are not currently working as expected.
Error: [] cURL error 28: Connection timed out after 10000 milliseconds

After the usual Google searches, I came across lots of similar documented issues, but no real resolution that worked. The message suggests that there is a time out, but the proxy setting is being honoured right?

Eventually google lead me to
https://www.samuelaguilera.com/post/curl-error-28-wordpress which gave me a starting point that helped resolve the issue.

The solution

The solution was to create or to add to the functions block of the theme a small number of functions which control the options that cURL uses.

I have shamelessly lifted the code from https://www.samuelaguilera.com/post/curl-error-28-wordpress and have added my additions to the sar_custom_curl_timeout function.

<?php
// NOTE: THE CODE TO COPY/PASTE STARTS *BELOW* THIS LINE

// Setting a custom timeout value for cURL. 
// Using a high value for priority to ensure the function 
// runs after any other added to the same action hook.
add_action('http_api_curl', 'sar_custom_curl_timeout', 9999, 1);
function sar_custom_curl_timeout( $handle ){
 curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 5 ); 
 curl_setopt( $handle, CURLOPT_TIMEOUT, 5 ); 
curl_setopt( $handle, CURLOPT_PROXYPORT, 8080);curl_setopt( $handle, CURLOPT_PROXY, 'proxy.servername.internal');
}

// Setting custom timeout for the HTTP request
add_filter( 'http_request_timeout', 'sar_custom_http_request_timeout', 9999 );
function sar_custom_http_request_timeout( $timeout_value ) {
 return 5; 
}

// Setting custom timeout in HTTP request args
add_filter('http_request_args', 'sar_custom_http_request_args', 9999, 1);
function sar_custom_http_request_args( $r ){
 $r['timeout'] = 5;
 return $r;
}

Adding these functions into the themes functions.php file immediately resolved the issues detected by the Site Health tool. It also resolved the issues that I had been having with WordPress Cron jobs.

Similar Posts

  • Take control of Domain Passwords with Password Control

    There are many ways to change passwords on a Windows Active Directory Domain.  Users can change their own passwords on the workstation.  However for Network administrators there are not any built-in tools for administrating other user network passwords, unless you Remote Desktop to the Domain Controller or have installed the local support tools for Active…

  • Need to copy files across a network? Use RoboCopy

    If you are migrating a file server or just have lots of files to copy from one networked machine to another, then instead of using the Windows Explorer method of copying the files, why not use RoboCopy?

  • Documenting scripts and source code

    When writing applications, it is vital to be able to look back at your code and understand exactly what each line of code does as quickly as possible. Retrospectively Documenting scripts and source code is time consuming and prone to error, so its best to write the comments either as you are writing the code,…

  • Microsoft Baseline Security Analyzer

    You can never be to careful when it comes to protecting your file servers and important workstations.  Windows Update doesn’t always provide all the current updates and security patches to protect your systems. Microsoft has a free solution that will help to identify security problems in your network.  It is called the Microsoft Baseline Security…

  • Internet Explorer Proxy settings

    Here is a bit of VB Script that will change the proxy settings of a machine if you are unable to access the Internet Explorer control panel or the Windows Registry. USE AT YOUR OWN RISK

  • Internet Explorer Max Connections

    If you are using a broadband connection it can be quite frustrating having to wait for Internet Explorer to finish rendering a web page.  Internet Explorer has a built in limit to the number of items that can be downloaded at any one time.  This limitation has been defined as part of the HTTP Specifications. …