Skip to content

Add configurable static asset hosting #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
name: Test & Build

on:
push:
branches:
- main
tags:
- 'v*'
pull_request:
push:
branches: [main]
tags: [v*]

jobs:
test:
runs-on: [self-hosted, linux, x64]
name: Test
runs-on: [self-hosted, linux]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Run tests
run: docker build --target test .
docker:
build:
name: Build
runs-on: [self-hosted, linux]
needs: test
uses: secondlife/docker-build-workflow/.github/workflows/build.yaml@v3
with:
image: platform/nginx-proxy
platforms: linux/amd64,linux/arm64
push: true
promote: ${{ startsWith(github.ref, 'refs/tags/v') }}
secrets:
github_pat: ${{ secrets.SHARED_GITHUB_TOKEN }}
jfrog_token: ${{ secrets.SHARED_JFROG_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: secondlife/action-build-docker@v1
with:
image: platform/nginx-proxy
platforms: linux/amd64,linux/arm64
promote: ${{ startsWith(github.ref, 'refs/tags/v') }}
jfrog-token: ${{ secrets.SHARED_JFROG_TOKEN }}
28 changes: 23 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
############
# Gomplate
############
FROM artifactory.secondlife.io/dockerhub/alpine:3 AS gomplate
ARG TARGETPLATFORM=linux/amd64
ARG GOMPLATE_VERSION=3.10.0
ARG GOMPLATE_AMD64_SHA256=603539aac4e09f98a8ca5b6e5da0c21213221206dc7175a5644255c7a22b936d
ARG GOMPLATE_ARM64_SHA256=3352ef521977ee39cdd406a9a7943d0b5f29772e5542995416bf093b90bbae2c
RUN apk add --no-cache curl bash
SHELL ["/bin/bash", "-c"]
RUN ARCH=${TARGETPLATFORM/linux\//} \
&& curl -Lf https://github.com/hairyhenderson/gomplate/releases/download/v${GOMPLATE_VERSION}/gomplate_linux-${ARCH}-slim -o /tmp/gomplate \
&& sha_envvar="GOMPLATE_${ARCH^^}_SHA256" \
&& GOMPLATE_SHA256="${!sha_envvar}" \
&& echo "${GOMPLATE_SHA256} /tmp/gomplate" | sha256sum -c \
&& chmod +x /tmp/gomplate
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm installing gomplate as a layer, but we could alternatively provide an OCI image for multiple platforms that applications could source from.


############
# Base
############
ARG ARCH=
FROM artifactory.secondlife.io/dockerhub/${ARCH}alpine:3 AS base
FROM artifactory.secondlife.io/dockerhub/alpine:3 AS base
COPY --from=gomplate /tmp/gomplate /usr/local/bin/
RUN apk add --no-cache \
bash \
nginx \
nginx-mod-http-headers-more
bash \
nginx \
nginx-mod-http-headers-more
COPY src /
ENV LISTEN_PORT=80
ENV STATIC_LOCATIONS=
EXPOSE 80
STOPSIGNAL SIGQUIT
ENTRYPOINT ["/docker-entrypoint.sh"]
Expand Down
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,50 @@ Pair nginx-proxy with your favorite upstream server (wsgi, uwsgi, asgi, et al.)
| `PROXY_REVERSE_URL` | Upstream server URL | Yes | | http://myapp:8080 |
| `LISTEN_PORT` | Server port | Yes | 8080 | |
| `SILENT` | Silence entrypoint output | No | | |
| `STATIC_LOCATIONS` | Static asset mappings | No | | |

### Hosting Static Assets

Static files can be hosted from your proxied application by sharing a volume
mount between nginx-proxy and your app container then defining a list of
hosted directories using `STATIC_LOCATIONS`.

In ECS, you can mount directies with with the `volumesFrom` directive. With
docker-compose like so:

```yaml
services:
app:
# ...
volumes:
static:/var/www/static
proxy:
# ...
environment:
STATIC_LOCATIONS:/static/:/var/www/static/
volumes:
static:/var/www/static
volumes:
static:
```

The syntax of `STATIC_LOCATIONS` is `HOSTED_PATH1:LOCAL_PATH1,HOSTED_PATH2:LOCAL_PATH2`

## Development

A test suite is baked into nginx-proxy's Dockerfile. You can run it by building
the test layer: `docker build --target test .`

[nginx container]: https://hub.docker.com/_/nginx
[mo]: https://github.com/tests-always-included/mo

### Differences from standard nginx container

Notable differences from the official [nginx container][]

- [mo][] is used to render nginx configuration templates so that image startup
- [gomplate][] is used to render nginx configuration templates so that image startup
is aborted if a template variable is missing. This is an improvement over the
official image, which uses `envsubst`.
- alpine's official nginx package is used in order to ensure compatibility with
distro-provided nginx modules. This is another enhancement, as the official
image cannot be used with alpine's nginx modules.

[nginx container]: https://hub.docker.com/_/nginx
[gomplate]: https://docs.gomplate.ca/
2 changes: 1 addition & 1 deletion src/docker-entrypoint.d/00-render-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ do
final=$(basename "$f")
final=${final%.template}
final="/etc/nginx/conf.d/$final"
cat "$f" | mo --fail-not-set --fail-on-function > "$final"
cat "$f" | gomplate > "$final"
log "$0: Rendered $f and moved it to $final"
done
13 changes: 11 additions & 2 deletions src/etc/nginx/templates/default.conf.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server {
listen {{LISTEN_PORT}};
listen {{ .Env.LISTEN_PORT }};
server_name localhost;
add_header X-Request-ID $request_id;

Expand All @@ -9,7 +9,16 @@ server {
add_header X-Content-Type-Options "nosniff";

location / {
proxy_pass {{PROXY_REVERSE_URL}};
proxy_pass {{ .Env.PROXY_REVERSE_URL }};
proxy_set_header X-Request-ID $request_id;
}

{{ if .Env.STATIC_LOCATIONS }}
{{ range (.Env.STATIC_LOCATIONS | strings.Split "," )}}
{{ $l := (. | strings.Split ":" )}}
location {{index $l 0 }} {
alias {{index $l 1 }};
}
{{ end }}
{{ end }}
}
Loading