From 4dd4d7c56768424714384aa146b072f30a57347c Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Thu, 17 Sep 2015 19:09:29 -0700 Subject: [PATCH] initial pass at nginx proxy without VHOST. closes #1123 --- plugins/00_dokku-standard/commands | 12 +- plugins/certs/commands | 5 +- plugins/common/functions | 13 ++ plugins/domains/commands | 3 +- plugins/domains/functions | 7 +- plugins/nginx-vhosts/commands | 200 ++++-------------- plugins/nginx-vhosts/functions | 198 +++++++++++++++++ plugins/nginx-vhosts/post-deploy | 3 +- plugins/nginx-vhosts/post-domains-update | 3 +- .../nginx.conf.ssl_terminated.template | 4 +- .../templates/nginx.conf.template | 4 +- .../templates/nginx.ssl.conf.template | 12 +- 12 files changed, 287 insertions(+), 177 deletions(-) create mode 100755 plugins/nginx-vhosts/functions diff --git a/plugins/00_dokku-standard/commands b/plugins/00_dokku-standard/commands index af5e43770..051956413 100755 --- a/plugins/00_dokku-standard/commands +++ b/plugins/00_dokku-standard/commands @@ -193,8 +193,18 @@ case "$1" in echo "$SCHEME://$(< "$DOKKU_ROOT/VHOST")" else for PORT_FILE in $DOKKU_ROOT/$APP/PORT.*; do - echo "$SCHEME://$(< "$DOKKU_ROOT/HOSTNAME"):$(< "$PORT_FILE")" + echo "http://$(< "$DOKKU_ROOT/HOSTNAME"):$(< "$PORT_FILE") (container)" done + + DOKKU_NGINX_PORT=$(config_get $APP DOKKU_NGINX_PORT || true) + DOKKU_NGINX_SSL_PORT=$(config_get $APP DOKKU_NGINX_SSL_PORT || true) + + if [[ -n "$DOKKU_NGINX_PORT" ]]; then + echo "http://$(< "$DOKKU_ROOT/HOSTNAME"):$DOKKU_NGINX_PORT (nginx)" + fi + if [[ -n "$DOKKU_NGINX_SSL_PORT" ]]; then + echo "https://$(< "$DOKKU_ROOT/HOSTNAME"):$DOKKU_NGINX_SSL_PORT (nginx-ssl)" + fi fi ;; diff --git a/plugins/certs/commands b/plugins/certs/commands index ba6e62bc0..2dea833fc 100755 --- a/plugins/certs/commands +++ b/plugins/certs/commands @@ -2,6 +2,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" source "$PLUGIN_AVAILABLE_PATH/certs/functions" +source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions" is_tar_import() { [[ -t 0 ]] && return 1 @@ -57,7 +58,7 @@ certs_set() { mv "$KEY_FILE" "$DOKKU_ROOT/$APP/tls/server.key" cd $DOKKU_ROOT rm -rf $TEMP_DIR - dokku nginx:build-config $APP + nginx_build_config $APP } case "$1" in @@ -84,7 +85,7 @@ case "$1" in mkdir -p "$DOKKU_ROOT/$APP/tls" dokku_log_info1 "Installing certificate and key..." mv -f $TMP_WORK_DIR/server.key $TMP_WORK_DIR/server.crt $SSL_PATH - [[ -n "$DOMAIN" ]] && (dokku domains:add $APP $DOMAIN || dokku nginx:build-config $APP) + [[ -n "$DOMAIN" ]] && (dokku domains:add $APP $DOMAIN || nginx_build_config $APP) dokku_log_info1 "The following is a certificate signing request that can be used" dokku_log_info1 "to generate an 'officially' signed SSL certificate for $APP at $DOMAIN" dokku_log_info1 "by a CA of your choosing." diff --git a/plugins/common/functions b/plugins/common/functions index fecfce1ef..f14132c14 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -330,3 +330,16 @@ docker_cleanup() { # shellcheck disable=SC2046 docker rmi $(docker images -f 'dangling=true' -q) &> /dev/null & } + +get_open_port() { + while true; do + local port=$(shuf -i 1025-65535 -n 1) + if ! nc -z 0.0.0.0 $port; then + echo $port + return 0 + else + continue + fi + done + dokku_log_fail "no open ports found...wut?!" +} diff --git a/plugins/domains/commands b/plugins/domains/commands index b7dea8836..3788efb8e 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -3,6 +3,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" source "$PLUGIN_AVAILABLE_PATH/config/functions" source "$PLUGIN_AVAILABLE_PATH/domains/functions" +source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions" RE_IPV4="([0-9]{1,3}[\.]){3}[0-9]{1,3}" @@ -94,7 +95,7 @@ case "$1" in echo "$DOMAIN" >> "$DOKKU_ROOT/$APP/VHOST" done # we need to restart the app to make sure we're binding to the appropriate network interface - dokku nginx:build-config $APP + nginx_build_config $APP plugn trigger post-domains-update $APP for DOMAIN in "$@"; do dokku_log_info1 "Added $DOMAIN to $APP" diff --git a/plugins/domains/functions b/plugins/domains/functions index 2a69c42a0..501f51908 100755 --- a/plugins/domains/functions +++ b/plugins/domains/functions @@ -3,6 +3,9 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" get_app_domains() { - local APP=$1; verify_app_name $APP - cat "$DOKKU_ROOT/$APP/VHOST" + local APP=$1; local VHOST_FILE="$DOKKU_ROOT/$APP/VHOST" + verify_app_name $APP + if [[ -f "$VHOST_FILE" ]]; then + cat "$VHOST_FILE" + fi } diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index 3851cdceb..d90349d79 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -1,168 +1,16 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" -source "$PLUGIN_AVAILABLE_PATH/certs/functions" source "$PLUGIN_AVAILABLE_PATH/config/functions" -source "$PLUGIN_AVAILABLE_PATH/domains/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) - sudo /etc/init.d/nginx reload > /dev/null - ;; - - opensuse) - sudo /sbin/service nginx reload > /dev/null - ;; - esac -} +source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions" 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_PATH="$DOKKU_ROOT/tls" - APP_SSL_PATH="$DOKKU_ROOT/$APP/tls" - APP_NGINX_TEMPLATE="$DOKKU_ROOT/$APP/nginx.conf.template" + APP="$2"; DOKKU_APP_LISTEN_PORT="$3"; DOKKU_APP_LISTEN_IP="$4" - if [[ -z "$DOKKU_APP_LISTEN_PORT" ]] && [[ -z "$DOKKU_APP_LISTEN_IP" ]]; then - shopt -s nullglob - for DOKKU_APP_IP_FILE in $DOKKU_ROOT/$APP/IP.web.*; do - DOKKU_APP_PORT_FILE=$(echo $DOKKU_APP_IP_FILE | sed -e "s:IP:PORT:g") - DOKKU_APP_LISTENER_IP=$(< $DOKKU_APP_IP_FILE) - DOKKU_APP_LISTENER_PORT=$(< $DOKKU_APP_PORT_FILE) + nginx_build_config $APP $DOKKU_APP_LISTEN_PORT $DOKKU_APP_LISTEN_IP - DOKKU_APP_LISTENERS+=" " - DOKKU_APP_LISTENERS+="$DOKKU_APP_LISTENER_IP:$DOKKU_APP_LISTENER_PORT" - DOKKU_APP_LISTENERS+=" " - done - shopt -u nullglob - fi - - DOKKU_APP_CIDS=($(get_app_container_ids $APP)) - docker cp "${DOKKU_APP_CIDS[0]}:/app/nginx.conf.template" "$DOKKU_ROOT/$APP/" 2> /dev/null || true - - eval "$(config_export global)" - eval "$(config_export app $APP)" - [[ -f "$APP_NGINX_TEMPLATE" ]] && NGINX_TEMPLATE="$APP_NGINX_TEMPLATE" && NGINX_CUSTOM_TEMPLATE="true" && dokku_log_info1 'Overriding default nginx.conf with detected nginx.conf.template' - - if [[ ! -n "$NO_VHOST" ]] && [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then - NONSSL_VHOSTS=$(get_app_domains $APP) - if [[ -n "$(is_ssl_enabled $APP)" ]]; then - SSL_HOSTNAME=$(get_ssl_hostnames $APP) - - [[ -n "$SSL_HOSTNAME" ]] && SSL_HOSTNAME_REGEX=$(echo "$SSL_HOSTNAME" | xargs | sed 's|\.|\\.|g' | sed 's/\*/\[^\.\]\*/g' | sed 's/ /|/g') - if (egrep -q "^${SSL_HOSTNAME_REGEX}$" $VHOST_PATH); then - SSL_INUSE=true - else - dokku_log_info1 "No matching configured domains for $APP found in SSL certificate. Your app will not be configured with an SSL endpoint" - dokku_log_info1 "Please add appropriate domains via the dokku domains command" - dokku_log_info1 "Configured domains for app:" - for domain in $(echo $NONSSL_VHOSTS| xargs); do - dokku_log_info2 "$domain" - done - dokku_log_info1 "Domains found in SSL certificate:" - for domain in $(echo $SSL_HOSTNAME | xargs); do - dokku_log_info2 "$domain" - done - fi - if [[ "$(is_ssl_enabled $APP)" == "app" ]]; then - SSL_DIRECTIVES=$(cat <> $NGINX_CONF" - done <<< "$SSL_VHOSTS" - fi - - if [[ -n "$NONSSL_VHOSTS" ]]; then - NOSSL_SERVER_NAME=$(echo $NONSSL_VHOSTS | tr '\n' ' ') - xargs -i echo "-----> Configuring {}..." <<< "$NONSSL_VHOSTS" - if [[ -n "$DOKKU_SSL_TERMINATED" ]] && [[ -z "$NGINX_CUSTOM_TEMPLATE" ]]; then - NGINX_TEMPLATE="$(dirname $0)/templates/nginx.conf.ssl_terminated.template" - elif [[ -z "$NGINX_CUSTOM_TEMPLATE" ]]; then - NGINX_TEMPLATE="$(dirname $0)/templates/nginx.conf.template" - fi - eval "cat <<< \"$(< $NGINX_TEMPLATE)\" >> $NGINX_CONF" - fi - - if [[ -n "$DOKKU_APP_LISTEN_PORT" ]] && [[ -n "$DOKKU_APP_LISTEN_IP" ]]; then - echo "upstream $APP { server $DOKKU_APP_LISTEN_IP:$DOKKU_APP_LISTEN_PORT; }" >> $NGINX_CONF - elif [[ -n "$DOKKU_APP_LISTENERS" ]]; then - echo "upstream $APP { " >> $NGINX_CONF - for listener in $DOKKU_APP_LISTENERS; do - echo " server $listener;" >> $NGINX_CONF - done - echo "}" >> $NGINX_CONF - fi - - dokku_log_info1 "Creating $SCHEME nginx.conf" - mv $NGINX_CONF "$DOKKU_ROOT/$APP/nginx.conf" - - if is_deployed "$APP"; then - dokku_log_info1 "Running nginx-pre-reload" - plugn trigger nginx-pre-reload $APP $DOKKU_APP_LISTEN_PORT $DOKKU_APP_LISTEN_IP - - dokku_log_verbose "Reloading nginx" - validate_nginx && restart_nginx - fi - - echo "# THIS FILE IS GENERATED BY DOKKU - DO NOT EDIT, YOUR CHANGES WILL BE OVERWRITTEN" > $URLS_PATH - xargs -i echo "https://{}" <<< "${SSL_VHOSTS}" >> $URLS_PATH - xargs -i echo "http://{}" <<< "${NONSSL_VHOSTS}" >> $URLS_PATH - else - if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then - dokku_log_info1 "VHOST support disabled, deleting $APP/VHOST" - rm "$DOKKU_ROOT/$APP/VHOST" - fi - if [[ -f "$DOKKU_ROOT/$APP/URLS" ]]; then - dokku_log_info1 "VHOST support disabled, deleting $APP/URLS" - rm "$DOKKU_ROOT/$APP/URLS" - fi - if [[ -f "$DOKKU_ROOT/$APP/nginx.conf" ]]; then - dokku_log_info1 "VHOST support disabled, deleting nginx.conf" - rm "$DOKKU_ROOT/$APP/nginx.conf" - - if is_deployed "$APP"; then - dokku_log_info1 "VHOST support disabled, reloading nginx after nginx.conf deletion" - validate_nginx && restart_nginx - fi - fi - fi - ;; + ;; nginx:access-logs|nginx:error-logs) [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 @@ -180,18 +28,52 @@ EOF fi tail "$NGINX_LOGS_ARGS" "$NGINX_LOGS_PATH" - ;; + ;; + + nginx:disable) + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + verify_app_name "$2" + APP="$2" + + eval "$(config_export app $APP)" + if [[ -z "$NO_VHOST" ]]; then + config_set --no-restart $APP NO_NGINX=1 + config_set $APP NO_VHOST=1 + elif [[ -z "$NO_NGINX" ]]; then + config_set --no-restart $APP NO_NGINX=1 + nginx_build_config $APP + else + dokku_log_info1 "nginx is already disable for app ($APP)" + fi + ;; + + nginx:enable) + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + verify_app_name "$2" + APP="$2" + + eval "$(config_export app $APP)" + if [[ -n "$NO_NGINX" ]]; then + config_unset --no-restart $APP NO_NGINX + unset NO_NGINX + nginx_build_config $APP + else + dokku_log_info1 "nginx is already enabled for app ($APP)" + fi + ;; help | nginx:help) cat<, (Re)builds nginx config for given app nginx:access-logs [-t], Show the nginx access logs for an application (-t follows) nginx:error-logs [-t], Show the nginx error logs for an application (-t follows) + nginx:disable , disable nginx for an application (forces container binding to external interface) + nginx:enable , enable nginx for an application EOF - ;; + ;; *) exit $DOKKU_NOT_IMPLEMENTED_EXIT - ;; + ;; esac diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions new file mode 100755 index 000000000..1382c92f1 --- /dev/null +++ b/plugins/nginx-vhosts/functions @@ -0,0 +1,198 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/certs/functions" +source "$PLUGIN_AVAILABLE_PATH/config/functions" +source "$PLUGIN_AVAILABLE_PATH/domains/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) + sudo /etc/init.d/nginx reload > /dev/null + ;; + + opensuse) + sudo /sbin/service nginx reload > /dev/null + ;; + esac +} + +nginx_build_config() { + local APP="$1"; local DOKKU_APP_LISTEN_PORT="$2"; local DOKKU_APP_LISTEN_IP="$3" + verify_app_name "$APP" + VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" + URLS_PATH="$DOKKU_ROOT/$APP/URLS" + WILDCARD_SSL_PATH="$DOKKU_ROOT/tls" + APP_SSL_PATH="$DOKKU_ROOT/$APP/tls" + APP_NGINX_TEMPLATE="$DOKKU_ROOT/$APP/nginx.conf.template" + + eval "$(config_export global)" + eval "$(config_export app $APP)" + + if [[ ! -n "$NO_NGINX" ]]; then + if [[ -z "$DOKKU_APP_LISTEN_PORT" ]] && [[ -z "$DOKKU_APP_LISTEN_IP" ]]; then + shopt -s nullglob + for DOKKU_APP_IP_FILE in $DOKKU_ROOT/$APP/IP.web.*; do + DOKKU_APP_PORT_FILE=$(echo $DOKKU_APP_IP_FILE | sed -e "s:IP:PORT:g") + DOKKU_APP_LISTENER_IP=$(< $DOKKU_APP_IP_FILE) + DOKKU_APP_LISTENER_PORT=$(< $DOKKU_APP_PORT_FILE) + + DOKKU_APP_LISTENERS+=" " + DOKKU_APP_LISTENERS+="$DOKKU_APP_LISTENER_IP:$DOKKU_APP_LISTENER_PORT" + DOKKU_APP_LISTENERS+=" " + done + shopt -u nullglob + fi + + DOKKU_APP_CIDS=($(get_app_container_ids $APP)) + docker cp "${DOKKU_APP_CIDS[0]}:/app/nginx.conf.template" "$DOKKU_ROOT/$APP/" 2> /dev/null || true + + [[ -f "$APP_NGINX_TEMPLATE" ]] && NGINX_TEMPLATE="$APP_NGINX_TEMPLATE" && NGINX_CUSTOM_TEMPLATE="true" && dokku_log_info1 'Overriding default nginx.conf with detected nginx.conf.template' + + NGINX_CONF=$(mktemp -t "nginx.conf.XXXXXX") + SCHEME="http" + + if [[ -n "$NO_VHOST" ]]; then + if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then + dokku_log_info1 "VHOST support disabled, deleting $APP/VHOST" + rm "$DOKKU_ROOT/$APP/VHOST" + fi + fi + + if [[ -z "$DOKKU_NGINX_PORT" ]]; then + if [[ -n "$NO_VHOST" ]]; then + dokku_log_info1 "no nginx port set. setting to random open high port" + local NGINX_PORT=$(get_open_port) + else + local NGINX_PORT=80 + fi + config_set --no-restart $APP DOKKU_NGINX_PORT=${NGINX_PORT} + else + NGINX_PORT=$DOKKU_NGINX_PORT + fi + + NONSSL_VHOSTS=$(get_app_domains $APP) + if [[ -n "$(is_ssl_enabled $APP)" ]]; then + SSL_HOSTNAME=$(get_ssl_hostnames $APP) + SSL_INUSE=true + + [[ -n "$SSL_HOSTNAME" ]] && SSL_HOSTNAME_REGEX=$(echo "$SSL_HOSTNAME" | xargs | sed 's|\.|\\.|g' | sed 's/\*/\[^\.\]\*/g' | sed 's/ /|/g') + if ! (egrep -q "^${SSL_HOSTNAME_REGEX}$" $VHOST_PATH &> /dev/null); then + dokku_log_info1 "No matching configured domains for $APP found in SSL certificate. Your app will show as insecure in a browser if accessed via SSL" + dokku_log_info1 "Please add appropriate domains via the dokku domains command" + [[ -n "$NONSSL_VHOSTS" ]] && dokku_log_info1 "Configured domains for app:" + for domain in $(echo $NONSSL_VHOSTS| xargs); do + dokku_log_info2 "$domain" + done + [[ -n "$SSL_HOSTNAME" ]] && dokku_log_info1 "Domains found in SSL certificate:" + for domain in $(echo $SSL_HOSTNAME | xargs); do + dokku_log_info2 "$domain" + done + fi + + if [[ "$(is_ssl_enabled $APP)" == "app" ]]; then + SSL_DIRECTIVES=$(cat < /dev/null || true) + + if [[ -z "$DOKKU_NGINX_SSL_PORT" ]]; then + if [[ -n "$NO_VHOST" ]]; then + dokku_log_info1 "no nginx ssl port set. setting to random open high port" + local NGINX_SSL_PORT=$(get_open_port) + else + NGINX_SSL_PORT=443 + fi + config_set --no-restart $APP DOKKU_NGINX_SSL_PORT=${NGINX_SSL_PORT} + else + local NGINX_SSL_PORT=$DOKKU_NGINX_SSL_PORT + fi + + while read line; do + [[ -z "$line" ]] && continue + dokku_log_info1 "Configuring SSL for $line..." + SSL_SERVER_NAME=$line + NOSSL_SERVER_NAME=$line + eval "cat <<< \"$(< $NGINX_TEMPLATE)\" >> $NGINX_CONF" + done <<< "$SSL_VHOSTS" + fi + + if [[ -n "$DOKKU_SSL_TERMINATED" ]] && [[ -z "$NGINX_CUSTOM_TEMPLATE" ]]; then + NGINX_TEMPLATE="$(dirname $0)/templates/nginx.conf.ssl_terminated.template" + elif [[ -z "$NGINX_CUSTOM_TEMPLATE" ]]; then + NGINX_TEMPLATE="$(dirname $0)/templates/nginx.conf.template" + fi + + if [[ -n "$NONSSL_VHOSTS" ]]; then + NOSSL_SERVER_NAME=$(echo $NONSSL_VHOSTS | tr '\n' ' ') + xargs -i echo "-----> Configuring {}..." <<< "$NONSSL_VHOSTS" + fi + eval "cat <<< \"$(< $NGINX_TEMPLATE)\" >> $NGINX_CONF" + [[ -n "$NO_VHOST" ]] && sed --in-place -n -e '/^.*server_name.*$/!p' $NGINX_CONF + + if [[ -n "$DOKKU_APP_LISTEN_PORT" ]] && [[ -n "$DOKKU_APP_LISTEN_IP" ]]; then + echo "upstream $APP { server $DOKKU_APP_LISTEN_IP:$DOKKU_APP_LISTEN_PORT; }" >> $NGINX_CONF + elif [[ -n "$DOKKU_APP_LISTENERS" ]]; then + echo "upstream $APP { " >> $NGINX_CONF + for listener in $DOKKU_APP_LISTENERS; do + echo " server $listener;" >> $NGINX_CONF + done + echo "}" >> $NGINX_CONF + fi + + dokku_log_info1 "Creating $SCHEME nginx.conf" + mv $NGINX_CONF "$DOKKU_ROOT/$APP/nginx.conf" + + if is_deployed "$APP"; then + dokku_log_info1 "Running nginx-pre-reload" + plugn trigger nginx-pre-reload $APP $DOKKU_APP_LISTEN_PORT $DOKKU_APP_LISTEN_IP + + dokku_log_verbose "Reloading nginx" + validate_nginx && restart_nginx + fi + + if [[ -z "$NO_VHOST" ]]; then + echo "# THIS FILE IS GENERATED BY DOKKU - DO NOT EDIT, YOUR CHANGES WILL BE OVERWRITTEN" > $URLS_PATH + xargs -i echo "https://{}" <<< "${SSL_VHOSTS}" >> $URLS_PATH + xargs -i echo "http://{}" <<< "${NONSSL_VHOSTS}" >> $URLS_PATH + fi + else + dokku_log_info1 "nginx support is disabled for app ($APP)." + if [[ -f "$DOKKU_ROOT/$APP/nginx.conf" ]]; then + dokku_log_info1 "deleting nginx.conf" + rm "$DOKKU_ROOT/$APP/nginx.conf" + + if is_deployed "$APP"; then + dokku_log_info1 "reloading nginx after nginx.conf deletion" + validate_nginx && restart_nginx + fi + fi + fi +} diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index e3fd84430..cee12b518 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -2,6 +2,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" source "$PLUGIN_AVAILABLE_PATH/config/functions" +source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions" APP="$1" if [[ -f "$DOKKU_ROOT/$APP/IP.web.1" ]] && [[ -f "$DOKKU_ROOT/$APP/PORT.web.1" ]]; then @@ -13,5 +14,5 @@ if [[ -f "$DOKKU_ROOT/$APP/IP.web.1" ]] && [[ -f "$DOKKU_ROOT/$APP/PORT.web.1" ] dokku domains:setup $APP fi - dokku nginx:build-config $APP + nginx_build_config $APP fi diff --git a/plugins/nginx-vhosts/post-domains-update b/plugins/nginx-vhosts/post-domains-update index 20abd9d5e..b62f24ac5 100755 --- a/plugins/nginx-vhosts/post-domains-update +++ b/plugins/nginx-vhosts/post-domains-update @@ -1,7 +1,8 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions" APP="$1" -dokku nginx:build-config $APP +nginx_build_config $APP diff --git a/plugins/nginx-vhosts/templates/nginx.conf.ssl_terminated.template b/plugins/nginx-vhosts/templates/nginx.conf.ssl_terminated.template index bc7bc3a1d..642b0d653 100644 --- a/plugins/nginx-vhosts/templates/nginx.conf.ssl_terminated.template +++ b/plugins/nginx-vhosts/templates/nginx.conf.ssl_terminated.template @@ -1,8 +1,8 @@ # Nginx configuration when running behind a load balancer that terminates SSL # connections (e.g. AWS ELB) server { - listen [::]:80; - listen 80; + listen [::]:$NGINX_PORT; + listen $NGINX_PORT; server_name $NOSSL_SERVER_NAME; location / { proxy_pass http://$APP; diff --git a/plugins/nginx-vhosts/templates/nginx.conf.template b/plugins/nginx-vhosts/templates/nginx.conf.template index ad29248c2..23a88e1da 100644 --- a/plugins/nginx-vhosts/templates/nginx.conf.template +++ b/plugins/nginx-vhosts/templates/nginx.conf.template @@ -1,6 +1,6 @@ server { - listen [::]:80; - listen 80; + listen [::]:$NGINX_PORT; + listen $NGINX_PORT; server_name $NOSSL_SERVER_NAME; access_log /var/log/nginx/${APP}-access.log; error_log /var/log/nginx/${APP}-error.log; diff --git a/plugins/nginx-vhosts/templates/nginx.ssl.conf.template b/plugins/nginx-vhosts/templates/nginx.ssl.conf.template index 312cd83c3..ac49e6e9b 100644 --- a/plugins/nginx-vhosts/templates/nginx.ssl.conf.template +++ b/plugins/nginx-vhosts/templates/nginx.ssl.conf.template @@ -1,22 +1,22 @@ server { - listen [::]:80; - listen 80; + listen [::]:$NGINX_PORT; + listen $NGINX_PORT; server_name $NOSSL_SERVER_NAME; access_log /var/log/nginx/${APP}-access.log; error_log /var/log/nginx/${APP}-error.log; - return 301 https://\$host\$request_uri; + return 301 https://\$host:$NGINX_SSL_PORT\$request_uri; } server { - listen [::]:443 ssl spdy; - listen 443 ssl spdy; + listen [::]:$NGINX_SSL_PORT ssl spdy; + listen $NGINX_SSL_PORT ssl spdy; server_name $SSL_SERVER_NAME; access_log /var/log/nginx/${APP}-access.log; error_log /var/log/nginx/${APP}-error.log; $SSL_DIRECTIVES keepalive_timeout 70; - add_header Alternate-Protocol 443:npn-spdy/2; + add_header Alternate-Protocol $NGINX_SSL_PORT:npn-spdy/2; location / { gzip on;