Skip to content

About

Tiny Nitro service that redirects every request to a target URL — shipped as a Docker image

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

232 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nitro Web Redirect

A lightweight Nitro.js application that redirects all incoming requests to a specified target URL. Built with Nitro.

Table of Contents

Features

  • Redirects every request to one configured target
  • Configurable redirect status code: 301, 302, 303, 307 or 308
  • Keeps the requested path and query string, with an optional base path on the target
  • Validates its configuration at startup and refuses to start if it is invalid
  • Available as a Docker image for linux/amd64 and linux/arm64

Quick Start

Using Docker (Recommended)

docker run -d \
  -e REDIRECT_TARGET=example.com \
  -e REDIRECT_STATUS_CODE=301 \
  -p 3000:3000 \
  piscis/nitro-web-redirect:latest

Local Installation

# Clone the repository
git clone https://github.com/piscis/nitro-web-redirect.git
cd nitro-web-redirect

# Install dependencies (using pnpm, the recommended package manager)
pnpm install

# Configure environment variables
cp .env.example .env

# Start the server
pnpm run dev:local

Maintainers skip the .env step and run pnpm run dev, which takes the configuration from Phase: see Development configuration.

Configuration

Configure the application using environment variables:

Variable Required Default Description
REDIRECT_TARGET Yes - The redirect target: a host, optionally with a protocol and a base path
REDIRECT_STATUS_CODE No 301 The redirect status code: one of 301, 302, 303, 307, 308

Startup validation

The configuration is checked once when the server starts. On success it logs the configuration it serves:

ℹ Redirecting to https://example.com with 301

If the configuration is invalid, the server logs the reason and exits with code 1 instead of starting:

  • REDIRECT_TARGET is missing, empty, or the string undefined or null.
  • REDIRECT_STATUS_CODE is set to anything other than 301, 302, 303, 307 or 308 (for example 200, 404 or abc). An empty value counts as unset and gives 301.
 ERROR  Invalid configuration: REDIRECT_STATUS_CODE "abc" is not one of 301, 302, 303, 307, 308

Each redirect is logged as one line, e.g. 301 GET /foo?x=1 → https://example.com/foo?x=1.

How the location is built

Every request is answered with a redirect to the target followed by the requested path and query string:

  • Protocol: the target has a protocol only if it starts with http:// or https://. Otherwise the request's protocol is used, honouring the X-Forwarded-Proto header set by a reverse proxy. A host such as httpbin.org counts as having no protocol.
  • Trailing slashes on the target are trimmed, so https://example.com/ and /foo give https://example.com/foo.
  • Base path: a path in the target is placed in front of the requested path, so https://example.com/base and /foo?x=1 give https://example.com/base/foo?x=1.
  • Path and query of the request are kept unchanged, including percent-encoding.
REDIRECT_TARGET Request Location
https://example.com http://redirect.local/foo?x=1 https://example.com/foo?x=1
example.com http://redirect.local/foo http://example.com/foo
example.com /foo with X-Forwarded-Proto: https https://example.com/foo
https://example.com/ /foo https://example.com/foo
https://example.com/base /foo?x=1 https://example.com/base/foo?x=1

Examples

# Redirect to example.com with the same protocol as the request
REDIRECT_TARGET='example.com'

# Redirect to example.com with HTTPS protocol regardless of request protocol
REDIRECT_TARGET='https://example.com'

# Redirect into a base path on the target
REDIRECT_TARGET='https://example.com/base'

# Use a temporary redirect (302) instead of permanent (301)
REDIRECT_STATUS_CODE=302

Docker Usage

Using Pre-built Image from Docker Hub

The official Docker image is available on Docker Hub for linux/amd64 and linux/arm64. Every tag covers both platforms, and Docker pulls the one that fits the host:

docker pull piscis/nitro-web-redirect:latest

Running the Docker Container

docker run -d \
  -e REDIRECT_TARGET=example.com \
  -e REDIRECT_STATUS_CODE=301 \
  -p 3000:3000 \
  piscis/nitro-web-redirect:latest

Building Your Own Docker Image

# Build the image
docker build -t nitro-web-redirect .

# Run the container
docker run -d \
  -e REDIRECT_TARGET=example.com \
  -p 3000:3000 \
  nitro-web-redirect

Docker Compose Example

version: '3'
services:
  redirect:
    image: piscis/nitro-web-redirect:latest
    environment:
      REDIRECT_TARGET: example.com
      REDIRECT_STATUS_CODE: 301
    ports:
      - '3000:3000'
    restart: unless-stopped

Local Development

Prerequisites

  • Node.js 26 (see .nvmrc)
  • pnpm 12 (see packageManager in package.json)

Toolchain

What matters is the two versions, not how you get them: Node.js from .nvmrc and pnpm from packageManager in package.json. Nothing in the repository binds you to a version manager — there is no .prototools, .tool-versions or Volta pin to keep in sync — so pick whichever of these you already have, or none:

proto install node && proto install pnpm   # https://moonrepo.dev/docs/proto/install
fnm use && corepack enable                 # or nvm use, asdf install, …
corepack enable                            # a system Node.js 26 already in place

Each of those reads the pins above rather than a file of its own. With a manager's shims on PATH, plain node and pnpm resolve the version this directory pins; node --version against .nvmrc and pnpm --version against packageManager are the only check that matters.

Setup

# Install dependencies
pnpm install

# Start development server
pnpm run dev          # maintainers, configuration from Phase
pnpm run dev:local    # with a .env of your own

The development server will be available at http://localhost:3000

Development configuration

The development server needs REDIRECT_TARGET, and the startup check exits if it is missing. There are two ways to supply it, and the repository does not carry a .env.

External developers: cp .env.example .env, edit it, and use the :local scripts (pnpm run dev:local, pnpm run preview:local). Nitro reads .env itself.

Maintainers: the Development set lives in Phase, on a self-hosted instance. .phase.json links this repository to the app and holds only opaque identifiers, so it is safe to commit. Ask the maintainer for the host URL and an invite to the organisation.

brew install phase                 # or https://pkg.phase.dev/install.sh
phase auth                         # against the self-hosted host
phase secrets list                 # sanity check: the Development set

pnpm run dev                       # runs through `phase run`

phase run spawns its child through sh -c, which has no node_modules/.bin on PATH. Inside a pnpm script that does not matter, because pnpm puts it there; calling phase run straight from a shell, use pnpm exec <bin>, and quote the whole command when it takes flags of its own (phase run "pnpm exec nitro dev --port 4000") so phase does not read them as its own.

Available Scripts

  • pnpm run dev - Start development server, configuration from Phase
  • pnpm run dev:local - Start development server, configuration from .env
  • pnpm run build - Build for production
  • pnpm run preview - Preview production build locally, configuration from Phase
  • pnpm run preview:local - Preview production build locally, configuration from .env
  • pnpm run lint - Run linting
  • pnpm run test - Run tests in watch mode
  • pnpm run test:run - Run tests once

Production Deployment

Build for Production

pnpm run build

Start Production Server

pnpm start

For more deployment options, see the Nitro deployment documentation.

Testing

Run the test suite:

pnpm run test:run

Unit tests in test/unit/ cover the configuration and location rules without a server. The tests in test/e2e/ build the server and send requests to it in-process with nitro-test-utils; each file runs one configuration, because the server reads it once at startup.

Every end-to-end file pins the whole configuration through withConfiguration, so the suite gives the same result whatever is in your own environment. Run it plainly, with pnpm run test:run — not through phase run.

Releasing

Versions come from the conventional-commit titles on main: pnpm release writes CHANGELOG.md and bumps package.json on a release branch, a v* tag creates the release, and the GitHub mirror publishes the image to Docker Hub after a smoke test. The steps are in docs/agents/releasing.md.


License

MIT License

About

Tiny Nitro service that redirects every request to a target URL — shipped as a Docker image

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages