From b5495ed0c348f65ea83d9de7a1d5068310146705 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 21 Apr 2015 01:14:10 -0400 Subject: [PATCH 1/5] use helper functions instead of echo command --- plugins/domains/commands | 2 +- plugins/nginx-vhosts/commands | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/domains/commands b/plugins/domains/commands index 950ee2415..282d1eb65 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -45,7 +45,7 @@ case "$1" in VHOST=$(< "$DOKKU_ROOT/HOSTNAME") fi if [[ "$VHOST" =~ $RE_IPV4 ]] || [[ "$VHOST" =~ $RE_IPV6 ]];then - echo "unsupported vhost config found. disabling vhost support" + dokku_log_info2 "unsupported vhost config found. disabling vhost support" dokku config:set-norestart $APP NO_VHOST=1 else if [[ -f "$DOKKU_ROOT/VHOST" ]]; then diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index 13e601cdf..84ca6d895 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -97,7 +97,7 @@ EOF dokku_log_info1 "Running nginx-pre-reload" pluginhook nginx-pre-reload $APP $DOKKU_APP_LISTEN_PORT $DOKKU_APP_LISTEN_IP - echo " Reloading nginx" + dokku_log_verbose "Reloading nginx" restart_nginx fi From b299f66c288612b69b9efddb8da8a50fd4bc0475 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 21 Apr 2015 01:58:56 -0400 Subject: [PATCH 2/5] Validate nginx configuration before reloading nginx. Closes #999 --- plugins/nginx-vhosts/commands | 12 ++++++++++++ plugins/nginx-vhosts/install | 20 +++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index 84ca6d895..cafe92a28 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -2,6 +2,17 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$(dirname $0)/../common/functions" +validate_nginx () { + set +e + sudo /usr/sbin/nginx -t > /dev/null 2>&1 + exit_code=$? + set -e + if [[ "$exit_code" -ne "0" ]]; then + sudo /usr/sbin/nginx -t + exit "$exit_code" + fi +} + restart_nginx () { case "$DOKKU_DISTRO" in ubuntu) @@ -98,6 +109,7 @@ EOF pluginhook nginx-pre-reload $APP $DOKKU_APP_LISTEN_PORT $DOKKU_APP_LISTEN_IP dokku_log_verbose "Reloading nginx" + validate_nginx restart_nginx fi diff --git a/plugins/nginx-vhosts/install b/plugins/nginx-vhosts/install index 29ac86ee2..c348c3b22 100755 --- a/plugins/nginx-vhosts/install +++ b/plugins/nginx-vhosts/install @@ -1,19 +1,17 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -if [[ ! -f /etc/sudoers.d/dokku-nginx ]]; then - case "$DOKKU_DISTRO" in - ubuntu) - echo "%dokku ALL=(ALL) NOPASSWD:/etc/init.d/nginx reload" >> /etc/sudoers.d/dokku-nginx - ;; +case "$DOKKU_DISTRO" in + ubuntu) + echo "%dokku ALL=(ALL) NOPASSWD:/etc/init.d/nginx reload, /usr/sbin/nginx -t" > /etc/sudoers.d/dokku-nginx + ;; - opensuse) - echo "%dokku ALL=(ALL) NOPASSWD:/sbin/service nginx reload" >> /etc/sudoers.d/dokku-nginx - ;; - esac + opensuse) + echo "%dokku ALL=(ALL) NOPASSWD:/sbin/service nginx reload, /usr/sbin/nginx -t" > /etc/sudoers.d/dokku-nginx + ;; +esac - chmod 0440 /etc/sudoers.d/dokku-nginx -fi +chmod 0440 /etc/sudoers.d/dokku-nginx # if dokku.conf has not been created, create it if [[ ! -f /etc/nginx/conf.d/dokku.conf ]]; then From a8f08bd5a86546178030b7ea1c45dd1a3c36c38a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 21 Apr 2015 02:38:47 -0400 Subject: [PATCH 3/5] [ci skip] Add documentation for custom nginx.conf files Closes #998 Closes #1000 --- docs/nginx.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/nginx.md b/docs/nginx.md index 7f176a54f..947e7d15f 100644 --- a/docs/nginx.md +++ b/docs/nginx.md @@ -77,6 +77,12 @@ server { The above is a sample http configuration that adds an `X-Served-By` header to requests. The template file **must** be owned by the user `dokku:dokku`. +A few tips for custom nginx templates: + +- Special characters - dollar signs, spaces inside of quotes, etc. - should be escaped with a single backslash or can cause deploy failures. +- Templated files will be validated via `nginx -t`. +- Application environment variables can be used within your nginx configuration. + ## Customizing hostnames Applications typically have the following structure for their hostname: From 661b6706def14c1ad5e0f59fb597095cdad88efc Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Tue, 21 Apr 2015 10:58:11 -0700 Subject: [PATCH 4/5] fix verify_app_name to bomb on null app. use it in nginx:build-config --- plugins/common/functions | 1 + plugins/nginx-vhosts/commands | 1 + 2 files changed, 2 insertions(+) diff --git a/plugins/common/functions b/plugins/common/functions index da87b1300..8db3af6dd 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -104,6 +104,7 @@ dokku_container_log_verbose_quiet() { verify_app_name() { local APP="$1" + [[ ! -n "$APP" ]] && dokku_log_fail "(verify_app_name) APP must not be null" [[ ! -d "$DOKKU_ROOT/$APP" ]] && dokku_log_fail "App $APP does not exist" IMAGE="dokku/$APP" return 0 diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index cafe92a28..19b48df10 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -28,6 +28,7 @@ restart_nginx () { case "$1" in nginx:build-config) APP="$2"; DOKKU_APP_LISTEN_PORT="$3"; DOKKU_APP_LISTEN_IP="${4}" + verify_app_name "$APP" VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" URLS_PATH="$DOKKU_ROOT/$APP/URLS" WILDCARD_SSL="$DOKKU_ROOT/tls" From f55e047619b37f907347d24dcd565f53ab313aa2 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Tue, 21 Apr 2015 11:53:00 -0700 Subject: [PATCH 5/5] negative test for validate_nginx --- tests/unit/nginx-vhosts.bats | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/unit/nginx-vhosts.bats b/tests/unit/nginx-vhosts.bats index 4fc07af94..66838c126 100644 --- a/tests/unit/nginx-vhosts.bats +++ b/tests/unit/nginx-vhosts.bats @@ -94,3 +94,12 @@ assert_http_success() { deploy_app assert_nonssl_domain "www.test.app.dokku.me" } + +@test "(nginx-vhosts) nginx:build-config (validate_nginx)" { + deploy_app + echo "some lame nginx config" > "$DOKKU_ROOT/$TEST_APP/nginx.conf.template" + run /bin/bash -c "dokku nginx:build-config $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_failure +}