Skip to content

Commit 3e94dc5

Browse files
authored
Merge pull request #3 from strongloop/nginx-optimize-cache
nginx: optimize caching of packages
2 parents 3cbe44a + e9ddc2f commit 3e94dc5

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

entrypoint.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ export npm_config_registry=${npm_config_registry:-https://registry.npmjs.org}
1515
# necessary because nginx requires a resolver when upstreams are dynamic
1616
dnsmasq --listen-address=127.0.0.1 --user=root
1717

18+
# in case it hasn't been created as a tmpfs already
19+
mkdir -p /tmp/npm
20+
1821
exec /usr/local/openresty/bin/openresty -c /nginx.conf $*

nginx.conf

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ http {
1818
default_type application/octet-stream;
1919
sendfile on;
2020
keepalive_timeout 65;
21-
proxy_cache_path /tmp/npm-cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
21+
proxy_cache_path /tmp/npm/cache levels=1:2 keys_zone=NPM:10m inactive=90d max_size=5g;
22+
proxy_temp_path /tmp/npm/temp;
23+
proxy_cache_lock on;
24+
log_format upstreamlog '$remote_addr - $remote_user [$time_local] '
25+
'"$request" $status $body_bytes_sent '
26+
'"$http_referer" "$http_user_agent" "$upstream_cache_status"';
27+
access_log logs/access.log upstreamlog;
2228
server {
2329
listen 4873;
2430
set $npm_config_registry '';
@@ -27,27 +33,34 @@ http {
2733
local upstream = os.getenv("npm_config_registry")
2834
-- escape . and - which have special meaning in Lua patterns
2935
upstream = upstream:gsub("%.", "%%."):gsub("%-", "%%-"):gsub("/+$", "")
30-
ngx.log(ngx.ERR, 'using upstream base:', upstream)
36+
-- ngx.log(ngx.ERR, 'using upstream base:', upstream)
3137
return upstream
3238
}
3339
set_by_lua_block $npm_config_registry {
3440
local upstream = os.getenv("npm_config_registry"):gsub("/+$", "")
35-
ngx.log(ngx.ERR, 'using upstream server:', upstream)
41+
-- ngx.log(ngx.ERR, 'using upstream server:', upstream)
3642
return upstream
3743
}
3844
location ~ ^/.*\.tgz$ {
3945
resolver 127.0.0.1 ipv6=off;
4046
proxy_pass $npm_config_registry;
41-
proxy_cache STATIC;
42-
proxy_cache_valid 200 1d;
47+
proxy_cache NPM;
48+
proxy_ignore_headers X-Accel-Redirect X-Accel-Expires X-Accel-Limit-Rate X-Accel-Buffering X-Accel-Charset Expires Cache-Control Set-Cookie Vary;
49+
# npm package tarballs are NEVER supposed to change
50+
proxy_cache_revalidate off;
51+
proxy_cache_valid 200 90d;
4352
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
4453
}
4554
location / {
4655
resolver 127.0.0.1 ipv6=off;
4756
proxy_pass $npm_config_registry;
4857
proxy_buffers 32 1m;
49-
proxy_cache STATIC;
50-
proxy_cache_valid 200 1d;
58+
proxy_cache NPM;
59+
# need nginx-1.11.10 for this, which isn't in openresty yet:
60+
# proxy_cache_background_update on;
61+
proxy_cache_revalidate on;
62+
# let our metadata be 20 minutes old, at most
63+
proxy_cache_valid 200 20m;
5164
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
5265
# need to disable encoding in order to be able to filter the response body
5366
proxy_set_header Accept-Encoding "";
@@ -60,7 +73,7 @@ http {
6073
local upstream = ngx.var.upstream_base
6174
-- need to construct URL because we may be proxying http<->https
6275
local base = ngx.var.scheme .. '://' .. ngx.var.http_host
63-
ngx.log(ngx.ERR, "Modifying JSON of " .. ngx.var.uri .. " to replace '" .. upstream .. "' with '" .. base .. "'")
76+
-- ngx.log(ngx.ERR, "Modifying JSON of " .. ngx.var.uri .. " to replace '" .. upstream .. "' with '" .. base .. "'")
6477
ngx.arg[1] = string.gsub(ngx.arg[1], upstream, base)
6578
}
6679
}

0 commit comments

Comments
 (0)