refactor REDIRECT_PROTO and add some examples

This commit is contained in:
Michael Contento 2017-03-29 19:23:29 +02:00
parent b6c804b68c
commit 023f04c7a4
12 changed files with 72 additions and 31 deletions

View File

@ -35,7 +35,7 @@ ENV REDIRECT_CODE=302
# And the protocol we should redirect to. Change this to "https" if you
# serve via https (e.g. with a SSL-termination proxy infront of this)
ENV REDIRECT_SCHEME="http"
ENV REDIRECT_PROTO="auto"
ADD etc/ /etc/
ADD usr/ /usr/

View File

@ -1,5 +0,0 @@
server {
listen 80;
server_name ~^(?!www.)(?<domain>.+)$;
return {{REDIRECT_CODE}} {{REDIRECT_SCHEME}}://www.$domain$request_uri;
}

View File

@ -0,0 +1,6 @@
server {
listen 80;
server_name ~^(?!www.)(?<domain>.+)$;
return {{REDIRECT_CODE}} {{REDIRECT_PROTO}}://www.$domain$request_uri;
}

View File

@ -1,5 +0,0 @@
server {
listen 80;
server_name ~^www.(?<domain>.+)$;
return {{REDIRECT_CODE}} {{REDIRECT_SCHEME}}://$domain$request_uri;
}

View File

@ -0,0 +1,5 @@
server {
listen 80;
server_name ~^www.(?<domain>.+)$;
return {{REDIRECT_CODE}} {{REDIRECT_PROTO}}://$domain$request_uri;
}

View File

@ -0,0 +1,7 @@
https://* {
errors stderr
tls self_signed
proxy / http://backend:80 {
transparent
}
}

View File

@ -0,0 +1,13 @@
version: '3'
services:
backend:
build: ../../
volumes:
- './index.php:/var/www/index.php'
frontend:
image: abiosoft/caddy
volumes:
- './Caddyfile:/etc/Caddyfile'
ports:
- '443:443'

View File

@ -0,0 +1,3 @@
<?php
phpinfo();

View File

@ -0,0 +1,8 @@
version: '3'
services:
backend:
build: ../../
volumes:
- './index.php:/var/www/index.php'
ports:
- '80:80'

View File

@ -0,0 +1,3 @@
<?php
phpinfo();

View File

@ -1,25 +1,31 @@
#!/usr/bin/env sh
set -eu
chown www-data.www-data "${DOCUMENT_ROOT}"
for file in /etc/nginx/*/*.conf; do
sed -i \
-e "s#{{REDIRECT_CODE}}#${REDIRECT_CODE}#g" \
-e "s#{{DOCUMENT_ROOT}}#${DOCUMENT_ROOT}#g" \
-e "s#{{REDIRECT_SCHEME}}#${REDIRECT_SCHEME}#g" \
"${file}"
done
rm -f /etc/nginx/sites-enabled/redirect-*.conf
if [[ "${REDIRECT_MODE}" == "apex-to-www" ]]; then
ln -s \
/etc/nginx/sites-available/redirect-apex-to-www.conf \
/etc/nginx/sites-enabled/redirect-apex-to-www.conf
elif [[ "${REDIRECT_MODE}" == "www-to-apex" ]]; then
ln -s \
/etc/nginx/sites-available/redirect-www-to-apex.conf \
/etc/nginx/sites-enabled/redirect-www-to-apex.conf
# Properly detect requested redirect
if [ "${REDIRECT_PROTO}" == "auto" ]; then
REDIRECT_PROTO="\$http_x_forwarded_proto";
elif [ "${REDIRECT_PROTO}" != "http" ] && [ "${REDIRECT_PROTO}" != "https" ]; then
echo "ERROR: Invalid value for REDIRECT_PROTO, got '${REDIRECT_PROTO}'" >&2
echo "ERROR: Valid values are: 'auto', 'http' or 'https'" >&2
exit 1
fi
exec /usr/bin/supervisord
# Replace templates in nginx config
for file in /etc/nginx/*/*.tpl; do
sed \
-e "s#{{DOCUMENT_ROOT}}#${DOCUMENT_ROOT}#g" \
-e "s#{{REDIRECT_CODE}}#${REDIRECT_CODE}#g" \
-e "s#{{REDIRECT_PROTO}}#${REDIRECT_PROTO}#g" \
"${file}" \
> "${file%.tpl}"
done
# Activate the right
rm -f /etc/nginx/sites-enabled/redirect-*.conf
if [[ -f "/etc/nginx/sites-available/redirect-${REDIRECT_MODE}.conf" ]]; then
ln -s \
"/etc/nginx/sites-available/redirect-${REDIRECT_MODE}.conf" \
"/etc/nginx/sites-enabled/redirect-${REDIRECT_MODE}.conf"
fi
exec /usr/bin/supervisord -c /etc/supervisord.conf