admin管理员组

文章数量:1391995

I am trying to configure Nginx () as a proxy for an NPM repository (). The setup in Nginx is fairly simple:

server {
    listen 80;
    server_name my.proxy;

    access_log /var/log/nginx/my.proxy.access.log;
    error_log /var/log/nginx/my.proxy.error.log info;

    location / {
        proxy_pass $request_uri;
        proxy_set_header Host my.npm.repo;

        # Additional headers
        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 $scheme;

        # Proxy settings
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
    }
}

I have also configured .npmrc on my local machine as follows:

registry=/npm/npm-all/

The problem arises when I run npm install in my project folder. NPM successfully fetches metadata for a dependency, for example:

GET /npm/npm-all/postcss

The response contains a JSON file listing download URLs for the package tarballs. However, these URLs are direct links from the NPM repository, such as:

/npm/npm-all/postcss-1.2.3.tgz

Since the NPM repository is unaware of the proxy, it returns URLs pointing to itself instead of my proxy. Unfortunately, I cannot modify the repository's configuration to change these URLs.

To address this, I attempted using sub_filter in the Nginx configuration:

sub_filter_types *;
sub_filter 'my.npm.repo' 'my.proxy';
sub_filter_once off;

This successfully rewrites the URLs in the JSON response, so I now see:

/npm/npm-all/postcss-1.2.3.tgz

However, despite this change, NPM still tries to download the tarball from the original repository URL (/npm/npm-all/postcss-1.2.3.tgz) instead of my proxy.

I have cleared the NPM cache, but the issue persists. I am unsure where NPM is getting the original URL from.

Has anyone encountered this issue before? Any suggestions would be greatly appreciated.

Thanks!

本文标签: Issue with Nginx Proxy for NPM RepositoryStack Overflow