Skip to content

Commit 3cbe44a

Browse files
authored
Merge pull request #2 from strongloop/nginx-wip
initial nginx/openresty based proxy
2 parents 7bc7aa5 + 21e13f6 commit 3cbe44a

File tree

4 files changed

+91
-54
lines changed

4 files changed

+91
-54
lines changed

Dockerfile

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
1-
FROM node:4-slim
1+
FROM openresty/openresty:alpine
22
MAINTAINER Ryan Graham <[email protected]>
33

4-
# Run as unprivileged, even under docker
5-
RUN adduser \
6-
--group \
7-
--system \
8-
--home /var/lib/sinopia \
9-
--disabled-password \
10-
--disabled-login \
11-
sinopia
4+
RUN apk --no-cache add dnsmasq
125

13-
USER sinopia
14-
ENV HOME=/var/lib/sinopia
15-
16-
WORKDIR /var/lib/sinopia
17-
18-
RUN npm install --loglevel=warn --no-spin sinopia && \
19-
npm --no-spin --loglevel=warn cache clean && \
20-
rm -rf ~/.node-gyp
21-
22-
COPY sinopia.sh /var/lib/sinopia/run.sh
6+
ADD entrypoint.sh nginx.conf /
237

8+
# Not port 80 because ephemeral-npm stnadardized on couchdb's port already
249
EXPOSE 4873
2510

26-
ENTRYPOINT ["/var/lib/sinopia/run.sh"]
11+
ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
# ephemeral-npm env vars supported:
4+
# [x] npm_config_registry
5+
# [ ] MAX_BODY_SIZE
6+
# [ ] MAX_USERS
7+
# [ ] MAXAGE
8+
# [ ] TIMEOUT
9+
# [ ] NPM_SECRET
10+
# [ ] NPM_USER
11+
# [ ] NPM_PASSWORD
12+
13+
export npm_config_registry=${npm_config_registry:-https://registry.npmjs.org}
14+
15+
# necessary because nginx requires a resolver when upstreams are dynamic
16+
dnsmasq --listen-address=127.0.0.1 --user=root
17+
18+
exec /usr/local/openresty/bin/openresty -c /nginx.conf $*

nginx.conf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
user nobody;
2+
worker_processes 1;
3+
daemon off;
4+
5+
env npm_config_registry;
6+
7+
events {
8+
worker_connections 1024;
9+
}
10+
11+
12+
http {
13+
# We literally only care about 2 types of files/payloads.
14+
types {
15+
application/json json;
16+
application/gzip tgz;
17+
}
18+
default_type application/octet-stream;
19+
sendfile on;
20+
keepalive_timeout 65;
21+
proxy_cache_path /tmp/npm-cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
22+
server {
23+
listen 4873;
24+
set $npm_config_registry '';
25+
set $upstream_base '';
26+
set_by_lua_block $upstream_base {
27+
local upstream = os.getenv("npm_config_registry")
28+
-- escape . and - which have special meaning in Lua patterns
29+
upstream = upstream:gsub("%.", "%%."):gsub("%-", "%%-"):gsub("/+$", "")
30+
ngx.log(ngx.ERR, 'using upstream base:', upstream)
31+
return upstream
32+
}
33+
set_by_lua_block $npm_config_registry {
34+
local upstream = os.getenv("npm_config_registry"):gsub("/+$", "")
35+
ngx.log(ngx.ERR, 'using upstream server:', upstream)
36+
return upstream
37+
}
38+
location ~ ^/.*\.tgz$ {
39+
resolver 127.0.0.1 ipv6=off;
40+
proxy_pass $npm_config_registry;
41+
proxy_cache STATIC;
42+
proxy_cache_valid 200 1d;
43+
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
44+
}
45+
location / {
46+
resolver 127.0.0.1 ipv6=off;
47+
proxy_pass $npm_config_registry;
48+
proxy_buffers 32 1m;
49+
proxy_cache STATIC;
50+
proxy_cache_valid 200 1d;
51+
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
52+
# need to disable encoding in order to be able to filter the response body
53+
proxy_set_header Accept-Encoding "";
54+
# modifying the response body will change the length
55+
header_filter_by_lua_block {
56+
ngx.header.content_length = nil
57+
}
58+
# replace all occurances of, eg. https://registry.npmjs.org with http://127.0.0.1:4873
59+
body_filter_by_lua_block {
60+
local upstream = ngx.var.upstream_base
61+
-- need to construct URL because we may be proxying http<->https
62+
local base = ngx.var.scheme .. '://' .. ngx.var.http_host
63+
ngx.log(ngx.ERR, "Modifying JSON of " .. ngx.var.uri .. " to replace '" .. upstream .. "' with '" .. base .. "'")
64+
ngx.arg[1] = string.gsub(ngx.arg[1], upstream, base)
65+
}
66+
}
67+
}
68+
}

sinopia.sh

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)