diff --git a/plugins/nginx-vhosts/post-deploy b/plugins/nginx-vhosts/post-deploy index 1bc802a92..f2905a905 100755 --- a/plugins/nginx-vhosts/post-deploy +++ b/plugins/nginx-vhosts/post-deploy @@ -2,6 +2,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x APP="$1"; PORT="$2" +VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" WILDCARD_SSL="$DOKKU_ROOT/tls" SSL="$DOKKU_ROOT/$APP/tls" @@ -21,20 +22,31 @@ restart_nginx () { if [[ -n "$NO_VHOST" ]]; then echo "-----> NO_VHOST config detected" +elif [[ -f "$DOKKU_ROOT/VHOST" ]]; then + [ -f $VHOST_PATH ] || { + echo "-----> Creating new $VHOST_PATH..." + if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then + cat "$DOKKU_ROOT/$APP/URL" > $VHOST_PATH + else + VHOST=$(< "$DOKKU_ROOT/VHOST") + SUBDOMAIN=${APP/%\.${VHOST}/} + hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST) + if [[ ! -n $hostname ]]; then + if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then + hostname="${APP/\//-}" + else + hostname="${APP/\//-}.$VHOST" + fi + fi + + echo $hostname > $VHOST_PATH + fi + } fi -if [[ -f "$DOKKU_ROOT/VHOST" && ! -n "$NO_VHOST" ]]; then - VHOST=$(< "$DOKKU_ROOT/VHOST") - SUBDOMAIN=${APP/%\.${VHOST}/} - hostname=$(: | pluginhook nginx-hostname $APP $SUBDOMAIN $VHOST) - if [[ ! -n $hostname ]]; then - if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then - hostname="${APP/\//-}" - else - hostname="${APP/\//-}.$VHOST" - fi - fi +NONSSL_VHOSTS=`cat $VHOST_PATH` +if [[ ! -n "$NO_VHOST" ]]; then if [[ -e "$SSL/server.crt" ]] && [[ -e "$SSL/server.key" ]]; then SSL_INUSE="$SSL" SSL_DIRECTIVES=$(cat < Configuring SSL for $line..." + SSL_SERVER_NAME=$line + eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf" + done <<< "$SSL_VHOSTS" fi APP_NGINX_TEMPLATE="$DOKKU_ROOT/$APP/nginx.conf.template" @@ -60,10 +83,14 @@ EOF NGINX_CONF=$APP_NGINX_TEMPLATE fi + cat $VHOST_PATH | xargs -i \ + echo "-----> Configuring {}..." + NOSSL_SERVER_NAME=`echo $NONSSL_VHOSTS | tr '\n' ' '` + echo "-----> Creating $SCHEME nginx.conf" echo "upstream $APP { server 127.0.0.1:$PORT; }" > $DOKKU_ROOT/$APP/nginx.conf eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf" - echo "$SCHEME://$hostname" > "$DOKKU_ROOT/$APP/URL" + echo $NOSSL_SERVER_NAME > "$DOKKU_ROOT/$APP/URL" echo "-----> Running nginx-pre-reload" pluginhook nginx-pre-reload $APP $PORT diff --git a/plugins/nginx-vhosts/templates/nginx.conf b/plugins/nginx-vhosts/templates/nginx.conf index 9b73e6964..8ed426275 100644 --- a/plugins/nginx-vhosts/templates/nginx.conf +++ b/plugins/nginx-vhosts/templates/nginx.conf @@ -1,7 +1,7 @@ server { listen [::]:80; listen 80; - server_name $hostname; + server_name $NOSSL_SERVER_NAME; location / { proxy_pass http://$APP; proxy_http_version 1.1; diff --git a/plugins/nginx-vhosts/templates/nginx.ssl.conf b/plugins/nginx-vhosts/templates/nginx.ssl.conf index a77e39c89..e30a6a82d 100644 --- a/plugins/nginx-vhosts/templates/nginx.ssl.conf +++ b/plugins/nginx-vhosts/templates/nginx.ssl.conf @@ -1,14 +1,14 @@ server { listen [::]:80; listen 80; - server_name $hostname; + server_name $NOSSL_SERVER_NAME; return 301 https://\$host\$request_uri; } server { listen [::]:443 ssl spdy; listen 443 ssl spdy; - server_name $hostname; + server_name $SSL_SERVER_NAME; $SSL_DIRECTIVES keepalive_timeout 70;