From 52777836ec906d70f3064776356458db229980ae Mon Sep 17 00:00:00 2001 From: Maik Marschner Date: Thu, 14 Mar 2024 10:56:04 +0100 Subject: [PATCH 1/3] Update nginx --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 2d31c20..66ede38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM nginx:1.15-alpine +FROM nginx:1.25-alpine COPY start.sh /usr/local/bin/ From 45425f0452846cd7fdca7931978402d30f4cae6f Mon Sep 17 00:00:00 2001 From: Maik Marschner Date: Thu, 14 Mar 2024 13:37:25 +0100 Subject: [PATCH 2/3] Do not always append a trailing slash. --- start.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/start.sh b/start.sh index 2afca76..f9dac11 100755 --- a/start.sh +++ b/start.sh @@ -11,11 +11,6 @@ else if ! [[ $REDIRECT_TARGET =~ ^https?:// ]]; then REDIRECT_TARGET="http://$REDIRECT_TARGET" fi - - # Add trailing slash - if [[ ${REDIRECT_TARGET:length-1:1} != "/" ]]; then - REDIRECT_TARGET="$REDIRECT_TARGET/" - fi fi # Default to 80 @@ -29,7 +24,7 @@ cat < /etc/nginx/conf.d/default.conf server { listen ${LISTEN}; - rewrite ^/(.*)\$ ${REDIRECT_TARGET}\$1 ${REDIRECT_TYPE}; + rewrite ^(.*)\$ ${REDIRECT_TARGET}\$1 ${REDIRECT_TYPE}; } EOF From 64f7cb93ca6acbee9c33a02cdf5c5c8544c72c53 Mon Sep 17 00:00:00 2001 From: Maik Marschner Date: Thu, 14 Mar 2024 13:37:52 +0100 Subject: [PATCH 3/3] Add option to redirect without retaining the path. --- README.md | 2 +- start.sh | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e217fd4..77115b4 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Possible redirect targets include domains (`mydomain.net`), paths (`mydomain.net **Example:** `$ docker run --rm -d -e REDIRECT_TARGET=mydomain.net -p 80:80 morbz/docker-web-redirect` ### Paths are retained ### -The URL path and GET parameters are retained. That means that a request to `http://myolddomain.net/index.php?page=2` will be redirected to `http://mydomain.net/index.php?page=2` when `REDIRECT_TARGET=mydomain.net` is set. +The URL path and GET parameters are retained by default. That means that a request to `http://myolddomain.net/index.php?page=2` will be redirected to `http://mydomain.net/index.php?page=2` when `REDIRECT_TARGET=mydomain.net` is set. If you do not want to retain the path and GET parameters, set the environment variable `RETAIN_PATH` to `false`. ### Permanent redirects ### Redirects are, by default, permanent (HTTP status code 301). That means browsers will cache the redirect and will go directly to the new site on further requests. Also search engines will recognize the new domain and change their URLs. This means this image is not suitable for temporary redirects e.g. for site maintenance. diff --git a/start.sh b/start.sh index f9dac11..7279e54 100755 --- a/start.sh +++ b/start.sh @@ -20,13 +20,24 @@ if [ ! -z "$PORT" ]; then LISTEN="$PORT" fi -cat < /etc/nginx/conf.d/default.conf -server { - listen ${LISTEN}; +: ${RETAIN_PATH:='true'} +if [ "$RETAIN_PATH" = "true" ]; then + cat < /etc/nginx/conf.d/default.conf + server { + listen ${LISTEN}; - rewrite ^(.*)\$ ${REDIRECT_TARGET}\$1 ${REDIRECT_TYPE}; -} + rewrite ^(.*)\$ ${REDIRECT_TARGET}\$1 ${REDIRECT_TYPE}; + } EOF +else + cat < /etc/nginx/conf.d/default.conf + server { + listen ${LISTEN}; + + rewrite ^(.*)\$ ${REDIRECT_TARGET} ${REDIRECT_TYPE}; + } +EOF +fi echo "Listening to $LISTEN, Redirecting HTTP requests to ${REDIRECT_TARGET}..."