-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
nginx-pre-reload
executable file
·68 lines (57 loc) · 2.31 KB
/
nginx-pre-reload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/certs/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
# shellcheck disable=SC2155
redirect_nginx_pre_load_trigger() {
# shellcheck disable=SC2034
declare desc="add nginx redirect servers"
# shellcheck disable=SC2034
local trigger="redirect_nginx_pre_load_trigger"
local APP="$1"
local APP_ROOT="$DOKKU_ROOT/$APP"
local REDIRECT_FILE="$APP_ROOT/REDIRECTS"
[[ ! -s "$REDIRECT_FILE" ]] && exit 0
if [[ "$(plugn trigger proxy-type "$APP")" != "nginx" ]]; then
return
fi
local NGINX_CONF="$APP_ROOT/nginx.conf"
local NGINX_TEMPLATE="$(dirname "$0")/templates/nginx.conf.sigil"
local PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP || true)
if [[ -z "$PROXY_PORT_MAP" ]]; then
PROXY_PORT_MAP="$(plugn trigger ports-get "$APP" | xargs)"
fi
PROXY_PORT_MAP="$(echo -e "${PROXY_PORT_MAP}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
local NGINX_LOCATION=$(get_nginx_location)
if [[ -z "$NGINX_LOCATION" ]]; then
exit 1
fi
local NGINX_VERSION="$($NGINX_LOCATION -v 2>&1 | cut -d'/' -f 2 || true)"
if is_ssl_enabled "$APP"; then
local APP_SSL_PATH="$DOKKU_ROOT/$APP/tls"
fi
local NGINX_CONF_PREPEND=$(mktemp)
# shellcheck disable=SC2064
trap "rm -f \"$NGINX_CONF_PREPEND\"" EXIT
while read -r line; do
[[ -z "$line" ]] && continue
local DOMAIN=$(echo "$line" | awk -F ':' '{ print $1 }')
local DEST_DOMAIN=$(echo "$line" | awk -F ':' '{ print $2 }')
local REDIR_CODE=$(echo "$line" | awk -F ':' '{ print $3 ? $3 : "301" }')
dokku_log_info1 "Configuring redirect for $DOMAIN to $DEST_DOMAIN via HTTP $REDIR_CODE..."
# shellcheck disable=SC2191
local SIGIL_PARAMS=(-f $NGINX_TEMPLATE APP="$APP" DOKKU_ROOT="$DOKKU_ROOT"
APP_SSL_PATH="$APP_SSL_PATH"
DOMAIN="$DOMAIN" DEST_DOMAIN="$DEST_DOMAIN" REDIR_CODE="$REDIR_CODE"
PROXY_PORT_MAP="$PROXY_PORT_MAP")
sigil "${SIGIL_PARAMS[@]}" | cat -s >>"$NGINX_CONF_PREPEND"
done <<<"$(<"$REDIRECT_FILE")"
if [[ -s "$NGINX_CONF_PREPEND" ]]; then
cat "$NGINX_CONF" >>"$NGINX_CONF_PREPEND"
cat "$NGINX_CONF_PREPEND" >"$NGINX_CONF"
fi
}
redirect_nginx_pre_load_trigger "$@"