mirror of
https://github.com/trambarhq/relaks-wordpress-example.git
synced 2025-09-08 23:20:40 +02:00
Fixed Nginx cache config (issue #41).
This commit is contained in:
@@ -21,6 +21,7 @@ let dnsCache = Bluebird.promisifyAll(DNSCache({
|
|||||||
|
|
||||||
const SERVER_PORT = 80;
|
const SERVER_PORT = 80;
|
||||||
const WORDPRESS_HOST = process.env.WORDPRESS_HOST;
|
const WORDPRESS_HOST = process.env.WORDPRESS_HOST;
|
||||||
|
const CACHE_CONTROL = 'public,s-maxage=604800';
|
||||||
|
|
||||||
// start up Express
|
// start up Express
|
||||||
let app = Express();
|
let app = Express();
|
||||||
@@ -44,6 +45,7 @@ async function handleTimestampRequest(req, res, next) {
|
|||||||
try {
|
try {
|
||||||
let now = new Date;
|
let now = new Date;
|
||||||
let ts = now.toISOString();
|
let ts = now.toISOString();
|
||||||
|
res.set({ 'Cache-Control': CACHE_CONTROL });
|
||||||
res.type('text').send(ts);
|
res.type('text').send(ts);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err);
|
next(err);
|
||||||
@@ -56,15 +58,15 @@ async function handleCacheStatusRequest(req, res, next) {
|
|||||||
res.type('html');
|
res.type('html');
|
||||||
res.write(`<html><head><title>Nginx Cache</title></head><body>`);
|
res.write(`<html><head><title>Nginx Cache</title></head><body>`);
|
||||||
res.write(`<table border="1" cellpadding="2">`);
|
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>`);
|
res.write(`<tbody>`);
|
||||||
let entries = await NginxCache.read();
|
let entries = await NginxCache.read();
|
||||||
let total = 0;
|
let total = 0;
|
||||||
for (let entry of _.orderBy(entries, 'mtime', 'desc')) {
|
for (let entry of _.orderBy(entries, 'mtime', 'desc')) {
|
||||||
let { url, mtime, size } = entry;
|
let { url, md5, mtime, size } = entry;
|
||||||
let date = Moment(mtime).format('LLLL');
|
let date = Moment(mtime).format('LLL');
|
||||||
let sizeKB = _.round(size / 1024, 2);
|
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;
|
total += size;
|
||||||
}
|
}
|
||||||
let totalMB = _.round(total / 1024 / 1024, 2);
|
let totalMB = _.round(total / 1024 / 1024, 2);
|
||||||
@@ -87,6 +89,7 @@ async function handleJSONRequest(req, res, next) {
|
|||||||
if (json.total) {
|
if (json.total) {
|
||||||
res.set({ 'X-WP-Total': json.total });
|
res.set({ 'X-WP-Total': json.total });
|
||||||
}
|
}
|
||||||
|
res.set({ 'Cache-Control': CACHE_CONTROL });
|
||||||
res.send(json.text);
|
res.send(json.text);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err);
|
next(err);
|
||||||
@@ -110,6 +113,8 @@ async function handlePageRequest(req, res, next) {
|
|||||||
// not caching content generated for SEO
|
// not caching content generated for SEO
|
||||||
res.set({ 'X-Accel-Expires': 0 });
|
res.set({ 'X-Accel-Expires': 0 });
|
||||||
} else {
|
} else {
|
||||||
|
res.set({ 'Cache-Control': CACHE_CONTROL });
|
||||||
|
|
||||||
// remember the URLs used by the page
|
// remember the URLs used by the page
|
||||||
pageDependencies[path] = page.sourceURLs.map(addTrailingSlash);
|
pageDependencies[path] = page.sourceURLs.map(addTrailingSlash);
|
||||||
}
|
}
|
||||||
|
@@ -12,11 +12,13 @@ async function fetch(path) {
|
|||||||
object = JSON.parse(resText);
|
object = JSON.parse(resText);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// remove any error msg that got dumped into the output stream
|
// remove any error msg that got dumped into the output stream
|
||||||
resText = resText.replace(/^[^\{\[]+/, '');
|
if (res.status === 200) {
|
||||||
object = JSON.parse(resText);
|
resText = resText.replace(/^[^\{\[]+/, '');
|
||||||
|
object = JSON.parse(resText);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (res.status >= 400) {
|
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);
|
let err = new Error(msg);
|
||||||
err.status = res.status;
|
err.status = res.status;
|
||||||
throw err;
|
throw err;
|
||||||
|
@@ -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;
|
proxy_temp_path /var/cache/nginx/tmp;
|
||||||
|
|
||||||
server {
|
server {
|
||||||
@@ -8,14 +8,12 @@ server {
|
|||||||
location / {
|
location / {
|
||||||
proxy_pass http://node;
|
proxy_pass http://node;
|
||||||
proxy_set_header Host $http_host;
|
proxy_set_header Host $http_host;
|
||||||
proxy_buffering on;
|
|
||||||
proxy_cache data;
|
proxy_cache data;
|
||||||
proxy_cache_key $uri$is_args$args;
|
proxy_cache_key $uri$is_args$args;
|
||||||
proxy_cache_min_uses 1;
|
proxy_cache_min_uses 1;
|
||||||
proxy_cache_valid 200 301 302 7d;
|
proxy_cache_valid 400 404 1m;
|
||||||
proxy_cache_valid 404 1m;
|
|
||||||
proxy_hide_header Cache-Control;
|
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-Allow-Origin *;
|
||||||
add_header Access-Control-Expose-Headers X-WP-Total;
|
add_header Access-Control-Expose-Headers X-WP-Total;
|
||||||
|
@@ -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;
|
proxy_temp_path /var/cache/nginx/tmp;
|
||||||
|
|
||||||
server {
|
server {
|
||||||
@@ -19,14 +19,12 @@ server {
|
|||||||
location / {
|
location / {
|
||||||
proxy_pass http://node;
|
proxy_pass http://node;
|
||||||
proxy_set_header Host $http_host;
|
proxy_set_header Host $http_host;
|
||||||
proxy_buffering on;
|
|
||||||
proxy_cache data;
|
proxy_cache data;
|
||||||
proxy_cache_key $uri$is_args$args;
|
proxy_cache_key $uri$is_args$args;
|
||||||
proxy_cache_min_uses 1;
|
proxy_cache_min_uses 1;
|
||||||
proxy_cache_valid 200 301 302 7d;
|
proxy_cache_valid 400 404 1m;
|
||||||
proxy_cache_valid 404 1m;
|
|
||||||
proxy_hide_header Cache-Control;
|
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-Allow-Origin *;
|
||||||
add_header Access-Control-Expose-Headers X-WP-Total;
|
add_header Access-Control-Expose-Headers X-WP-Total;
|
||||||
|
Reference in New Issue
Block a user