Skip to content
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

Resolver TTL #12

Open
wants to merge 6 commits into
base: latest
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ FROM nginx:alpine

ENV HTPASSWD='foo:$apr1$odHl5EJN$KbxMfo86Qdve2FH4owePn.' \
FORWARD_PORT=80 \
FORWARD_HOST=web
FORWARD_HOST=web \
PROXY_READ_TIMEOUT=900

WORKDIR /opt

RUN apk add --no-cache gettext
RUN apk add --no-cache gettext apache2-utils

COPY auth.conf auth.htpasswd launch.sh ./

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ docker run -d \
## Configuration
- `HTPASSWD` (default: `foo:$apr1$odHl5EJN$KbxMfo86Qdve2FH4owePn.`): Will be written to the .htpasswd file on launch (non-persistent)
- `FORWARD_PORT` (default: `80`): Port of the **source** container that should be forwarded
- `FORWARD_HOST` (default: `web`): Host of the **source** container that should be forwarded
- `PROXY_READ_TIMEOUT` (default: `900`): Timeout of the backend response
> The container does not need any volumes to be mounted! Nonetheless you will find all interesting files at `/etc/nginx/*`.

## Multiple Users
Expand All @@ -42,6 +44,20 @@ docker run -d --link web:web --name auth \
```
results in 2 users (`foo:bar` and `test:test`).

## Raw Credentials
If passing the contents of the HTPASSWD file is not convenient for you (because
you need to perform additional step of generating it via `htpasswd -nb foo
bar`), you can pass the credentials in a raw form and the contents of HTPASSWD
variable will be generated for you. The `RAW_CREDENTIALS=1` must be set to
enable this feature.

```
docker run -d --link web:web --name auth \
-e HTPASSWD=$'foo:bar\ntest:test' \
-e RAW_CREDENTIALS=1 \
beevelop/nginx-basic-auth
```

## Troubleshooting
```
nginx: [emerg] host not found in upstream "web" in /etc/nginx/conf.d/auth.conf:80
Expand Down
8 changes: 6 additions & 2 deletions auth.conf
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
server {
listen 80 default_server;

resolver $NAMESERVER valid=30s;

set $backend "http://${FORWARD_HOST}:${FORWARD_PORT}";

location / {
auth_basic "Restricted";
auth_basic_user_file auth.htpasswd;

proxy_pass http://${FORWARD_HOST}:${FORWARD_PORT};
proxy_read_timeout 900;
proxy_pass $backend;
proxy_read_timeout ${PROXY_READ_TIMEOUT};
}
}
16 changes: 15 additions & 1 deletion launch.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#!/bin/sh

if [ "$RAW_CREDENTIALS" = 1 ]; then
HTPASSWD=$(
for line in $(echo $HTPASSWD); do
USERNAME="$(echo "$line" | cut -d':' -f1)"
PASSWORD="$(echo "$line" | cut -d':' -f2)"
htpasswd -nb "$USERNAME" "$PASSWORD" | head -n1
done
)
fi

export NAMESERVER=$(cat /etc/resolv.conf | grep 'nameserver' | awk '{print $2}' | tr '\n' ' ')

rm /etc/nginx/conf.d/default.conf || :
envsubst < auth.conf > /etc/nginx/conf.d/auth.conf

envsubst '$NAMESERVER,$FORWARD_HOST,$FORWARD_PORT,$PROXY_READ_TIMEOUT' \
< auth.conf > /etc/nginx/conf.d/auth.conf
envsubst < auth.htpasswd > /etc/nginx/auth.htpasswd

nginx -g "daemon off;"