nginx-vhosts rewrite
This commit is contained in:
1 parent
4404e145c0
commit
9842d7498b
65 files changed
+1779
-1123
No files matched your search
@@ -1,13 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
|
||||
|
||||
APP="$1"
|
||||
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "false" ]]; then
|
||||
echo true # bind to external ip. VHOST is disabled
|
||||
elif [[ "$(is_global_vhost_enabled $APP)" == "false" ]] && [[ ! -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
|
||||
echo true # bind to external ip. no global vhost or global vhost is an ip
|
||||
else
|
||||
echo false
|
||||
fi
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
[[ " nginx:build-config nginx:access-logs nginx:error-logs nginx:disable nginx:enable help nginx:help " == *" $1 "* ]] || exit $DOKKU_NOT_IMPLEMENTED_EXIT
|
||||
[[ " nginx:build-config nginx:access-logs nginx:error-logs help nginx:help " == *" $1 "* ]] || exit $DOKKU_NOT_IMPLEMENTED_EXIT
|
||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
|
||||
source "$PLUGIN_AVAILABLE_PATH/config/functions"
|
||||
@@ -7,58 +7,13 @@ 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"
|
||||
|
||||
nginx_build_config $APP $DOKKU_APP_LISTEN_PORT $DOKKU_APP_LISTEN_IP
|
||||
|
||||
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
||||
nginx_build_config $2
|
||||
;;
|
||||
|
||||
nginx:access-logs|nginx:error-logs)
|
||||
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
||||
verify_app_name "$2"
|
||||
APP="$2"
|
||||
|
||||
NGINX_LOGS_TYPE=${1#nginx:}
|
||||
NGINX_LOGS_TYPE=${NGINX_LOGS_TYPE%-logs}
|
||||
NGINX_LOGS_PATH="/var/log/nginx/$APP-$NGINX_LOGS_TYPE.log"
|
||||
|
||||
if [[ $3 == "-t" ]]; then
|
||||
NGINX_LOGS_ARGS="-F"
|
||||
else
|
||||
NGINX_LOGS_ARGS="-n 20"
|
||||
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"
|
||||
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "true" ]]; then
|
||||
config_set --no-restart $APP DOKKU_NO_NGINX=1
|
||||
disable_app_vhost $APP
|
||||
elif [[ "$(is_app_nginx_enabled $APP)" == "true" ]]; then
|
||||
config_set --no-restart $APP DOKKU_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"
|
||||
|
||||
if [[ "$(is_app_nginx_enabled $APP)" == "false" ]]; then
|
||||
config_unset --no-restart $APP DOKKU_NO_NGINX NO_VHOST
|
||||
unset DOKKU_NO_NGINX NO_VHOST
|
||||
nginx_build_config $APP
|
||||
else
|
||||
dokku_log_info1 "nginx is already enabled for app ($APP)"
|
||||
fi
|
||||
nginx_logs "$@"
|
||||
;;
|
||||
|
||||
help | nginx:help)
|
||||
@@ -66,8 +21,6 @@ case "$1" in
|
||||
nginx:build-config <app>, (Re)builds nginx config for given app
|
||||
nginx:access-logs <app> [-t], Show the nginx access logs for an application (-t follows)
|
||||
nginx:error-logs <app> [-t], Show the nginx error logs for an application (-t follows)
|
||||
nginx:disable <app>, Disable nginx for an application (forces container binding to external interface)
|
||||
nginx:enable <app>, Enable nginx for an application
|
||||
EOF
|
||||
;;
|
||||
|
||||
|
||||
+145
-186
@@ -4,72 +4,13 @@ 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"
|
||||
|
||||
is_app_nginx_enabled() {
|
||||
local APP="$1"
|
||||
verify_app_name "$APP"
|
||||
|
||||
local DOKKU_NO_NGINX=$(config_get $APP DOKKU_NO_NGINX)
|
||||
if [[ -z "$DOKKU_NO_NGINX" ]]; then
|
||||
echo true
|
||||
else
|
||||
echo false
|
||||
fi
|
||||
}
|
||||
|
||||
is_global_vhost_enabled() {
|
||||
local GLOBAL_VHOST_FILE="$DOKKU_ROOT/VHOST"
|
||||
local GLOBAL_VHOST_ENABLED=true
|
||||
[[ -f "$GLOBAL_VHOST_FILE" ]] && local GLOBAL_VHOST=$(< "$GLOBAL_VHOST_FILE")
|
||||
|
||||
# Ensure the ip address continues to the end of the line
|
||||
# Fixes using a wildcard dns service such as xip.io which allows for *.<ip address>.xip.io
|
||||
local RE_IPV4="$(get_ipv4_regex)"
|
||||
local RE_IPV6="$(get_ipv6_regex)"
|
||||
|
||||
if [[ -z "$GLOBAL_VHOST" ]] || [[ "$GLOBAL_VHOST" =~ $RE_IPV4 ]] || [[ "$GLOBAL_VHOST" =~ $RE_IPV6 ]]; then
|
||||
local GLOBAL_VHOST_ENABLED=false
|
||||
fi
|
||||
echo $GLOBAL_VHOST_ENABLED
|
||||
}
|
||||
|
||||
is_app_vhost_enabled() {
|
||||
local APP=$1; local APP_VHOST_FILE="$DOKKU_ROOT/$APP/VHOST"
|
||||
verify_app_name $APP
|
||||
|
||||
local NO_VHOST=$(config_get $APP NO_VHOST)
|
||||
local APP_VHOST_ENABLED=true
|
||||
|
||||
if [[ "$NO_VHOST" == "1" ]]; then
|
||||
local APP_VHOST_ENABLED=false
|
||||
elif [[ -f "$DOKKU_ROOT/$APP/nginx.conf" ]] && [[ ! -f "$APP_VHOST_FILE" ]] && [[ "$NO_VHOST" != "0" ]]; then
|
||||
local APP_VHOST_ENABLED=false
|
||||
fi
|
||||
|
||||
echo $APP_VHOST_ENABLED
|
||||
}
|
||||
|
||||
disable_app_vhost() {
|
||||
local APP=$1; local APP_VHOST_FILE="$DOKKU_ROOT/$APP/VHOST"
|
||||
verify_app_name $APP
|
||||
|
||||
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
|
||||
|
||||
[[ "$2" == "--no-restart" ]] && local CONFIG_SET_ARGS=$2
|
||||
config_set $CONFIG_SET_ARGS $APP NO_VHOST=1
|
||||
}
|
||||
source "$PLUGIN_AVAILABLE_PATH/ps/functions"
|
||||
|
||||
validate_nginx() {
|
||||
local desc="validate entire nginx config"
|
||||
set +e
|
||||
sudo /usr/sbin/nginx -t > /dev/null 2>&1
|
||||
exit_code=$?
|
||||
local exit_code=$?
|
||||
set -e
|
||||
if [[ "$exit_code" -ne "0" ]]; then
|
||||
sudo /usr/sbin/nginx -t
|
||||
@@ -78,6 +19,7 @@ validate_nginx() {
|
||||
}
|
||||
|
||||
restart_nginx() {
|
||||
local desc="restart nginx for given distros"
|
||||
case "$DOKKU_DISTRO" in
|
||||
debian)
|
||||
sudo /usr/sbin/invoke-rc.d nginx reload > /dev/null
|
||||
@@ -93,148 +35,166 @@ restart_nginx() {
|
||||
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"
|
||||
APP_NGINX_SSL_TEMPLATE="$DOKKU_ROOT/$APP/nginx.ssl.conf.template"
|
||||
nginx_logs() {
|
||||
local desc="display app nginx logs"
|
||||
local APP="$2"; verify_app_name "$APP"
|
||||
local NGINX_LOGS_TYPE=${1#nginx:}
|
||||
local NGINX_LOGS_TYPE=${NGINX_LOGS_TYPE%-logs}
|
||||
local NGINX_LOGS_PATH="/var/log/nginx/$APP-$NGINX_LOGS_TYPE.log"
|
||||
|
||||
eval "$(config_export global)"
|
||||
eval "$(config_export app $APP)"
|
||||
if [[ $3 == "-t" ]]; then
|
||||
NGINX_LOGS_ARGS="-F"
|
||||
else
|
||||
NGINX_LOGS_ARGS="-n 20"
|
||||
fi
|
||||
|
||||
if [[ ! -n "$DOKKU_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="${DOKKU_APP_IP_FILE//IP/PORT}"
|
||||
DOKKU_APP_LISTENER_IP=$(< $DOKKU_APP_IP_FILE)
|
||||
DOKKU_APP_LISTENER_PORT=$(< $DOKKU_APP_PORT_FILE)
|
||||
tail "$NGINX_LOGS_ARGS" "$NGINX_LOGS_PATH"
|
||||
}
|
||||
|
||||
DOKKU_APP_LISTENERS+=" "
|
||||
DOKKU_APP_LISTENERS+="$DOKKU_APP_LISTENER_IP:$DOKKU_APP_LISTENER_PORT"
|
||||
DOKKU_APP_LISTENERS+=" "
|
||||
done
|
||||
shopt -u nullglob
|
||||
fi
|
||||
configure_nginx_ports() {
|
||||
local desc="configure nginx listening ports"
|
||||
local APP=$1; verify_app_name "$APP"
|
||||
local RAW_TCP_PORTS="$(get_app_raw_tcp_ports $APP)"
|
||||
local DOKKU_NGINX_PORT=$(config_get $APP DOKKU_NGINX_PORT)
|
||||
local DOKKU_NGINX_SSL_PORT=$(config_get $APP DOKKU_NGINX_SSL_PORT)
|
||||
local IS_APP_VHOST_ENABLED="$(is_app_vhost_enabled $APP)"
|
||||
|
||||
DOKKU_APP_CIDS=($(get_app_container_ids $APP))
|
||||
for file in nginx.conf.template nginx.ssl.conf.template; do
|
||||
docker cp "${DOKKU_APP_CIDS[0]}:/app/${file}" "$DOKKU_ROOT/$APP/" 2> /dev/null || true
|
||||
done
|
||||
|
||||
[[ -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 the app has is not vhost enabled then, let's make sure we're cleaned up
|
||||
[[ "$(is_app_vhost_enabled $APP)" == "false" ]] && disable_app_vhost $APP --no-restart
|
||||
|
||||
if [[ -z "$DOKKU_NGINX_PORT" ]]; then
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "false" ]]; then
|
||||
if [[ -z "$DOKKU_NGINX_PORT" ]]; then
|
||||
if [[ -z "$RAW_TCP_PORTS" ]]; then
|
||||
if [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then
|
||||
dokku_log_info1 "no nginx port set. setting to random open high port"
|
||||
local NGINX_PORT=$(get_available_port)
|
||||
else
|
||||
local NGINX_PORT=80
|
||||
fi
|
||||
config_set --no-restart $APP DOKKU_NGINX_PORT=${NGINX_PORT}
|
||||
fi
|
||||
if [[ -n "$NGINX_PORT" ]]; then
|
||||
config_set --no-restart $APP DOKKU_NGINX_PORT="${NGINX_PORT}"
|
||||
fi
|
||||
fi
|
||||
|
||||
if (is_ssl_enabled $APP) && [[ -z "$DOKKU_NGINX_SSL_PORT" ]] && [[ -z "$RAW_TCP_PORTS" ]]; then
|
||||
if [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then
|
||||
dokku_log_info1 "no nginx ssl port set. setting to random open high port"
|
||||
local NGINX_SSL_PORT=$(get_available_port)
|
||||
else
|
||||
NGINX_PORT=$DOKKU_NGINX_PORT
|
||||
local NGINX_SSL_PORT=443
|
||||
fi
|
||||
config_set --no-restart $APP DOKKU_NGINX_SSL_PORT=${NGINX_SSL_PORT}
|
||||
fi
|
||||
}
|
||||
|
||||
NONSSL_VHOSTS=$(get_app_domains $APP)
|
||||
if [[ -n "$(is_ssl_enabled $APP)" ]]; then
|
||||
SSL_HOSTNAME=$(get_ssl_hostnames $APP)
|
||||
SSL_INUSE=true
|
||||
validate_ssl_domains() {
|
||||
local desc="check configured domains against SSL cert contents and show warning if mismatched"
|
||||
local APP=$1; verify_app_name "$APP"
|
||||
local SSL_HOSTNAME=$(get_ssl_hostnames $APP)
|
||||
local SSL_HOSTNAME_REGEX=$(echo "$SSL_HOSTNAME" | xargs | sed 's|\.|\\.|g' | sed 's/\*/\[^\.\]\*/g' | sed 's/ /|/g')
|
||||
local domain
|
||||
|
||||
[[ -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 ! (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 <<EOF
|
||||
ssl_certificate $APP_SSL_PATH/server.crt;
|
||||
ssl_certificate_key $APP_SSL_PATH/server.key;
|
||||
EOF
|
||||
)
|
||||
elif [[ "$(is_ssl_enabled $APP)" == "global" ]]; then
|
||||
SSL_DIRECTIVES=""
|
||||
fi
|
||||
fi
|
||||
get_custom_nginx_template() {
|
||||
local APP="$1"; verify_app_name "$APP"
|
||||
local DESTINATION="$2"
|
||||
local IMAGE_TAG="$(get_running_image_tag $APP)"
|
||||
local IMAGE=$(get_app_image_name $APP $IMAGE_TAG)
|
||||
local NGINX_TEMPLATE_NAME="nginx.conf.sigil"
|
||||
|
||||
if [[ -n "$SSL_INUSE" ]]; then
|
||||
SCHEME="https"
|
||||
copy_from_image "$IMAGE" "$NGINX_TEMPLATE_NAME" "$DESTINATION" 2>/dev/null || true
|
||||
}
|
||||
|
||||
[[ -f "$APP_NGINX_SSL_TEMPLATE" ]] && NGINX_SSL_TEMPLATE="$APP_NGINX_SSL_TEMPLATE" && dokku_log_info1 'Overriding default nginx.ssl.conf with detected nginx.ssl.conf.template'
|
||||
[[ -z "$NGINX_SSL_TEMPLATE" ]] && NGINX_SSL_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-vhosts/templates/nginx.ssl.conf.template"
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "true" ]]; then
|
||||
SSL_VHOSTS=$(egrep "^${SSL_HOSTNAME_REGEX}$" $VHOST_PATH || true)
|
||||
else
|
||||
SSL_VHOSTS=$(< $DOKKU_ROOT/HOSTNAME)
|
||||
fi
|
||||
NONSSL_VHOSTS=$(egrep -v "^${SSL_HOSTNAME_REGEX}$" $VHOST_PATH 2> /dev/null || true)
|
||||
nginx_build_config() {
|
||||
local desc="build nginx config to proxy app containers using sigil"
|
||||
local APP="$1"; verify_app_name "$APP"
|
||||
local DOKKU_APP_LISTEN_PORT="$2"; local DOKKU_APP_LISTEN_IP="$3"
|
||||
local VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"; local URLS_PATH="$DOKKU_ROOT/$APP/URLS"
|
||||
local NGINX_TEMPLATE_NAME="nginx.conf.sigil"
|
||||
local DEFAULT_NGINX_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-vhosts/templates/$NGINX_TEMPLATE_NAME"
|
||||
local NGINX_TEMPLATE="$DEFAULT_NGINX_TEMPLATE"; local SCHEME=http
|
||||
local NGINX_TEMPLATE_SOURCE="built-in"; local APP_SSL_PATH="$DOKKU_ROOT/$APP/tls"
|
||||
local RAW_TCP_PORTS="$(get_app_raw_tcp_ports $APP)"
|
||||
|
||||
if [[ -z "$DOKKU_NGINX_SSL_PORT" ]]; then
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "false" ]]; then
|
||||
dokku_log_info1 "no nginx ssl port set. setting to random open high port"
|
||||
local NGINX_SSL_PORT=$(get_available_port)
|
||||
local DOKKU_DISABLE_PROXY=$(config_get $APP DOKKU_DISABLE_PROXY)
|
||||
local IS_APP_VHOST_ENABLED=$(is_app_vhost_enabled $APP)
|
||||
|
||||
if [[ -z "$DOKKU_DISABLE_PROXY" ]]; then
|
||||
if [[ -z "$DOKKU_APP_LISTEN_PORT" ]] && [[ -z "$DOKKU_APP_LISTEN_IP" ]]; then
|
||||
shopt -s nullglob
|
||||
local DOKKU_APP_IP_FILE
|
||||
for DOKKU_APP_IP_FILE in $DOKKU_ROOT/$APP/IP.web.*; do
|
||||
local DOKKU_APP_PORT_FILE="${DOKKU_APP_IP_FILE//IP/PORT}"
|
||||
local DOKKU_APP_LISTENER_IP=$(< $DOKKU_APP_IP_FILE)
|
||||
local DOKKU_APP_LISTENER_PORT=$(< $DOKKU_APP_PORT_FILE)
|
||||
|
||||
if [[ -z "$RAW_TCP_PORTS" ]]; then
|
||||
local DOKKU_APP_LISTENERS+=" $DOKKU_APP_LISTENER_IP:$DOKKU_APP_LISTENER_PORT "
|
||||
else
|
||||
NGINX_SSL_PORT=443
|
||||
local DOKKU_APP_LISTENERS+=" $DOKKU_APP_LISTENER_IP "
|
||||
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 -r line; do
|
||||
[[ -z "$line" ]] && continue
|
||||
dokku_log_info1 "Configuring SSL for $line...(using $NGINX_SSL_TEMPLATE)"
|
||||
SSL_SERVER_NAME=$line
|
||||
NOSSL_SERVER_NAME=$line
|
||||
eval "cat <<< \"$(< $NGINX_SSL_TEMPLATE)\" >> $NGINX_CONF"
|
||||
done <<< "$SSL_VHOSTS"
|
||||
fi
|
||||
|
||||
if [[ -n "$DOKKU_SSL_TERMINATED" ]] && [[ -z "$NGINX_CUSTOM_TEMPLATE" ]]; then
|
||||
NGINX_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-vhosts/templates/nginx.conf.ssl_terminated.template"
|
||||
elif [[ -z "$NGINX_CUSTOM_TEMPLATE" ]]; then
|
||||
NGINX_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-vhosts/templates/nginx.conf.template"
|
||||
fi
|
||||
|
||||
if [[ -n "$NONSSL_VHOSTS" ]]; then
|
||||
NOSSL_SERVER_NAME=$(echo $NONSSL_VHOSTS | tr '\n' ' ')
|
||||
xargs -i echo "-----> Configuring {}...(using $NGINX_TEMPLATE)" <<< "$NONSSL_VHOSTS"
|
||||
eval "cat <<< \"$(< $NGINX_TEMPLATE)\" >> $NGINX_CONF"
|
||||
fi
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "false" ]] || ([[ -z "$NONSSL_VHOSTS" ]] && [[ -z "$SSL_VHOSTS" ]]); then
|
||||
eval "cat <<< \"$(< $NGINX_TEMPLATE)\" >> $NGINX_CONF"
|
||||
sed --in-place -n -e '/^.*server_name.*$/!p' $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
|
||||
local DOKKU_APP_LISTENERS=$(echo $DOKKU_APP_LISTENERS | xargs)
|
||||
done
|
||||
echo "}" >> $NGINX_CONF
|
||||
shopt -u nullglob
|
||||
elif [[ -n "$DOKKU_APP_LISTEN_PORT" ]] && [[ -n "$DOKKU_APP_LISTEN_IP" ]]; then
|
||||
local PASSED_LISTEN_IP_PORT=true
|
||||
fi
|
||||
|
||||
local TMPDIR=$(mktemp -d /tmp/nginx_template.XXXXX)
|
||||
local NGINX_CONF=$(mktemp -p ${TMPDIR} -t "nginx.conf.XXXXXX")
|
||||
local CUSTOM_NGINX_TEMPLATE="$TMPDIR/$NGINX_TEMPLATE_NAME"
|
||||
trap 'rm -rf "$NGINX_CONF $TMPDIR" > /dev/null' RETURN
|
||||
|
||||
get_custom_nginx_template "$APP" "$CUSTOM_NGINX_TEMPLATE"
|
||||
if [[ -f "$CUSTOM_NGINX_TEMPLATE" ]]; then
|
||||
dokku_log_info1 'Overriding default nginx.conf with detected nginx.conf.sigil'
|
||||
local NGINX_TEMPLATE="$CUSTOM_NGINX_TEMPLATE"
|
||||
local NGINX_TEMPLATE_SOURCE="app-supplied"
|
||||
fi
|
||||
|
||||
# setup nginx listen ports
|
||||
configure_nginx_ports "$APP"
|
||||
local NGINX_PORT=$(config_get $APP DOKKU_NGINX_PORT)
|
||||
local NGINX_SSL_PORT=$(config_get $APP DOKKU_NGINX_SSL_PORT)
|
||||
|
||||
local NONSSL_VHOSTS=$(get_app_domains "$APP")
|
||||
local NOSSL_SERVER_NAME=$(echo $NONSSL_VHOSTS | xargs)
|
||||
if is_ssl_enabled "$APP"; then
|
||||
local SSL_INUSE=true; local SCHEME=https
|
||||
validate_ssl_domains "$APP"
|
||||
local SSL_HOSTNAME=$(get_ssl_hostnames $APP)
|
||||
local SSL_HOSTNAME_REGEX=$(echo "$SSL_HOSTNAME" | xargs | sed 's|\.|\\.|g' | sed 's/\*/\[^\.\]\*/g' | sed 's/ /|/g')
|
||||
|
||||
if [[ "$IS_APP_VHOST_ENABLED" == "true" ]]; then
|
||||
local SSL_VHOSTS=$(egrep "^${SSL_HOSTNAME_REGEX}$" $VHOST_PATH || true)
|
||||
else
|
||||
local SSL_VHOSTS=$(< $DOKKU_ROOT/HOSTNAME)
|
||||
fi
|
||||
local SSL_SERVER_NAME=$(echo $SSL_VHOSTS | xargs)
|
||||
fi
|
||||
local SIGIL_PARAMS=(-f $NGINX_TEMPLATE APP="$APP" DOKKU_ROOT="$DOKKU_ROOT"
|
||||
NOSSL_SERVER_NAME="$NOSSL_SERVER_NAME"
|
||||
DOKKU_APP_LISTENERS="$DOKKU_APP_LISTENERS"
|
||||
PASSED_LISTEN_IP_PORT="$PASSED_LISTEN_IP_PORT"
|
||||
DOKKU_APP_LISTEN_PORT="$DOKKU_APP_LISTEN_PORT" DOKKU_APP_LISTEN_IP="$DOKKU_APP_LISTEN_IP"
|
||||
APP_SSL_PATH="$APP_SSL_PATH" SSL_INUSE="$SSL_INUSE" SSL_SERVER_NAME="$SSL_SERVER_NAME"
|
||||
NGINX_PORT="$NGINX_PORT" NGINX_SSL_PORT="$NGINX_SSL_PORT" RAW_TCP_PORTS="$RAW_TCP_PORTS")
|
||||
|
||||
# execute sigil template processing
|
||||
xargs -i echo "-----> Configuring {}...(using $NGINX_TEMPLATE_SOURCE template)" <<< "$(echo ${SSL_VHOSTS} ${NONSSL_VHOSTS} | tr ' ' '\n' | sort -u)"
|
||||
# echo "sigil ${SIGIL_PARAMS[@]}"
|
||||
sigil "${SIGIL_PARAMS[@]}" > $NGINX_CONF
|
||||
|
||||
if is_deployed "$APP"; then
|
||||
dokku_log_info1 "Creating $SCHEME nginx.conf"
|
||||
mv $NGINX_CONF "$DOKKU_ROOT/$APP/nginx.conf"
|
||||
@@ -251,13 +211,12 @@ EOF
|
||||
validate_nginx && restart_nginx
|
||||
fi
|
||||
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "true" ]]; then
|
||||
if ([[ -n "$NONSSL_VHOSTS" ]] || [[ -n "$SSL_VHOSTS" ]]) && [[ "$IS_APP_VHOST_ENABLED" == "true" ]]; 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
|
||||
xargs -i echo "$SCHEME://{}" <<< "$(echo ${SSL_VHOSTS} ${NONSSL_VHOSTS} | tr ' ' '\n' | sort -u)" >> $URLS_PATH
|
||||
fi
|
||||
else
|
||||
# note because this clause is long. if $DOKKU_NO_NGINX is set:
|
||||
# note because this clause is long. if $DOKKU_DISABLE_PROXY is set:
|
||||
dokku_log_info1 "nginx support is disabled for app ($APP)."
|
||||
if [[ -f "$DOKKU_ROOT/$APP/nginx.conf" ]]; then
|
||||
dokku_log_info1 "deleting nginx.conf"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[plugin]
|
||||
description = "dokku core nginx-vhosts plugin"
|
||||
version = "0.4.0"
|
||||
version = "0.5.0"
|
||||
[plugin.config]
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
source "$PLUGIN_AVAILABLE_PATH/proxy/functions"
|
||||
|
||||
case "$DOKKU_DISTRO" in
|
||||
debian)
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
|
||||
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
|
||||
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
|
||||
source "$PLUGIN_AVAILABLE_PATH/proxy/functions"
|
||||
|
||||
APP="$1"
|
||||
if [[ -f "$DOKKU_ROOT/$APP/IP.web.1" ]] && [[ -f "$DOKKU_ROOT/$APP/PORT.web.1" ]]; then
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "false" ]]; then
|
||||
dokku_log_info1 "VHOST support disabled. Skipping domains setup"
|
||||
elif [[ ! -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
|
||||
dokku domains:setup $APP
|
||||
nginx_post_deploy() {
|
||||
local APP="$1"
|
||||
if [[ -f "$DOKKU_ROOT/$APP/IP.web.1" ]] && [[ -f "$DOKKU_ROOT/$APP/PORT.web.1" ]]; then
|
||||
if [[ "$(is_app_vhost_enabled $APP)" == "false" ]]; then
|
||||
dokku_log_info1 "VHOST support disabled. Skipping domains setup"
|
||||
elif [[ ! -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
|
||||
domains_setup $APP
|
||||
fi
|
||||
|
||||
nginx_build_config $APP
|
||||
fi
|
||||
}
|
||||
|
||||
nginx_build_config $APP
|
||||
if [[ "$(get_app_proxy_type $1)" == "nginx" ]]; then
|
||||
nginx_post_deploy "$@"
|
||||
fi
|
||||
@@ -2,7 +2,8 @@
|
||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
|
||||
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
|
||||
source "$PLUGIN_AVAILABLE_PATH/proxy/functions"
|
||||
|
||||
APP="$1"
|
||||
|
||||
nginx_build_config $APP
|
||||
if [[ "$(get_app_proxy_type $1)" == "nginx" ]]; then
|
||||
nginx_build_config $1
|
||||
fi
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/proxy/functions"
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/ps/functions"
|
||||
|
||||
nginx_disable() {
|
||||
local desc="disable nginx proxy"
|
||||
local APP="$1"; verify_app_name "$APP"
|
||||
|
||||
if [[ "$(get_app_proxy_type $APP)" == "nginx" ]]; then
|
||||
disable_app_vhost $APP --no-restart
|
||||
ps_restart $APP
|
||||
fi
|
||||
}
|
||||
|
||||
nginx_disable "$@"
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/proxy/functions"
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/ps/functions"
|
||||
|
||||
nginx_enable() {
|
||||
local desc="enable nginx proxy"
|
||||
local APP="$1"; verify_app_name "$APP"
|
||||
|
||||
if [[ "$(get_app_proxy_type $APP)" == "nginx" ]]; then
|
||||
enable_app_vhost $APP --no-restart
|
||||
ps_restart $APP
|
||||
fi
|
||||
}
|
||||
|
||||
nginx_enable "$@"
|
||||
@@ -0,0 +1,20 @@
|
||||
location / {
|
||||
|
||||
gzip on;
|
||||
gzip_min_length 1100;
|
||||
gzip_buffers 4 32k;
|
||||
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
|
||||
gzip_vary on;
|
||||
gzip_comp_level 6;
|
||||
|
||||
proxy_pass http://{{ .APP }};
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_set_header X-Forwarded-Port $server_port;
|
||||
proxy_set_header X-Request-Start $msec;
|
||||
}
|
||||
include {{ .DOKKU_ROOT }}/{{ .APP }}/nginx.conf.d/*.conf;
|
||||
@@ -0,0 +1,2 @@
|
||||
access_log /var/log/nginx/{{ .APP }}-access.log;
|
||||
error_log /var/log/nginx/{{ .APP }}-error.log;
|
||||
@@ -0,0 +1,10 @@
|
||||
{{ if .RAW_TCP_PORTS }}
|
||||
{{ include "tcp.server.config" . }}
|
||||
{{ else if .SSL_INUSE }}
|
||||
{{ include "nossl.server.config" . }}
|
||||
{{ include "ssl.server.config" . }}
|
||||
{{ include "upstream.config" . }}
|
||||
{{ else }}
|
||||
{{ include "nossl.server.config" . }}
|
||||
{{ include "upstream.config" . }}
|
||||
{{ end }}
|
||||
@@ -1,19 +0,0 @@
|
||||
# Nginx configuration when running behind a load balancer that terminates SSL
|
||||
# connections (e.g. AWS ELB)
|
||||
server {
|
||||
listen [::]:$NGINX_PORT;
|
||||
listen $NGINX_PORT;
|
||||
server_name $NOSSL_SERVER_NAME;
|
||||
location / {
|
||||
proxy_pass http://$APP;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host \$http_host;
|
||||
proxy_set_header X-Forwarded-Proto \$http_x_forwarded_proto;
|
||||
proxy_set_header X-Forwarded-For \$http_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Port \$http_x_forwarded_port;
|
||||
proxy_set_header X-Request-Start \$msec;
|
||||
}
|
||||
include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
server {
|
||||
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;
|
||||
|
||||
location / {
|
||||
|
||||
gzip on;
|
||||
gzip_min_length 1100;
|
||||
gzip_buffers 4 32k;
|
||||
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
|
||||
gzip_vary on;
|
||||
gzip_comp_level 6;
|
||||
|
||||
proxy_pass http://$APP;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host \$http_host;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
proxy_set_header X-Forwarded-For \$remote_addr;
|
||||
proxy_set_header X-Forwarded-Port \$server_port;
|
||||
proxy_set_header X-Request-Start \$msec;
|
||||
}
|
||||
include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf;
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
server {
|
||||
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:$NGINX_SSL_PORT\$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
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 $NGINX_SSL_PORT:npn-spdy/2;
|
||||
location / {
|
||||
|
||||
gzip on;
|
||||
gzip_min_length 1100;
|
||||
gzip_buffers 4 32k;
|
||||
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
|
||||
gzip_vary on;
|
||||
gzip_comp_level 6;
|
||||
|
||||
proxy_pass http://$APP;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host \$http_host;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
proxy_set_header X-Forwarded-For \$remote_addr;
|
||||
proxy_set_header X-Forwarded-Port \$server_port;
|
||||
proxy_set_header X-Request-Start \$msec;
|
||||
}
|
||||
include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
server {
|
||||
listen [::]:{{ .NGINX_PORT }};
|
||||
listen {{ .NGINX_PORT }};
|
||||
{{ if .NOSSL_SERVER_NAME }}server_name {{ .NOSSL_SERVER_NAME }}; {{ end }}
|
||||
{{ include "log.config" . }}
|
||||
{{ if .SSL_INUSE }}
|
||||
return 301 https://$host:{{ .NGINX_SSL_PORT }}$request_uri;
|
||||
{{ else }}
|
||||
{{ include "location.config" . }}
|
||||
{{ end }}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
server {
|
||||
listen [::]:{{ .NGINX_SSL_PORT }} ssl spdy;
|
||||
listen {{ .NGINX_SSL_PORT }} ssl spdy;
|
||||
{{ if .SSL_SERVER_NAME }}server_name {{ .SSL_SERVER_NAME }}; {{ end }}
|
||||
{{ include "log.config" . }}
|
||||
ssl_certificate {{ .APP_SSL_PATH }}/server.crt;
|
||||
ssl_certificate_key {{ .APP_SSL_PATH }}/server.key;
|
||||
|
||||
keepalive_timeout 70;
|
||||
add_header Alternate-Protocol {{ .NGINX_SSL_PORT }}:npn-spdy/2;
|
||||
{{ include "location.config" . }}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
{{ range .RAW_TCP_PORTS | split " " }}
|
||||
{{ $port := . }}
|
||||
server {
|
||||
listen [::]:{{ $port }};
|
||||
listen {{ $port }};
|
||||
{{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }}
|
||||
access_log /var/log/nginx/{{ $.APP }}-access.log;
|
||||
error_log /var/log/nginx/{{ $.APP }}-error.log;
|
||||
|
||||
location / {
|
||||
|
||||
gzip on;
|
||||
gzip_min_length 1100;
|
||||
gzip_buffers 4 32k;
|
||||
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
|
||||
gzip_vary on;
|
||||
gzip_comp_level 6;
|
||||
|
||||
proxy_pass http://{{ $.APP }}-{{ $port }};
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_set_header X-Forwarded-Port $server_port;
|
||||
proxy_set_header X-Request-Start $msec;
|
||||
}
|
||||
}
|
||||
include {{ $.DOKKU_ROOT }}/{{ $.APP }}/nginx.conf.d/*.conf;
|
||||
{{ if $.DOKKU_APP_LISTENERS }}
|
||||
upstream {{ $.APP }}-{{ $port }} {
|
||||
{{ range $.DOKKU_APP_LISTENERS | split " " }} server {{ . }}:{{ $port }};{{ end }}
|
||||
}
|
||||
{{ end }}{{ end }}
|
||||
@@ -0,0 +1,9 @@
|
||||
{{ if .DOKKU_APP_LISTENERS }}
|
||||
upstream {{ .APP }} {
|
||||
{{ range .DOKKU_APP_LISTENERS | split " " }} server {{ . }};
|
||||
{{ end }}}
|
||||
{{ else if .PASSED_LISTEN_IP_PORT }}
|
||||
upstream {{ .APP }} {
|
||||
server {{ .DOKKU_APP_LISTEN_IP }}:{{ .DOKKU_APP_LISTEN_PORT }};
|
||||
}
|
||||
{{ end }}
|
||||
Reference in new issue
Block a user