How to Self-Host Your Next.js Application – Configuration, Caching, and Deployment Strategies

Self-hosting a Next.js application gives you full control over caching, image optimization, environment variables, and deployment behavior. This article walks through how to configure your app for production, manage ISR and build cache, handle streaming, and optimize performance across containers and CDNs.

Self-hostingNext.jsISRProxyEnvironment Variables

~3 min read · Updated Oct 28, 2025

Image Optimization


Image optimization via next/image works out of the box with next start. You can also configure a custom image loader in next.config.js for static exports or external services.

  • Images are optimized at runtime, not during build
  • Can be disabled while retaining layout and format benefits
  • On Linux (glibc), additional memory tuning may be required

Proxy Support


Proxy works automatically when self-hosting with next start. It uses the Edge runtime for low-latency logic before routes or assets are served.

  • Not supported in static exports
  • Use full Node.js runtime if Edge APIs are insufficient
  • For advanced logic, consider Server Components or custom servers

Environment Variables


Next.js supports both build-time and runtime environment variables:

  • Server-only by default
  • Expose to client with NEXT_PUBLIC_ prefix
  • Runtime variables are evaluated during dynamic rendering
const value = process.env.MY_VALUE

Caching and ISR


Next.js caches static pages, ISR outputs, and assets automatically. The default cache is stored on disk and in memory.

  • Immutable assets: Cache-Control: public, max-age=31536000, immutable
  • ISR pages: s-maxage and stale-while-revalidate
  • Dynamic pages: private, no-cache, no-store

Custom Cache Handler


To share cache across containers or disable in-memory caching:

// next.config.js
module.exports = {
  cacheHandler: require.resolve('./cache-handler.js'),
  cacheMaxMemorySize: 0,
}

Example cache-handler.js:

const cache = new Map()

module.exports = class CacheHandler {
  async get(key) { return cache.get(key) }
  async set(key, data, ctx) {
    cache.set(key, { value: data, lastModified: Date.now(), tags: ctx.tags })
  }
  async revalidateTag(tags) {
    tags = [tags].flat()
    for (let [key, value] of cache) {
      if (value.tags.some(tag => tags.includes(tag))) {
        cache.delete(key)
      }
    }
  }
  resetRequestCache() {}
}

Build Cache and Versioning


Use a consistent build ID across containers:

generateBuildId: async () => process.env.GIT_HASH

Next.js detects version skew and reloads assets when needed. Use localStorage or URL state to persist data across reloads.


Streaming and Suspense


Streaming is supported in the App Router. To enable it behind Nginx:

// next.config.js
headers: [
  {
    source: '/:path*{/}?',
    headers: [{ key: 'X-Accel-Buffering', value: 'no' }],
  },
]

Static Assets and CDN Usage


Use assetPrefix to host assets on a separate domain or CDN:

assetPrefix: 'https://cdn.example.com'

Dynamic pages will include Cache-Control: private to prevent CDN caching. Static pages will include Cache-Control: public.


Conclusion


Self-hosting your Next.js application gives you full control over caching, environment variables, streaming, and deployment. With proper configuration, you can scale across containers, optimize performance, and maintain consistency across environments.


Written & researched by Dr. Shahin Siami

Related Articles

Advanced Client-Side Routing and Performance Hooks in Next.js

Next.js provides a rich set of client-side hooks and caching utilities that empower developers to build dynamic, responsive, and secure applications. From reading route parameters to tracking navigation state and reporting performance metrics, this guide walks you through the most important tools available in the App Router.

Continue

Handling Authorization and Caching in Next.js: A Developer’s Guide

Next.js introduces powerful experimental features for access control and smart caching. This guide covers the unauthorized() function for custom 401 handling, unstable_cache for persistent memoization, updateTag for instant cache invalidation, and useLinkStatus for inline navigation feedback. Learn how to use these tools to build secure, performant, and responsive applications.

Continue

redirect and refresh in Next.js — Smart Redirects and Client Refreshing via Server Actions

The redirect function in Next.js allows you to navigate users to a new route, returning either a 307 or 303 HTTP response depending on context. It works in Server Components, Client Components, Route Handlers, and Server Actions. The refresh function is used exclusively within Server Actions to refresh the client router. This article explains how both functions work, with practical examples and key considerations.

Continue

NextRequest and NextResponse in Next.js — Managing Cookies, Headers, Redirects, and Rewrites

Next.js extends the native Web Request and Response APIs with NextRequest and NextResponse, offering powerful tools for managing cookies, headers, redirects, rewrites, and JSON responses. These utilities simplify server-side logic and improve control over routing, personalization, and security. This guide walks through their capabilities with practical examples and best practices.

Continue

headers, ImageResponse, notFound, and permanentRedirect in Next.js — Request Handling, Dynamic Images, Errors, and Redirects

Next.js offers powerful tools for handling HTTP requests and responses in Server Components. The headers function lets you read incoming request headers. ImageResponse allows you to generate dynamic images using JSX and CSS. The notFound function renders a custom 404 page, and permanentRedirect enables permanent redirection to another route. This article explains how to use each feature with practical examples.

Continue

A Complete Guide to Using metadata and generateMetadata in Next.js

In modern versions of Next.js, managing page metadata is more powerful and intuitive than ever. Metadata is automatically injected into the <head> of your pages and plays a vital role in SEO, social sharing, and user experience. This guide explains the two main ways to define metadata: using the static metadata object and the dynamic generateMetadata function.

Continue