Skip to content

Commit 3baf57c

Browse files
committed
nginx: use lua_shared_dict for config
1 parent b419502 commit 3baf57c

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

nginx.conf

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,24 @@ http {
2525
'"$request" $status $body_bytes_sent '
2626
'"$http_referer" "$http_user_agent" "$upstream_cache_status"';
2727
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+
2841
server {
2942
listen 4873;
3043
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-
}
3944
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')
4346
}
4447
location ~ ^/.*\.tgz$ {
4548
root /tmp/npm/store;
@@ -64,7 +67,8 @@ http {
6467
}
6568
# replace all occurances of, eg. https://registry.npmjs.org with http://127.0.0.1:4873
6669
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')
6872
-- need to construct URL because we may be proxying http<->https
6973
local base = ngx.var.scheme .. '://' .. ngx.var.http_host
7074
-- ngx.log(ngx.ERR, "Modifying JSON of " .. ngx.var.uri .. " to replace '" .. upstream .. "' with '" .. base .. "'")

0 commit comments

Comments
 (0)