From 023f04c7a4231b6d9a05e43319c2e3594e52d71b Mon Sep 17 00:00:00 2001 From: Michael Contento Date: Wed, 29 Mar 2017 19:23:29 +0200 Subject: [PATCH] refactor REDIRECT_PROTO and add some examples --- Dockerfile | 2 +- .../sites-available/redirect-apex-to-www.conf | 5 --- .../redirect-apex-to-www.conf.tpl | 6 ++++ .../sites-available/redirect-www-to-apex.conf | 5 --- .../redirect-www-to-apex.conf.tpl | 5 +++ .../{site.conf => site.conf.tpl} | 0 examples/phpinfo-behind-https/Caddyfile | 7 ++++ .../phpinfo-behind-https/docker-compose.yml | 13 +++++++ examples/phpinfo-behind-https/index.php | 3 ++ examples/phpinfo/docker-compose.yml | 8 +++++ examples/phpinfo/index.php | 3 ++ usr/bin/docker-start | 34 +++++++++++-------- 12 files changed, 66 insertions(+), 25 deletions(-) delete mode 100644 etc/nginx/sites-available/redirect-apex-to-www.conf create mode 100644 etc/nginx/sites-available/redirect-apex-to-www.conf.tpl delete mode 100644 etc/nginx/sites-available/redirect-www-to-apex.conf create mode 100644 etc/nginx/sites-available/redirect-www-to-apex.conf.tpl rename etc/nginx/sites-available/{site.conf => site.conf.tpl} (100%) create mode 100644 examples/phpinfo-behind-https/Caddyfile create mode 100644 examples/phpinfo-behind-https/docker-compose.yml create mode 100644 examples/phpinfo-behind-https/index.php create mode 100644 examples/phpinfo/docker-compose.yml create mode 100644 examples/phpinfo/index.php diff --git a/Dockerfile b/Dockerfile index c16e659..32709d2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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/ diff --git a/etc/nginx/sites-available/redirect-apex-to-www.conf b/etc/nginx/sites-available/redirect-apex-to-www.conf deleted file mode 100644 index 9e8875b..0000000 --- a/etc/nginx/sites-available/redirect-apex-to-www.conf +++ /dev/null @@ -1,5 +0,0 @@ -server { - listen 80; - server_name ~^(?!www.)(?.+)$; - return {{REDIRECT_CODE}} {{REDIRECT_SCHEME}}://www.$domain$request_uri; -} diff --git a/etc/nginx/sites-available/redirect-apex-to-www.conf.tpl b/etc/nginx/sites-available/redirect-apex-to-www.conf.tpl new file mode 100644 index 0000000..31e10fd --- /dev/null +++ b/etc/nginx/sites-available/redirect-apex-to-www.conf.tpl @@ -0,0 +1,6 @@ +server { + listen 80; + server_name ~^(?!www.)(?.+)$; + + return {{REDIRECT_CODE}} {{REDIRECT_PROTO}}://www.$domain$request_uri; +} diff --git a/etc/nginx/sites-available/redirect-www-to-apex.conf b/etc/nginx/sites-available/redirect-www-to-apex.conf deleted file mode 100644 index b367d09..0000000 --- a/etc/nginx/sites-available/redirect-www-to-apex.conf +++ /dev/null @@ -1,5 +0,0 @@ -server { - listen 80; - server_name ~^www.(?.+)$; - return {{REDIRECT_CODE}} {{REDIRECT_SCHEME}}://$domain$request_uri; -} diff --git a/etc/nginx/sites-available/redirect-www-to-apex.conf.tpl b/etc/nginx/sites-available/redirect-www-to-apex.conf.tpl new file mode 100644 index 0000000..cdc463a --- /dev/null +++ b/etc/nginx/sites-available/redirect-www-to-apex.conf.tpl @@ -0,0 +1,5 @@ +server { + listen 80; + server_name ~^www.(?.+)$; + return {{REDIRECT_CODE}} {{REDIRECT_PROTO}}://$domain$request_uri; +} diff --git a/etc/nginx/sites-available/site.conf b/etc/nginx/sites-available/site.conf.tpl similarity index 100% rename from etc/nginx/sites-available/site.conf rename to etc/nginx/sites-available/site.conf.tpl diff --git a/examples/phpinfo-behind-https/Caddyfile b/examples/phpinfo-behind-https/Caddyfile new file mode 100644 index 0000000..fd5c9b4 --- /dev/null +++ b/examples/phpinfo-behind-https/Caddyfile @@ -0,0 +1,7 @@ +https://* { + errors stderr + tls self_signed + proxy / http://backend:80 { + transparent + } +} diff --git a/examples/phpinfo-behind-https/docker-compose.yml b/examples/phpinfo-behind-https/docker-compose.yml new file mode 100644 index 0000000..2a7bf5c --- /dev/null +++ b/examples/phpinfo-behind-https/docker-compose.yml @@ -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' diff --git a/examples/phpinfo-behind-https/index.php b/examples/phpinfo-behind-https/index.php new file mode 100644 index 0000000..83f1549 --- /dev/null +++ b/examples/phpinfo-behind-https/index.php @@ -0,0 +1,3 @@ +&2 + echo "ERROR: Valid values are: 'auto', 'http' or 'https'" >&2 + exit 1 +fi -for file in /etc/nginx/*/*.conf; do - sed -i \ - -e "s#{{REDIRECT_CODE}}#${REDIRECT_CODE}#g" \ +# Replace templates in nginx config +for file in /etc/nginx/*/*.tpl; do + sed \ -e "s#{{DOCUMENT_ROOT}}#${DOCUMENT_ROOT}#g" \ - -e "s#{{REDIRECT_SCHEME}}#${REDIRECT_SCHEME}#g" \ - "${file}" + -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 [[ "${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 +if [[ -f "/etc/nginx/sites-available/redirect-${REDIRECT_MODE}.conf" ]]; then ln -s \ - /etc/nginx/sites-available/redirect-www-to-apex.conf \ - /etc/nginx/sites-enabled/redirect-www-to-apex.conf + "/etc/nginx/sites-available/redirect-${REDIRECT_MODE}.conf" \ + "/etc/nginx/sites-enabled/redirect-${REDIRECT_MODE}.conf" fi -exec /usr/bin/supervisord +exec /usr/bin/supervisord -c /etc/supervisord.conf