├── .github └── workflows │ └── docker-publish.yml ├── Dockerfile ├── LICENSE ├── README.md └── start.sh /.github/workflows/docker-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | 6 | # GitHub recommends pinning actions to a commit SHA. 7 | # To get a newer version, you will need to update the SHA. 8 | # You can also reference a tag or branch, but the action may change without warning. 9 | 10 | name: Publish Docker image 11 | 12 | on: 13 | release: 14 | types: [published] 15 | 16 | jobs: 17 | push_to_registries: 18 | name: Push Docker image to multiple registries 19 | runs-on: ubuntu-latest 20 | permissions: 21 | packages: write 22 | contents: read 23 | steps: 24 | - name: Check out the repo 25 | uses: actions/checkout@v4 26 | 27 | - name: Log in to Docker Hub 28 | uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a 29 | with: 30 | username: ${{ secrets.DOCKER_USERNAME }} 31 | password: ${{ secrets.DOCKER_PASSWORD }} 32 | 33 | - name: Log in to the Container registry 34 | uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 35 | with: 36 | registry: ghcr.io 37 | username: ${{ github.actor }} 38 | password: ${{ secrets.GITHUB_TOKEN }} 39 | 40 | - name: Extract metadata (tags, labels) for Docker 41 | id: meta 42 | uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 43 | with: 44 | images: | 45 | morbz/docker-web-redirect 46 | ghcr.io/${{ github.repository }} 47 | 48 | - name: Build and push Docker images 49 | uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 50 | with: 51 | context: . 52 | push: true 53 | tags: ${{ steps.meta.outputs.tags }} 54 | labels: ${{ steps.meta.outputs.labels }} 55 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:1.25-alpine 2 | 3 | COPY start.sh /usr/local/bin/ 4 | 5 | RUN apk add --update bash \ 6 | && rm -rf /var/cache/apk/* \ 7 | && chmod +x /usr/local/bin/start.sh 8 | 9 | EXPOSE 80 10 | 11 | CMD ["start.sh"] 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Merten Peetz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker-Web-Redirect # 2 | 3 | ![Docker Build Status](https://img.shields.io/docker/build/morbz/docker-web-redirect.svg) ![Docker Pulls](https://img.shields.io/docker/pulls/morbz/docker-web-redirect.svg) ![Docker Stars](https://img.shields.io/docker/stars/morbz/docker-web-redirect.svg) 4 | 5 | This Docker container listens on port 80 and redirects all web traffic to the given target domain/URL. 6 | 7 | ## Features ## 8 | - Lightweight: Uses only ~2 MB RAM on Linux 9 | - Keeps the URL path and GET parameters 10 | - Permanent or temporary redirect 11 | 12 | ## Usage ## 13 | ### Docker run ### 14 | The target domain/URL is set by the `REDIRECT_TARGET` environment variable. 15 | Possible redirect targets include domains (`mydomain.net`), paths (`mydomain.net/my_page`) or specific protocols (`https://mydomain.net/my_page`). 16 | 17 | **Example:** `$ docker run --rm -d -e REDIRECT_TARGET=mydomain.net -p 80:80 morbz/docker-web-redirect` 18 | 19 | ### Paths are retained ### 20 | 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`. 21 | 22 | ### Permanent redirects ### 23 | 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. To make redirects temporary (HTTP status code 302), e.g. for site maintenance, set the environment variable `REDIRECT_TYPE` to `redirect`. 24 | 25 | ## Docker Compose ## 26 | This image can be combined with the [jwilder nginx-proxy](https://hub.docker.com/r/jwilder/nginx-proxy/). A sample docker-compose file that redirects `myolddomain.net` to `mydomain.net` could look like this: 27 | 28 | ```yaml 29 | version: '3' 30 | services: 31 | redirect: 32 | image: morbz/docker-web-redirect 33 | restart: always 34 | environment: 35 | - VIRTUAL_HOST=myolddomain.net 36 | - REDIRECT_TARGET=mydomain.net 37 | ``` 38 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ -z "$REDIRECT_TYPE" ]; then 3 | REDIRECT_TYPE="permanent" 4 | fi 5 | 6 | if [ -z "$REDIRECT_TARGET" ]; then 7 | echo "Redirect target variable not set (REDIRECT_TARGET)" 8 | exit 1 9 | else 10 | # Add http if not set 11 | if ! [[ $REDIRECT_TARGET =~ ^https?:// ]]; then 12 | REDIRECT_TARGET="http://$REDIRECT_TARGET" 13 | fi 14 | fi 15 | 16 | # Default to 80 17 | LISTEN="80" 18 | # Listen to PORT variable given on Cloud Run Context 19 | if [ ! -z "$PORT" ]; then 20 | LISTEN="$PORT" 21 | fi 22 | 23 | : ${RETAIN_PATH:='true'} 24 | if [ "$RETAIN_PATH" = "true" ]; then 25 | cat < /etc/nginx/conf.d/default.conf 26 | server { 27 | listen ${LISTEN}; 28 | 29 | rewrite ^(.*)\$ ${REDIRECT_TARGET}\$1 ${REDIRECT_TYPE}; 30 | } 31 | EOF 32 | else 33 | cat < /etc/nginx/conf.d/default.conf 34 | server { 35 | listen ${LISTEN}; 36 | 37 | rewrite ^(.*)\$ ${REDIRECT_TARGET} ${REDIRECT_TYPE}; 38 | } 39 | EOF 40 | fi 41 | 42 | 43 | echo "Listening to $LISTEN, Redirecting HTTP requests to ${REDIRECT_TARGET}..." 44 | 45 | exec nginx -g "daemon off;" 46 | --------------------------------------------------------------------------------