Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nginx caching #68

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions app/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default class UnboxApp {

this.app = new Koa()

this.startupDate = Date.now()

// Add the layers

// Catch errors
Expand Down Expand Up @@ -89,6 +91,14 @@ export default class UnboxApp {

this.app.use(koaBody())

this.app.use(async (ctx, next) => {
try {
await next()
} finally {
console.log(`${ctx.method} ${ctx.url} ${ctx.status} "${ctx.headers['if-modified-since']}" "${ctx.headers['cache-control']}"`)
}
})

// And the main handler
this.app.use(this.handler.bind(this))
}
Expand All @@ -105,7 +115,18 @@ export default class UnboxApp {
// Solve CORS issues
ctx.set('Access-Control-Allow-Origin', '*')

ctx.set('Cache-Control', `max-age=0`)
ctx.set('Cache-Control', `max-age=1`)

const ctxFresh = () => {
if (this.options.nginx?.cache?.support_bypass) {
// ctx fresh uses https://github.com/jshttp/fresh/blob/v0.5.2/index.js#L43-L49
// which always honors `Cache-Control: no-cache` in the request header
return ctx.fresh
} else {
const modifiedSince = ctx.request.headers['if-modified-since']
return !!modifiedSince && modifiedSince === ctx.response.headers['last-modified']
}
}

// Front page
if (request_path === '/') {
Expand Down Expand Up @@ -205,8 +226,12 @@ export default class UnboxApp {

// Send and check the Last-Modified/If-Modified-Since headers
ctx.status = 200
ctx.lastModified = new Date(details.date)
if (ctx.fresh) {
// If we fix a bug on the index pages, then the index page's `Last-Modified` date
// needs to change, or else nginx will continue to serve up the old buggy version
// (because the zip itself hasn't changed).
// TODO: Use a "deployDate" instead, so restarting Node doesn't flush the index cache?
ctx.lastModified = new Date(Math.max(details.date, this.startupDate))
if (ctxFresh()) {
ctx.status = 304
return
}
Expand Down Expand Up @@ -367,7 +392,7 @@ export default class UnboxApp {
// Send and check the Last-Modified/If-Modified-Since headers
ctx.status = 200
ctx.lastModified = new Date(details.date)
if (ctx.fresh) {
if (ctxFresh()) {
ctx.status = 304
return
}
Expand Down
37 changes: 19 additions & 18 deletions nginx/nginx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ GZIP="gzip on;
LISTEN="listen 80;
listen [::]:80;"

if [ "$SUPPORT_BYPASS" = "true" ]; then
BYPASS="proxy_cache_bypass \$http_pragma;"
fi

# Common proxy settings
PROXY="proxy_pass http://app:8080;
proxy_set_header Host \$host;
proxy_cache cache;
proxy_cache_revalidate on;
$BYPASS
add_header X-Cache \$upstream_cache_status;"

# Erase the conf file
> $CONF_FILE

Expand Down Expand Up @@ -53,49 +65,38 @@ EOF
fi
fi

CACHE_DIR="$DATA_DIR/nginx-cache"
mkdir -p $CACHE_DIR

# Top domain server
cat >> $CONF_FILE <<EOF
proxy_cache_path $CACHE_DIR keys_zone=cache:${KEYS_SIZE}m levels=1:2 max_size=${MAX_SIZE}m;

server {
$LISTEN
$SERVER_NAME
$SSL
$GZIP

location / {
proxy_pass http://app:8080;
proxy_set_header Host \$host;
$PROXY
}
}
EOF

if [ -n "$DOMAIN" ] && [ "$SUBDOMAINS" = "true" ]; then

CACHE_DIR="$DATA_DIR/nginx-cache"
mkdir -p $CACHE_DIR

if [ "$SUPPORT_BYPASS" = "true" ]; then
BYPASS="proxy_cache_bypass \$http_pragma;"
fi

# Subdomain server
cat >> $CONF_FILE <<EOF

proxy_cache_path $CACHE_DIR keys_zone=cache:${KEYS_SIZE}m levels=1:2 max_size=${MAX_SIZE}m;

server {
$LISTEN
server_name *.${DOMAIN};
$SSL
$GZIP
proxy_cache cache;
proxy_cache_valid 301 365d;
proxy_cache_valid any 1d;
$BYPASS

location / {
proxy_pass http://app:8080;
proxy_set_header Host \$host;
add_header X-Cache \$upstream_cache_status;
$PROXY
}
}
EOF
Expand Down
Loading