我刚开始使用 CloudFlare,但遇到了它的 CDN 服务问题。我希望我的脚本发回 Content-Type header 。我曾尝试使用页面规则,但一点帮助也没有。我只想获取内容长度!
PHP 脚本:
<?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 的响应 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
本地主机 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
最佳答案
您需要添加文件扩展名以便缓存脚本的输出,或者使用页面规则强制缓存资源。这将导致文件不使用分块编码发送,这将启用内容长度 header 。
来自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.
关于php - Cloudflare 内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32102205/