From e2e6964944ea718e2e3b35d096deb8dca0daad3a Mon Sep 17 00:00:00 2001 From: Tim Schneider Date: Tue, 23 Jan 2018 09:48:42 +0100 Subject: [PATCH 1/2] Adding the ability to serve static files with nginx-proxy This is done by intruducing a new VIRTUAL_PROTO=static. The VIRTUAL_ROOT variable points to the static root which should be mounted into the container as volume. Often the container of the static website does not need to run, so I introduced a new environment variable INCLUDE_STOPPED_CONTAINERS that adds the -include-stopped flag to docker-gen --- Procfile | 2 +- README.md | 9 +++++++++ docker-entrypoint.sh | 4 ++++ nginx.tmpl | 4 ++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Procfile b/Procfile index 29fe166..49cc933 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1,2 @@ -dockergen: docker-gen -watch -notify "nginx -s reload" /app/nginx.tmpl /etc/nginx/conf.d/default.conf +dockergen: docker-gen -watch${INCLUDE_STOPPED} -notify "nginx -s reload" /app/nginx.tmpl /etc/nginx/conf.d/default.conf nginx: nginx diff --git a/README.md b/README.md index c44bf80..6209835 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,15 @@ than a socket and expose that port. If you use fastcgi,you can set `VIRTUAL_ROOT=xxx` for your root directory +### Static File Backends + +If you want your container to only serve static content, set `VIRTUAL_PROTO=static` on the container. +You can then set `VIRTUAL_ROOT=xxx` to your static files' root directory. If your static files live in a stopped +container, be sure to set the `INCLUDE_STOPPED_CONTAINERS=true` environment variable on the nginx-proxy container as +stopped containers will otherwise be ignored. + +You can create a minimal docker container with your static website files in a volume. The volume can then be mounted +into the nginx-proxy container and be served with this "backend". ### Default Host diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 7e7a312..db2b2b4 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -14,6 +14,10 @@ if [[ $DOCKER_HOST = unix://* ]]; then fi fi +if [ "${INCLUDE_STOPPED_CONTAINERS}" == "true" ]; then + export INCLUDE_STOPPED=" -include-stopped" +fi + # Generate dhparam file if required # Note: if $DHPARAM_BITS is not defined, generate-dhparam.sh will use 2048 as a default /app/generate-dhparam.sh $DHPARAM_BITS diff --git a/nginx.tmpl b/nginx.tmpl index 726c74b..13069a0 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -277,6 +277,8 @@ server { root {{ trim $vhost_root }}; include fastcgi.conf; fastcgi_pass {{ trim $upstream_name }}; + {{ else if eq $proto "static" }} + root {{ trim $vhost_root }}; {{ else }} proxy_pass {{ trim $proto }}://{{ trim $upstream_name }}; {{ end }} @@ -324,6 +326,8 @@ server { root {{ trim $vhost_root }}; include fastcgi.conf; fastcgi_pass {{ trim $upstream_name }}; + {{ else if eq $proto "static" }} + root {{ trim $vhost_root }}; {{ else }} proxy_pass {{ trim $proto }}://{{ trim $upstream_name }}; {{ end }} From be6487d5325adaebe9d757d288f3299c109a477c Mon Sep 17 00:00:00 2001 From: Tim Schneider Date: Tue, 23 Jan 2018 09:51:19 +0100 Subject: [PATCH 2/2] overwriting INCLUDE_STOPPED to be empty if INCLUDE_STOPPED_CONTAINERS is not set This prevents the passing of arbitrary flags to docker-gen --- docker-entrypoint.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index db2b2b4..85b506f 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -16,6 +16,8 @@ fi if [ "${INCLUDE_STOPPED_CONTAINERS}" == "true" ]; then export INCLUDE_STOPPED=" -include-stopped" +else + export INCLUDE_STOPPED="" fi # Generate dhparam file if required