Simple CDN service module made with PHP

Simple CDN service module made with PHP

I used Cloudflare to reduce the traffic load, but the connection location was LAX (Los Angeles), so I made it slowly to use it and urgently used it in the form of CDN.

The server is located in VULTR.

The principle is that kilho.net/ {file} is connected to cdn.kilho.net/ {file} to disperse the traffic of the main hosting.

[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’])
{
Case ‘Get’:
$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)
{
Header ('http/1.0'. $ fileTime);
} else {
Include ‘_mime.php’;
$filetype = get_mime($filename);
$lastmodified = filemtime($filename);
$etag = md5_file($filename);

Header ('Content-Type:'. $ FileType);
Header ('Expires:' .gmdate ('D, D M y: I: s', Time ()+77760000). 'GMT');
Header ('cache-control: public, max-ages = 77760000');
Header ('Last-modified:' .gmdate ('d, d m y: I: s', $ lastmodified). 'GMT');
Header (‘e:’. $ etag);
Header (‘‘ ‘‘ ‘‘ ’’ ’’ ’’ ’’ ’);

if(isset($_SERVER[‘HTTP_IF_MODIFIED_SINCE’]))
{
if($_SERVER[‘HTTP_IF_MODIFIED_SINCE’]== GMDATE[‘HTTP_IF_NONE_MATCH’]==$etag)
{
Header ('http/1.0 304 not modified');
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;

Case ‘POST’:

Case ‘PUT’:

Case ‘Delete’;
break;
}

Header ('http/1.0 404 File not found.');
?>

[/PHP]

Comment (1)

Leave a Reply

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