Last active
March 20, 2021 12:34
-
-
Save g105b/a420f5cd999082d9cd3cc2e36237ad61 to your computer and use it in GitHub Desktop.
Nginx configuration for CDN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = $_SERVER["SERVER_NAME"]; | |
$cacheDir = $_SERVER["DOCUMENT_ROOT"]; | |
$uriPath = $_SERVER["REQUEST_URI"]; | |
$uriPath = strtok($uriPath, "?"); | |
$uriFull = "https://" . $host . $uriPath; | |
$cacheFilepath = $cacheDir . "/" . ltrim($uriPath, "/"); | |
if(substr($cacheFilepath, -1) === "/") { | |
$cacheFilepath .= "index.html"; | |
} | |
$responseBody = ""; | |
$ch = curl_init($uriFull); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header):int { | |
$headerLen = strlen($header); | |
$header = trim($header); | |
if(strlen($header) === 0) { | |
return $headerLen; | |
} | |
if(!strstr($header, ":")) { | |
return $headerLen; | |
} | |
header($header); | |
return $headerLen; | |
}); | |
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $chunk) use(&$responseBody):int { | |
ob_start(); | |
echo $chunk; | |
ob_end_flush(); | |
$responseBody .= $chunk; | |
return strlen($chunk); | |
}); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
"X-BrightFlair-CDN: $uriFull", | |
"User-Agent: BrightFlair CDN", | |
]); | |
curl_exec($ch); | |
$info = curl_getinfo($ch); | |
curl_close($ch); | |
http_response_code($info["http_code"]); | |
if(!is_dir(dirname($cacheFilepath))) { | |
mkdir(dirname($cacheFilepath), 0775, true); | |
} | |
file_put_contents($cacheFilepath, $responseBody); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
server_name _; | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
listen 443 ssl default_server; | |
listen [::]:443 ssl default_server; | |
# Dynamic $ssl_server_name requires permissions to /etc/letsencrypt/archive directory. | |
# Add www-data group to the directory structure with executable permissions. | |
ssl_certificate /etc/letsencrypt/live/$ssl_server_name/fullchain.pem; | |
ssl_certificate_key /etc/letsencrypt/live/$ssl_server_name/privkey.pem; | |
root /var/www/cdn/static/$host; | |
location / { | |
try_files $uri $uri/index.html $uri/index.htm @cdn; | |
add_header X-Debug 1234; | |
sendfile on; | |
sendfile_max_chunk 1m; | |
} | |
location @cdn { | |
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; | |
include fastcgi_params; | |
fastcgi_param SERVER_NAME $host; | |
fastcgi_param SCRIPT_FILENAME /var/www/cdn/cdn.php; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment