Skip to content

Instantly share code, notes, and snippets.

@knight42
Created March 5, 2016 01:26
Show Gist options
  • Save knight42/46189f4538e78c7cb987 to your computer and use it in GitHub Desktop.
Save knight42/46189f4538e78c7cb987 to your computer and use it in GitHub Desktop.
npm registry caching proxy using nginx
proxy_cache_path /mnt/array1/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
proxy_temp_path /mnt/array1/nginx/temp;
keepalive_timeout 2;
tcp_nodelay on;
sendfile on;
limit_conn_zone $binary_remote_addr zone=addr:20m;
limit_req_zone $binary_remote_addr zone=req_per_ip:20m rate=80r/s;
limit_req zone=req_per_ip burst=100;
server {
listen 1.2.3.4:80;
listen 1.2.3.4:443;
server_name registry.example.com;
resolver 8.8.8.8;
access_log /var/log/nginx/npm/access.log;
error_log /var/log/nginx/npm/error.log;
proxy_buffering on;
# npm may fail to parse json if stricter limitation is applied
limit_conn addr 50;
location / {
sub_filter registry.npmjs.org registry.example.com;
sub_filter_once off;
sub_filter_types application/json;
proxy_pass $scheme://registry.npmjs.org;
proxy_bind 2.3.4.5; # cross the GFW
proxy_set_header Host "registry.npmjs.org";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache STATIC;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment