Skip to content

Commit e9ddc2f

Browse files
committed
optimize caching of upstream responses
Especially tarballs, which were not being cached correctly before.
1 parent 54c14a3 commit e9ddc2f

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
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: 18 additions & 5 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 '';
@@ -38,16 +44,23 @@ http {
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 "";

0 commit comments

Comments
 (0)