admin管理员组

文章数量:1122832

I am trying to configure Nginx as a caching proxy for serving npm artifacts, and I have the following configuration:

Nginx Cache Path and Key Configuration:

proxy_cache_path /var/www/my-data/my-artifact-cache levels=1:2 keys_zone=my_cache:128m inactive=10d max_size=10g;

Nginx Location Block:

location /repository/ {
    proxy_set_header Host $host:$server_port;
    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_hide_header X-Robots-Tag;
    proxy_hide_header Link;
    proxy_pass http://@@HOST_IP@@:9080;
    proxy_cache my_cache;
    proxy_cache_key "$scheme$host$request_uri$is_args$args";
    proxy_cache_valid 200 10d;
    proxy_cache_valid 400 401 404 500 502 503 504 0s;
    proxy_cache_use_stale timeout updating;
}

Problem 1: Cached Responses for Nonexistent Versions

When I request an npm artifact (e.g., npm i [email protected]) and the upstream server does not have this version, Nginx still caches the 200 response. When the same version is requested again, it serves the response from the cache without rechecking the upstream, even though the artifact does not exist.

Problem 2: Cache Hits for Different Versions

When I install one version of an artifact (e.g., [email protected]), and later request a different version (e.g., [email protected]), Nginx logs show a cache hit with a 200 status code, but it serves the cached response of the previous version (5.0.0). It does not forward the request to the upstream to fetch the correct version. Expected Behavior

1. Each request should be uniquely identified in the cache based on the full URL, including version information.
2. If the requested artifact version is not available in the cache, Nginx should forward the request to the upstream server to fetch the correct version.

Current Configuration

The proxy_cache_key is set to:

proxy_cache_key "$scheme$host$request_uri$is_args$args";

This does not seem to be working as expected.

How can I configure Nginx to ensure that:

Each request (including version-specific details) is uniquely cached?
Nginx does not cache responses for non-existent versions?

FYI, I tried with this to be cache key should be unique as in case of npm requesting:

proxy_cache_key "$scheme$host$request_uri$is_args$args";
proxy_cache_key "$scheme$host$request_uri$is_args$args$http_user_agent";
proxy_cache_key "$scheme$host$request_uri$is_args$args$version";
proxy_cache_key "$scheme$host$request_uri$is_args$args$http_user_agent";
proxy_cache_key "$scheme$host$request_uri$is_args$args$request_method$http_user_agent";

Not worked with above...

also: The upstream I am using Sonatype nexus repo

EDIT: on my understanding, it is due to as npm first fetching packages and then installing tarball when it is npm install

in this case, nginx is caching this metadata and fetching from cache, which doesn't having the latest versions, so faiing,and by not checking in upstream,

how can we solve this?if this is the actual issue ...

Any help or suggestions would be greatly appreciated!

本文标签: Nginx Caching for NPM Artifacts Serving Incorrect Versions from CacheStack Overflow