Apache timeout caused by PHP getimagesize over https. Works fine on http

by Yamraj   Last Updated January 04, 2018 18:00 PM

It looks like PHP getimagesize() is causing some issues on apache over https. What happens is that when I call this over https, there is a lag equal to the timeout setting in the apache conf file before the page loads. For example, if I set the apache timeout value to 12 seconds, then the call hangs for 12 seconds before getting executed. If I access the same script over http, everything works fine. I've written a bit of debugging code below:

<?php
echo "The start time is " . date("h:i:sa");
echo "<br>";
$img_url = url()."/assets/img/logo/45.jpg";
    print $img_url;
echo "<br>";
echo "The time before getimagesize() is " . date("h:i:sa");
echo "<br>";
    $img_dimen = getimagesize($img_url);
    print_r($img_dimen);
echo "<br>";
echo "The time after getimagesize() is " . date("h:i:sa");
echo "<br>";

function url(){
    return sprintf(
        "%s://%s%s",
        isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
        $_SERVER['SERVER_NAME'],
    dirname($_SERVER['REQUEST_URI'])
);
}
?>

The output over https is:

The start time is 07:02:17pm
https://myserver.mydomain.com/assets/img/logo/45.jpg
The time before getimagesize() is 07:02:17pm
Array ( [0] => 226 [1] => 78 [2] => 2 [3] => width="226" height="78" [bits] => 8 [channels] => 3 [mime] => image/jpeg ) 
The time after getimagesize() is 07:02:30pm

As can be seen, it takes 13 seconds to complete the getimagesize() call.

The output over http is:

The start time is 07:05:30pm
https://myserver.mydomain.com/assets/img/logo/45.jpg
The time before getimagesize() is 07:05:30pm
Array ( [0] => 226 [1] => 78 [2] => 2 [3] => width="226" height="78" [bits] => 8 [channels] => 3 [mime] => image/jpeg ) 
The time after getimagesize() is 07:05:30pm

The timeout in apache.conf is set to 12. If I change this timeout value, then the time taken over https changes accordingly. The size of the image is just 32KB.

My vhost looks like:

<VirtualHost *:443>
ServerName myserver.mydomain.com
ServerAlias myserver.mydomain.com
DirectoryIndex index.php
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /root/apache-keys/mycert.crt
SSLCertificateKeyFile /root/apache-keys/mykey.key
SSLCertificateChainFile /root/apache-keys/mybundle.crt
<Directory "/var/www/html">
   Options FollowSymLinks
   AllowOverride All
   Require all granted
</Directory>
</VirtualHost>

The vhost conf file contains a section for *:80 with the same details as the above. There are other sites (different vhosts for different subdomains) hosted on this server. The vhost for each follows the same pattern as this one (*:443, *:80) and they are all in different .conf files. Most of them have self-signed certs while one of them shares the cert with this site (the cert is a wildcard cert). Changing the cert to a self-signed one didn't help either. Neither did disabling the other site that shares the same cert.

The error.log shows the below for some of the other sites:

[Wed Jan 03 08:07:03.067875 2018] [ssl:warn] [pid 1328] AH01909: RSA certificate configured for othersite1.mydomain.com:443 does NOT include an ID which matches the server name
[Wed Jan 03 08:07:03.068056 2018] [ssl:warn] [pid 1328] AH01909: RSA certificate configured for othersite2.mydomain.com:443 does NOT include an ID which matches the server name

Besides the above, there are no other errors in the error.log.

I've checked the site in ssllabs.com and there are no issues with the certs.

The output of apache2ctl -S shows that this site is the default server for both port 80 and port 443. I've tried disabling all other sites and yet this occurs.

Some more details: Apache version is 2.4.7. PHP version is 5.6.30 OS is Ubuntu 14.04

Any help is much appreciated. Please let me know if any additional information is required. I'd really like to get to the bottom of why does getimagesize cause an apache timeout before returning data over https.



Related Questions



Nginx HTTP to HTTPS redirect is not working

Updated June 12, 2017 15:00 PM

Apache won't redirect HTTP to HTTPS

Updated December 11, 2017 23:00 PM


mod_firehose for Apache 2.2?

Updated July 11, 2015 13:00 PM