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]

guest
1 Comment
Inline Feedbacks
View all comments
양서홍
양서홍

eeee