1
0
mirror of https://github.com/trambarhq/relaks-wordpress-example.git synced 2025-09-03 05:02:34 +02:00

Fixed Nginx cache config (issue #41).

This commit is contained in:
Chung Leong
2019-01-31 02:39:30 +01:00
parent 6024a5156f
commit 0671304865
4 changed files with 20 additions and 17 deletions

View File

@@ -21,6 +21,7 @@ let dnsCache = Bluebird.promisifyAll(DNSCache({
const SERVER_PORT = 80;
const WORDPRESS_HOST = process.env.WORDPRESS_HOST;
const CACHE_CONTROL = 'public,s-maxage=604800';
// start up Express
let app = Express();
@@ -44,6 +45,7 @@ async function handleTimestampRequest(req, res, next) {
try {
let now = new Date;
let ts = now.toISOString();
res.set({ 'Cache-Control': CACHE_CONTROL });
res.type('text').send(ts);
} catch (err) {
next(err);
@@ -56,15 +58,15 @@ async function handleCacheStatusRequest(req, res, next) {
res.type('html');
res.write(`<html><head><title>Nginx Cache</title></head><body>`);
res.write(`<table border="1" cellpadding="2">`);
res.write(`<thead><th>URL</th><th>Modified time</th><th>Size</th></thead>`);
res.write(`<thead><th>URL</th><th>MD5</th><th>Modified time</th><th>Size</th></thead>`);
res.write(`<tbody>`);
let entries = await NginxCache.read();
let total = 0;
for (let entry of _.orderBy(entries, 'mtime', 'desc')) {
let { url, mtime, size } = entry;
let date = Moment(mtime).format('LLLL');
let { url, md5, mtime, size } = entry;
let date = Moment(mtime).format('LLL');
let sizeKB = _.round(size / 1024, 2);
res.write(`<tr><td>${url}</td><td>${date}</td><td>${sizeKB}KB</td></tr>`)
res.write(`<tr><td>${url}</td><td>${md5}</td><td>${date}</td><td>${sizeKB}KB</td></tr>`)
total += size;
}
let totalMB = _.round(total / 1024 / 1024, 2);
@@ -87,6 +89,7 @@ async function handleJSONRequest(req, res, next) {
if (json.total) {
res.set({ 'X-WP-Total': json.total });
}
res.set({ 'Cache-Control': CACHE_CONTROL });
res.send(json.text);
} catch (err) {
next(err);
@@ -110,6 +113,8 @@ async function handlePageRequest(req, res, next) {
// not caching content generated for SEO
res.set({ 'X-Accel-Expires': 0 });
} else {
res.set({ 'Cache-Control': CACHE_CONTROL });
// remember the URLs used by the page
pageDependencies[path] = page.sourceURLs.map(addTrailingSlash);
}

View File

@@ -12,11 +12,13 @@ async function fetch(path) {
object = JSON.parse(resText);
} catch (err) {
// remove any error msg that got dumped into the output stream
resText = resText.replace(/^[^\{\[]+/, '');
object = JSON.parse(resText);
if (res.status === 200) {
resText = resText.replace(/^[^\{\[]+/, '');
object = JSON.parse(resText);
}
}
if (res.status >= 400) {
let msg = (object && object.message) ? object.message : 'Unknown error';
let msg = (object && object.message) ? object.message : resText;
let err = new Error(msg);
err.status = res.status;
throw err;

View File

@@ -1,4 +1,4 @@
proxy_cache_path /var/cache/nginx/data keys_zone=data:10m max_size=1g;
proxy_cache_path /var/cache/nginx/data keys_zone=data:10m max_size=1g inactive=7d;
proxy_temp_path /var/cache/nginx/tmp;
server {
@@ -8,14 +8,12 @@ server {
location / {
proxy_pass http://node;
proxy_set_header Host $http_host;
proxy_buffering on;
proxy_cache data;
proxy_cache_key $uri$is_args$args;
proxy_cache_min_uses 1;
proxy_cache_valid 200 301 302 7d;
proxy_cache_valid 404 1m;
proxy_cache_valid 400 404 1m;
proxy_hide_header Cache-Control;
proxy_ignore_headers Cache-Control Expires Set-Cookie;
proxy_ignore_headers Vary;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Expose-Headers X-WP-Total;

View File

@@ -1,4 +1,4 @@
proxy_cache_path /var/cache/nginx/data keys_zone=data:10m max_size=1g;
proxy_cache_path /var/cache/nginx/data keys_zone=data:10m max_size=1g inactive=7d;
proxy_temp_path /var/cache/nginx/tmp;
server {
@@ -19,14 +19,12 @@ server {
location / {
proxy_pass http://node;
proxy_set_header Host $http_host;
proxy_buffering on;
proxy_cache data;
proxy_cache_key $uri$is_args$args;
proxy_cache_min_uses 1;
proxy_cache_valid 200 301 302 7d;
proxy_cache_valid 404 1m;
proxy_cache_valid 400 404 1m;
proxy_hide_header Cache-Control;
proxy_ignore_headers Cache-Control Expires Set-Cookie;
proxy_ignore_headers Vary;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Expose-Headers X-WP-Total;