Skip to content

Commit

Permalink
Merge pull request #10 from IlyaSemenov/master
Browse files Browse the repository at this point in the history
Support Dokku 0.5 proxy maps
  • Loading branch information
josegonzalez authored Aug 26, 2016
2 parents 007945d + 38c7f8a commit f9509d3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 61 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

dokku-redirect is a plugin for [dokku][dokku] that gives the ability to set simple redirects for an application.

This plugin only redirects one domain to another and does not handle complete URLs. If both domains are managed by dokku and are TLS enabled, then nginx configuration for https redirects will be handled automatically.
This plugin only redirects one domain to another and does not handle complete URLs. If source domain is managed by dokku and is TLS enabled, then nginx configuration for https redirects will be handled automatically.

## Installation

```sh
# dokku 0.3.26
$ sudo git clone https://github.com/dokku/dokku-redirect.git /var/lib/dokku/plugins/redirect
$ dokku plugins-install

# dokku 0.4+
# dokku 0.5+
$ dokku plugin:install https://github.com/dokku/dokku-redirect.git
```

Expand Down
88 changes: 50 additions & 38 deletions nginx-pre-reload
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x

PLUGIN_BASE_PATH="$PLUGIN_PATH"
if [[ -n $DOKKU_API_VERSION ]]; then
PLUGIN_BASE_PATH="$PLUGIN_ENABLED_PATH"
fi
source "$PLUGIN_BASE_PATH/common/functions"
source "$PLUGIN_BASE_PATH/certs/functions"
source "$PLUGIN_BASE_PATH/config/functions"

APP="$1"; APP_ROOT="$DOKKU_ROOT/$APP"
REDIRECT_FILE="$APP_ROOT/REDIRECTS"
[[ ! -s $REDIRECT_FILE ]] && exit 0

NGINX_CONF="$APP_ROOT/nginx.conf"
TEMPLATE="$(dirname "$0")/templates/redirect.conf"
TEMPLATE_TLS="$(dirname "$0")/templates/redirect.tls.conf"
# shellcheck disable=SC2034
NGINX_PORT=$(config_get "$APP" DOKKU_NGINX_PORT || true)
# shellcheck disable=SC2034
NGINX_SSL_PORT=$(config_get "$APP" DOKKU_NGINX_SSL_PORT || true)
while read line; do
[[ -z "$line" ]] && continue
DOMAIN=$(echo "$line" | cut -d: -f1)
DEST=${line//[^:]*:/}
dokku_log_info1 "Configuring redirect for $DOMAIN to $DEST..."
eval "cat <<< \"$(< "$TEMPLATE")\" >> $NGINX_CONF"
source "$PLUGIN_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/certs/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/proxy/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"

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

[[ "$(get_app_proxy_type "$APP")" == "nginx" ]] || exit 0

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)
local NGINX_VERSION="$(nginx -v 2>&1 | cut -d'/' -f 2)"
local SPDY_SUPPORTED="$(is_spdy_enabled "$NGINX_VERSION")"
if 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 echo "$DOMAIN" | egrep -q "^$SSL_HOSTNAME_REGEX$" && echo "$DEST" | egrep -q "^$SSL_HOSTNAME_REGEX$"; then
APP_SSL_PATH="$APP_ROOT/tls"
# shellcheck disable=SC2034
SSL_DIRECTIVES=$(cat <<EOF
ssl_certificate $APP_SSL_PATH/server.crt;
ssl_certificate_key $APP_SSL_PATH/server.key;
EOF
)
eval "cat <<< \"$(< "$TEMPLATE_TLS")\" >> $NGINX_CONF"
fi
local APP_SSL_PATH="$DOKKU_ROOT/$APP/tls"
fi
done <<< "$(< "$REDIRECT_FILE")"

local NGINX_CONF_PREPEND=$(mktemp)
# shellcheck disable=SC2064
trap "rm -f \"$NGINX_CONF_PREPEND\"" EXIT

while read line; do
[[ -z "$line" ]] && continue
local DOMAIN=$(echo "$line" | cut -d: -f1)
local DEST_DOMAIN=${line//[^:]*:/}
dokku_log_info1 "Configuring redirect for $DOMAIN to $DEST..."
local SIGIL_PARAMS=(-f $NGINX_TEMPLATE APP="$APP" DOKKU_ROOT="$DOKKU_ROOT"
SPDY_SUPPORTED="$SPDY_SUPPORTED"
APP_SSL_PATH="$APP_SSL_PATH"
DOMAIN="$DOMAIN" DEST_DOMAIN="$DEST_DOMAIN"
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 "$@"
26 changes: 26 additions & 0 deletions templates/nginx.conf.sigil
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{ range $port_map := .PROXY_PORT_MAP | split " " }}
{{ $port_map_list := $port_map | split ":" }}
{{ $scheme := index $port_map_list 0 }}
{{ $listen_port := index $port_map_list 1 }}

{{ if eq $scheme "http" }}
server {
listen [::]:{{ $listen_port }};
listen {{ $listen_port }};
server_name {{ $.DOMAIN }};
access_log off;
return 301 $scheme://{{ $.DEST_DOMAIN }}$request_uri;
}
{{ else if eq $scheme "https"}}
server {
listen [::]:{{ $listen_port }} ssl {{ if eq $.SPDY_SUPPORTED "true" }}spdy{{ else }}http2{{ end }};
listen {{ $listen_port }} ssl {{ if eq $.SPDY_SUPPORTED "true" }}spdy{{ else }}http2{{ end }};
server_name {{ $.DOMAIN }};
access_log off;

ssl_certificate {{ $.APP_SSL_PATH }}/server.crt;
ssl_certificate_key {{ $.APP_SSL_PATH }}/server.key;

return 301 $scheme://{{ $.DEST_DOMAIN }}$request_uri;
}
{{ end }}{{ end }}
6 changes: 0 additions & 6 deletions templates/redirect.conf

This file was deleted.

11 changes: 0 additions & 11 deletions templates/redirect.tls.conf

This file was deleted.

0 comments on commit f9509d3

Please sign in to comment.