Cashing packages for npm registry with NGINX

Alex Shemshurenko alexshe at
Sun Apr 2 11:33:07 UTC 2017

My project is javascript app. I have lots of dependencies. I/O for npm
registry takes major portion of CI execution. So my idea is to setup NGINX
in front of npm registry and cache tgz files downloads. Im running Ubuntu
14.04. NGINX version is 1.4.6

This is my nginx configuration script

user www-data;
worker_processes 4;
pid /run/;

events {
    worker_connections 768;
    # multi_accept on;}

http {

    # Basic Settings

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logging Settings

    # access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # Gzip Settings

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    #   include /etc/nginx/sites-enabled/*;
    # HTTP 1.1 support
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $proxy_connection;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
    proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
    proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;

    # If we receive X-Forwarded-Proto, pass it through; otherwise,
pass along the
    # scheme used to connect to this server
    map $http_x_forwarded_proto $proxy_x_forwarded_proto {
        default $http_x_forwarded_proto;
        ''      $scheme;

    # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
    # server port the client connected to
    map $http_x_forwarded_port $proxy_x_forwarded_port {
        default $http_x_forwarded_port;
        ''      $server_port;

    # If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
    # Connection header that may have been passed to this server
    map $http_upgrade $proxy_connection {
        default upgrade;
        '' close;

    # Set appropriate X-Forwarded-Ssl header
    map $scheme $proxy_x_forwarded_ssl {
        default off;
        https on;

    server {
        listen 80 default_server;

       location / {
            access_log /var/log/nginx/root.log;
            root       /var/tmp/nginx/npm;
            try_files  $request_uri @fetch;

        location @fetch {
                proxy_pass              http://nmregistry:4873$request_uri;
                proxy_store             /var/tmp/nginx/npm$request_uri;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_store_access      user:rw group:rw all:r;

It works, i can install packages but they are not cached on NGINX machine.
Cant see any tgz files in /var/tmp/nginx/npm

What am i doing wrong here?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list