Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 953d4ee

Browse files
author
Steffen Bleul
committed
Added Http Basic Auth for reverse proxies.
1 parent 279c988 commit 953d4ee

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ RUN export CONTAINER_USER=nginx && \
1919
apk add --update \
2020
ca-certificates \
2121
curl \
22-
openssl && \
22+
openssl \
23+
apache2-utils && \
2324
if [ "${NGINX_VERSION}" = "latest" ]; \
2425
then apk add nginx ; \
2526
else apk add "nginx=${NGINX_VERSION}" ; \

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,44 @@ $ docker run -d \
306306

307307
> LETSENCRYPT_CERTIFICATES switches on special configuration for their certificates.
308308
309+
# Basic User Authentification
310+
311+
You can password protect any reverse proxy. Additionally you can specify an arbitrary amount of users.
312+
313+
Example of specifying a user `admin` with password `admin` for the first reverse proxy:
314+
315+
~~~~
316+
docker run -d \
317+
-p 80:80 \
318+
--name nginx \
319+
-e "SERVER1REVERSE_PROXY_LOCATION1=/" \
320+
-e "SERVER1REVERSE_PROXY_PASS1=http://www.heise.de" \
321+
-e "SERVER1REVERSE_PROXY_BASIC_AUTH_REALM1=Secure Location" \
322+
-e "SERVER1REVERSE_PROXY_BASIC_AUTH1USER1=admin" \
323+
-e "SERVER1REVERSE_PROXY_BASIC_AUTH1PASSWORD1=admin" \
324+
blacklabelops/nginx
325+
~~~~
326+
327+
> Access to http://localhost will be now password protected with user `admin` and password `admin`.
328+
329+
Multiple users:
330+
331+
~~~~
332+
docker run -d \
333+
-p 80:80 \
334+
--name nginx \
335+
-e "SERVER1REVERSE_PROXY_LOCATION1=/" \
336+
-e "SERVER1REVERSE_PROXY_PASS1=http://www.heise.de" \
337+
-e "SERVER1REVERSE_PROXY_BASIC_AUTH_REALM1=Secure Location" \
338+
-e "SERVER1REVERSE_PROXY_BASIC_AUTH1USER1=admin1" \
339+
-e "SERVER1REVERSE_PROXY_BASIC_AUTH1PASSWORD1=admin1" \
340+
-e "SERVER1REVERSE_PROXY_BASIC_AUTH1USER2=admin2" \
341+
-e "SERVER1REVERSE_PROXY_BASIC_AUTH1PASSWORD2=admin2" \
342+
blacklabelops/nginx
343+
~~~~
344+
345+
> Access to http://localhost are both enabled for user `admin1` and user `admin2`.
346+
309347
# Build The Image
310348

311349
The build process can take the following argument:

imagescripts/reverse_proxy.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ _EOF_
9999
done
100100
}
101101

102+
function createBasicAuthFile() {
103+
local reverse_proxy_basic_auth_id=$1
104+
local passwd_file=$2
105+
for (( u=1; ; u++ ))
106+
do
107+
local VAR_BASIC_AUTH_USER="${reverse_proxy_basic_auth_id}USER${u}"
108+
local VAR_BASIC_AUTH_PASSWORD="${reverse_proxy_basic_auth_id}PASSWORD${u}"
109+
if [ ! -n "${!VAR_BASIC_AUTH_USER}" ]; then
110+
break
111+
fi
112+
local BASIC_AUTH_USER="${!VAR_BASIC_AUTH_USER}"
113+
local BASIC_AUTH_PASSWORD="${!VAR_BASIC_AUTH_PASSWORD}"
114+
htpasswd -b $passwd_file $BASIC_AUTH_USER $BASIC_AUTH_PASSWORD
115+
done
116+
}
117+
102118
for (( i=1; ; i++ ))
103119
do
104120
VAR_REVERSE_PROXY_LOCATION="$1REVERSE_PROXY_LOCATION$i"
@@ -109,6 +125,7 @@ do
109125
VAR_REVERSE_PROXY_HOST="$1SERVER_NAME"
110126
VAR_PROXY_APPLICATION="$1PROXY_APPLICATION"
111127
VAR_PROXY_APPLICATION_PROXY="$1REVERSE_PROXY_APPLICATION$i"
128+
VAR_REVERSE_PROXY_BASIC_AUTH_REALM="$1REVERSE_PROXY_BASIC_AUTH_REALM$i"
112129

113130
if [ ! -n "${!VAR_REVERSE_PROXY_LOCATION}" ]; then
114131
break
@@ -125,6 +142,7 @@ do
125142
NGINX_PROXY_HOST=${!VAR_REVERSE_PROXY_HOST}
126143
NGINX_PROXY_APPLICATION=${!VAR_PROXY_APPLICATION}
127144
NGINX_PROXY_APPLICATION_PROXY=${!VAR_PROXY_APPLICATION_PROXY}
145+
NGINX_PROXY_BASIC_AUTH_REALM=${!VAR_REVERSE_PROXY_BASIC_AUTH_REALM}
128146

129147
if [ -n "${NGINX_PROXY_APPLICATION_PROXY}" ]; then
130148
NGINX_PROXY_APPLICATION=${NGINX_PROXY_APPLICATION_PROXY}
@@ -148,6 +166,16 @@ _EOF_
148166
setProxyHeaderFields
149167
fi
150168

169+
if [ -n "${NGINX_PROXY_BASIC_AUTH_REALM}" ]; then
170+
htpasswd_file=$configFileReverseProxy/htpasswd_reverse_proxy$i
171+
touch $htpasswd_file
172+
cat >> $configFileReverseProxy/reverseProxy.conf <<_EOF_
173+
auth_basic "${NGINX_PROXY_BASIC_AUTH_REALM}";
174+
auth_basic_user_file ${htpasswd_file};
175+
_EOF_
176+
createBasicAuthFile "$1REVERSE_PROXY_BASIC_AUTH${i}" $htpasswd_file
177+
fi
178+
151179
if [ -n "${NGINX_PROXY_BUFFERING}" ]; then
152180
cat >> $configFileReverseProxy/reverseProxy.conf <<_EOF_
153181
proxy_buffering ${NGINX_PROXY_BUFFERING};

0 commit comments

Comments
 (0)