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: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: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:Deployment identifier
Configure adeploymentId to enable version skew protection during rolling deployments:
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: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.