Skip to main content
When self-hosting Next.js, you have several deployment options depending on your infrastructure. This guide covers configuration for each approach.

Reverse proxy

Place a reverse proxy (such as Nginx) in front of your Next.js server rather than exposing it directly to the internet. A proxy handles malformed requests, rate limiting, payload size limits, and security concerns, freeing the Next.js server to focus on rendering.

Image optimization

next/image works with zero configuration when running next start. To use a separate image optimization service, configure a custom image loader.
On glibc-based Linux systems, image optimization may require additional configuration to prevent excessive memory usage.

Environment variables

Server environment variables are available at runtime during dynamic rendering:
This allows a single Docker image to be promoted through multiple environments with different values.

Caching and ISR

By default, the Next.js cache is stored on the filesystem. This works automatically when self-hosting with both the Pages and App Router.

Configuring caching

For distributed deployments (e.g., Kubernetes with multiple pods), configure a custom cache handler to share cache across instances:
You can store cached values in external storage like Redis or AWS S3 for consistency across pods.

Build cache

Use a consistent build ID across containers to avoid stale assets:

Multi-server deployments

Server Actions encryption key

When running multiple server instances, all must use the same encryption key for Server Actions. Set a consistent key via environment variable:
The key must be a base64-encoded value with a valid AES key length (16, 24, or 32 bytes).

Deployment identifier

Configure a deploymentId to enable version skew protection during rolling deployments:
When a deployment ID is configured, Next.js includes it in asset URLs and navigation requests. If a mismatch is detected between client and server, Next.js triggers a full page reload to ensure clients receive consistent assets.

Shared cache

By default, the in-memory cache is not shared across instances. Use 'use cache: remote' with a custom cache handler to store data in external storage.

Streaming and Suspense

The App Router supports streaming responses when self-hosting. If using Nginx, disable buffering to enable streaming:

Docker deployment

A typical Next.js Dockerfile uses standalone output to minimize image size:
1

Enable standalone output

2

Build the Docker image

3

Run the container

after()

The after function is fully supported when self-hosting with next start. When stopping the server, send SIGINT or SIGTERM signals and wait for pending callbacks to complete.