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
版权声明:本文标题:Nginx Caching for NPM Artifacts Serving Incorrect Versions from Cache - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304103a1932116.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论