Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit 58e79f8

Browse files
Adding prerender hook to a CDN static page (#8)
Adding prerender hook to a CDN static page
1 parent 3ecea97 commit 58e79f8

File tree

4 files changed

+70
-42
lines changed

4 files changed

+70
-42
lines changed

configuration/defaults/___server.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ defaults:
1919
config: ~/.server/generated_nginx.conf
2020
prerender:
2121
enabled: false
22+
cache_ttl: 24h # by default cache lives 24 hours
23+
cdn_url: null
24+
cdn_path: / # CDN folder relative to root path (/)
2225
url: null # Proxy pass URL for bots, with schema.
2326
resolver: "8.8.8.8 8.8.4.4 1.1.1.1 1.0.0.1" # nginx "resolver" option to force DNS resolution and prevent caching of IPs
2427
headers: [] # Authorization headers (or others), format: key - $headerName, value - $headerValue

src/Handler/NginxHandler.php

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,26 @@ public function generateConfig(HeaderInterface $header): void
7373
$this->makePathForFiles([$this->pidFile, $this->configFile]);
7474
$this->touchFile($this->pidFile);
7575

76-
$data = $this->templates->render('nginx_default.conf', [
77-
'serverRoot' => realpath($this->getServerRoot()),
78-
'serverIndex' => $this->configuration->get('server.index'),
79-
'serverPort' => $this->configuration->get('server.port'),
80-
'serverHost' => $this->configuration->get('server.host'),
81-
'prerenderEnabled' => $this->configuration->get('server.prerender.enabled'),
82-
'prerenderUrl' => $this->getHostWithoutTrailingSlash('server.prerender.url'),
83-
'prerenderHeaders' => $this->configuration->get('server.prerender.headers', []),
84-
'prerenderResolver' => $this->configuration->get('server.prerender.resolver', false),
85-
'prerenderHost' => $this->getHostWithoutTrailingSlash('server.prerender.host'),
86-
'headers' => $header->convert($this->configuration),
87-
'connProcMethod' => $this->getConnectionProcessingMethod(),
88-
'pidLocation' => $this->pidFile,
89-
'moduleBrotliInstalled' => $this->moduleBrotliInstalled,
90-
'platformSupportsAsyncIo' => $this->platformSupportsAsyncIo,
91-
]);
76+
$data = $this->templates->render(
77+
'nginx_default.conf',
78+
[
79+
'serverRoot' => realpath($this->getServerRoot()),
80+
'serverIndex' => $this->configuration->get('server.index'),
81+
'serverPort' => $this->configuration->get('server.port'),
82+
'serverHost' => $this->configuration->get('server.host'),
83+
'prerenderEnabled' => $this->configuration->get('server.prerender.enabled'),
84+
'prerenderCacheTTL' => $this->configuration->get('server.prerender.cache_ttl'),
85+
'CDNUrl' => $this->getHostWithoutTrailingSlash('server.prerender.cdn_url'),
86+
'CDNPath' => $this->configuration->get('server.prerender.cdn_path'),
87+
'prerenderHeaders' => $this->configuration->get('server.prerender.headers', []),
88+
'prerenderResolver' => $this->configuration->get('server.prerender.resolver', false),
89+
'headers' => $header->convert($this->configuration),
90+
'connProcMethod' => $this->getConnectionProcessingMethod(),
91+
'pidLocation' => $this->pidFile,
92+
'moduleBrotliInstalled' => $this->moduleBrotliInstalled,
93+
'platformSupportsAsyncIo' => $this->platformSupportsAsyncIo,
94+
]
95+
);
9296

9397
file_put_contents($this->configFile, $data);
9498
}
@@ -103,18 +107,23 @@ public function checkConfig(): void
103107
*/
104108
public function start(): void
105109
{
106-
$this->runProcess(['nginx', '-c', $this->configFile], function () {
107-
$this->logger->info(sprintf(
108-
'Server started at: %s:%d',
109-
$this->configuration->get('server.host'),
110-
$this->configuration->get('server.port')
111-
));
112-
});
110+
$this->runProcess(
111+
['nginx', '-c', $this->configFile],
112+
function () {
113+
$this->logger->info(
114+
sprintf(
115+
'Server started at: %s:%d',
116+
$this->configuration->get('server.host'),
117+
$this->configuration->get('server.port')
118+
)
119+
);
120+
}
121+
);
113122
}
114123

115124
public function reload(): void
116125
{
117-
if (!file_exists($this->pidFile)) {
126+
if ( ! file_exists($this->pidFile)) {
118127
throw new LogicException('Can\'t reload server. Pid file not found.');
119128
}
120129

@@ -136,6 +145,7 @@ public function stop(): void
136145

137146
/**
138147
* @param string $key
148+
*
139149
* @return bool|string
140150
*/
141151
private function getHostWithoutTrailingSlash(string $key)
@@ -155,10 +165,10 @@ private function getConnectionProcessingMethod(): string
155165
{
156166
switch (PHP_OS_FAMILY) {
157167
case 'Linux':
158-
return 'epoll';
168+
return 'epoll';
159169
case 'Darwin':
160170
case 'BSD':
161-
return 'kqueue';
171+
return 'kqueue';
162172
default:
163173
return 'poll';
164174
}
@@ -169,39 +179,43 @@ private function getConnectionProcessingMethod(): string
169179
*/
170180
private function checkIfPrerenderUrlIsAvailable(): void
171181
{
172-
if (!$this->configuration->get('server.prerender.enabled', false)) {
182+
if ( ! $this->configuration->get('server.prerender.enabled', false)) {
173183
$this->logger->info('Prerender is not enabled, skip check.');
184+
174185
return;
175186
}
176187

177-
$url = $this->configuration->get('server.prerender.url', false);
188+
$url = $this->configuration->get('server.prerender.cdn_url', false);
178189

179-
if (!$url) {
180-
throw new InvalidArgumentException('Prerender URL not set. Check server.prerender.url config key.');
190+
if ( ! $url) {
191+
throw new InvalidArgumentException('Prerender CDN URL not set. Check server.prerender.cdn_url config key.');
181192
}
182193

183194
$url = (string) parse_url($url, PHP_URL_HOST);
184195

185196
if (strlen($url) < 1) {
186-
throw new InvalidArgumentException('Prerender URL is invalid. Check server.prerender.url config key.');
197+
throw new InvalidArgumentException(
198+
'Prerender CDN URL is invalid. Check server.prerender.cdn_url config key.'
199+
);
187200
}
188201

189-
$this->logger->info('Ping prerender url...');
202+
$this->logger->info('Ping prerender cdn url...');
190203

191-
$ping = new Ping($url);
204+
$ping = new Ping($url);
192205
$latency = $ping->ping('fsockopen');
193206

194207
if ($latency !== false) {
195-
$this->logger->info('Prerender url is available.');
208+
$this->logger->info('Prerender cdn url is available.');
209+
196210
return;
197211
}
198212

199-
$this->logger->warning('Prerender url could not be reached: ' . $url);
213+
$this->logger->warning('Prerender cdn url could not be reached: ' . $url);
200214
}
201215

202216
private function checkIfPrerenderHostIsNotEmpty(): void
203217
{
204-
if (!$this->configuration->get('server.prerender.enabled', false)) {
218+
if ( ! $this->configuration->get('server.prerender.enabled', false)) {
205219
return;
206220
}
207221

@@ -215,8 +229,10 @@ private function checkIfPrerenderHostIsNotEmpty(): void
215229

216230
$valid = (bool) parse_url($host);
217231

218-
if (!$valid) {
219-
throw new InvalidArgumentException('Prerender host is invalid (can\'t parse a url). Check server.prerender.host config key.');
232+
if ( ! $valid) {
233+
throw new InvalidArgumentException(
234+
'Prerender host is invalid (can\'t parse a url). Check server.prerender.host config key.'
235+
);
220236
}
221237
}
222238

src/Handler/templates/nginx_default.conf.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
proxy_buffers 64 32k;
274274
proxy_busy_buffers_size 32k;
275275
proxy_cache STATIC;
276-
proxy_cache_valid 200 404 24h;
276+
proxy_cache_valid 200 <?=$prerenderCacheTTL?>;
277277
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
278278

279279
set $prerender 0;
@@ -295,9 +295,14 @@
295295
<?php endif;?>
296296

297297
if ($prerender = 1) {
298+
# need to redefine $request_uri if it will be "/" but $request_uri is blocked for changes
299+
set $req_uri $uri;
300+
if ($req_uri = /) {
301+
set $req_uri "/index";
302+
}
298303
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
299-
rewrite .* /<?=$prerenderHost?>$request_uri?$query_string break;
300-
proxy_pass "<?=$prerenderUrl?>";
304+
rewrite .* <?=$CDNPath?>$req_uri.html?$query_string break;
305+
proxy_pass "<?=$CDNUrl?>";
301306
break;
302307
}
303308

tests/configuration/defaults/___server.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ defaults:
1818
config: /tmp/generated_nginx.conf
1919
prerender:
2020
enabled: false
21-
url: null
21+
cache_ttl: 24h # by default cache lives 24 hours
22+
cdn_url: null
23+
cdn_folder: / # CDN folder relative to root path (/)
24+
headers: []
25+
resolver: false
2226
log_info: "%%cSTAGE=%s SHA1=%s; %%cSecurity bugs: [email protected], Job/partnership: [email protected]"
2327
logger:
2428
enabled: true

0 commit comments

Comments
 (0)