A lightweight Nitro.js application that redirects all incoming requests to a specified target URL. Built with Nitro.
- Features
- Quick Start
- Configuration
- Docker Usage
- Local Development
- Production Deployment
- Testing
- Releasing
- 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/amd64andlinux/arm64
docker run -d \
-e REDIRECT_TARGET=example.com \
-e REDIRECT_STATUS_CODE=301 \
-p 3000:3000 \
piscis/nitro-web-redirect:latest# 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:localMaintainers skip the .env step and run pnpm run dev, which takes the configuration
from Phase: see Development 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 |
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_TARGETis missing, empty, or the stringundefinedornull.REDIRECT_STATUS_CODEis set to anything other than301,302,303,307or308(for example200,404orabc). An empty value counts as unset and gives301.
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.
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://orhttps://. Otherwise the request's protocol is used, honouring theX-Forwarded-Protoheader set by a reverse proxy. A host such ashttpbin.orgcounts as having no protocol. - Trailing slashes on the target are trimmed, so
https://example.com/and/foogivehttps://example.com/foo. - Base path: a path in the target is placed in front of the requested path, so
https://example.com/baseand/foo?x=1givehttps://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 |
# 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=302The 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:latestdocker run -d \
-e REDIRECT_TARGET=example.com \
-e REDIRECT_STATUS_CODE=301 \
-p 3000:3000 \
piscis/nitro-web-redirect:latest# 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-redirectversion: '3'
services:
redirect:
image: piscis/nitro-web-redirect:latest
environment:
REDIRECT_TARGET: example.com
REDIRECT_STATUS_CODE: 301
ports:
- '3000:3000'
restart: unless-stopped- Node.js 26 (see
.nvmrc) - pnpm 12 (see
packageManagerinpackage.json)
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 placeEach 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.
# Install dependencies
pnpm install
# Start development server
pnpm run dev # maintainers, configuration from Phase
pnpm run dev:local # with a .env of your ownThe development server will be available at http://localhost:3000
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.
pnpm run dev- Start development server, configuration from Phasepnpm run dev:local- Start development server, configuration from.envpnpm run build- Build for productionpnpm run preview- Preview production build locally, configuration from Phasepnpm run preview:local- Preview production build locally, configuration from.envpnpm run lint- Run lintingpnpm run test- Run tests in watch modepnpm run test:run- Run tests once
pnpm run buildpnpm startFor more deployment options, see the Nitro deployment documentation.
Run the test suite:
pnpm run test:runUnit 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.
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.