Cloudflare Content Type

3k Views Asked by At

I have just started to use CloudFlare and have run into an issue with it's CDN service. I want my script to send back a the Content-Type header. I have tried to use page rules but doesn't help one bit. I just want to get the content length!

PHP Script:

<?php
    ob_start();
    ob_start('ob_gzhandler');

    $file = $_SERVER['DOCUMENT_ROOT'] . "/MyPath.txt"; //Path to your *.txt file 
    $contents = file($file); 
    $string = implode($contents); 
    echo $string;
    ob_end_flush();  // The ob_gzhandler one
    header('Content-Length: '.ob_get_length());
    ob_end_flush();  // The main one

?>

CloudFlare's Response Header:

HTTP/1.1 200 OK
Date: Wed, 19 Aug 2015 17:29:33 GMT
Content-Type: text/html
Connection: close
Set-Cookie: __cfduid=d47ae5690afc8da407e829810c558510b1440005373; expires=Thu, 1
8-Aug-16 17:29:33 GMT; path=/; domain=.mysite.com; HttpOnly
X-Powered-By: PHP/5.5.28
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Set-Cookie: PHPSESSID=f7b9cb0b5e9bf7e9af1d732e566a1e24; path=/
Server: cloudflare-nginx
CF-RAY: 218794cf540e0436-ORD

LocalHost Header:

HTTP/1.1 200 OK
Date: Wed, 19 Aug 2015 17:39:10 GMT
Server: Apache/2.4.9 (Win64) PHP/5.5.12
X-Powered-By: PHP/5.5.12
Set-Cookie: PHPSESSID=s3hdkbh4vd2rj5ieqocafebju6; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Length: 135680
Connection: close
Content-Type: text/html
1

There are 1 best solutions below

3
On

You will need to either add a file extension so that the output from your script gets cached, or use page rules to force the resource to be cached. This will cause the file not to be sent using chunked encoding, which will enable the content-length header.

From the CloudFlare help:

The solution/workaround - If you add a file extension to the resource so that it matches our list of supported file extensions so http://example.com/test/dynamicallyimage.php?size=3 becomes http://example.com/dynamicallyimage.jpg CloudFlare's system will then send it with the content-length header as long as you're also sending HTTP 1.0 as the protocol.

Alternatively you could use a PageRule and use the "custom caching" option to select "cache everything" which will force our system to cache http://example.com/test/dynamicallyimage.php?size=3 even though it doesn't have one of our usual file extensions -- in this case the content-length will also be preserved.