用PHP制成的简单CDN服务模块

用PHP制成的简单CDN服务模块

我使用CloudFlare减少了交通负荷,但是连接位置是Lax(Los Angeles),因此我将其用作CDN表单。

该服务器位于Vultr中。

原则是kilho.net/ {file}连接到cdn.kilho.net/ {file}以分散主托管的流量。

[PHP]

<?
$cfg[‘url’]='kilho.net’;
$cfg[‘cache’]='../ data/cache’;

$url = $_SERVER[‘REQUEST_SCHEME’]。’://'。$ cfg[‘url’].$_SERVER[‘REQUEST_URI’];
$url_arr = parse_url($url);

switch($_SERVER[‘REQUEST_METHOD’])
{
案例“获取”:
$filename = $cfg[‘cache’].$url_arr[‘path’];
$dirname = dirname($filename);
if(!is_file($filename))
{
if(!is_dir($dirname)) mkdir($dirname, 0707, true);

$ch = curl_init();
$ fp = fopen($ filename,'w');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER[‘REQUEST_SCHEME’]。’://'。$ cfg[‘url’]);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
fclose($fp);

switch($code = curl_getinfo($ch, CURLINFO_HTTP_CODE))
{
case 200:
break;

default:
touch($filename, $code);
}
curl_close($ch);
}

if(is_file($filename))
{
$filetime = filemtime($filename);

if($filetime<1000)
{
标头('http/1.0'。$ filetime);
} else {
包括“ _mime.php”;
$filetype = get_mime($filename);
$lastmodified = filemtime($filename);
$etag = md5_file($filename);

标题('content-type:'。$ filetype);
标头('Expires:'.gmdate('d,d,d m y:i:s',time()+77760000)。'gmt');
标头('CACH-CONTROL:public,max-ages = 77760000');
标头('Last-Modified:'.gmdate('d,d,d m y:i:s',$ lastModified)。'gmt');
标头('e:'。$ etag);
标头('''''’’’’’’’);

if(isset($_SERVER[‘HTTP_IF_MODIFIED_SINCE’]))
{
if($_SERVER[‘HTTP_IF_MODIFIED_SINCE’]== gmdate[‘HTTP_IF_NONE_MATCH’]==$etag)
{
标头('HTTP/1.0 304未修改');
exit;
}
}
}

if(strstr($_SERVER[‘HTTP_ACCEPT_ENCODING’],“ gzip’)))
if(in_array($ filetype,array”('text/plain','text/html','text/css','text/javascript','application/json','application/xml'
ob_start('ob_gzhandler');

$ fp = fopen($ filename,'rb');
fpassthru($fp);
fclose ($fp);

exit;
}

break;

案件“邮局”:

案例“放”:

案件“删除”;
break;
}

标头(找不到HTTP/1.0 404文件。');
?>

[/PHP]

Comment (1)

Leave a Reply

이메일 주소는 공개되지 않습니다. (* 질문, 건의사항 등은 "질문게시판"을 이용해주세요)