@@ -25,21 +25,24 @@ http {
25
25
'"$request " $status $body_bytes_sent '
26
26
'"$http_referer " "$http_user_agent " "$upstream_cache_status "' ;
27
27
access_log logs/access.log upstreamlog;
28
+
29
+ lua_shared_dict npmConfig 32k ;
30
+ init_by_lua_block {
31
+ local npmConfig = ngx.shared.npmConfig
32
+ local registry = os.getenv("npm_config_registry" ):gsub("/+$" , "" )
33
+ local pattern = registry:gsub("%." , "%%." ):gsub("%-" , "%%-" )
34
+ -- escape . and - which have special meaning in Lua patterns
35
+ npmConfig:set ('npm_config_registry' , registry)
36
+ npmConfig:set('npm_upstream_pattern' , pattern)
37
+ -- ngx.log(ngx.ERR, 'using upstream base: ' , registry)
38
+ -- ngx.log(ngx.ERR, 'using upstream pattern: ' , pattern)
39
+ }
40
+
28
41
server {
29
42
listen 4873 ;
30
43
set $npm_config_registry '' ;
31
- set $upstream_base '' ;
32
- set_by_lua_block $upstream_base {
33
- local upstream = os.getenv("npm_config_registry" )
34
- -- escape . and - which have special meaning in Lua patterns
35
- upstream = upstream:gsub("%." , "%%." ):gsub("%-" , "%%-" ):gsub("/+$" , "" )
36
- -- ngx.log(ngx.ERR, 'using upstream base:', upstream)
37
- return upstream
38
- }
39
44
set_by_lua_block $npm_config_registry {
40
- local upstream = os.getenv("npm_config_registry"):gsub("/+$", "")
41
- -- ngx.log(ngx.ERR, ' using upstream server:', upstream)
42
- return upstream
45
+ return ngx.shared.npmConfig:get('npm_config_registry' )
43
46
}
44
47
location ~ ^/.*\.tgz$ {
45
48
root /tmp/npm/store;
64
67
}
65
68
# replace all occurances of, eg. https://registry.npmjs.org with http://127.0.0.1:4873
66
69
body_filter_by_lua_block {
67
- local upstream = ngx.var.upstream_base
70
+ local npmConfig = ngx.shared.npmConfig
71
+ local upstream = npmConfig:get('npm_upstream_pattern' )
68
72
-- need to construct URL because we may be proxying http<->https
69
73
local base = ngx.var.scheme .. '://' .. ngx.var.http_host
70
74
-- ngx.log(ngx.ERR, "Modifying JSON of " .. ngx.var.uri .. " to replace '" .. upstream .. "' with '" .. base .. "' ")
0 commit comments