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