Deploying to Vercel
Vercel is the platform built by the Next.js team. It requires zero configuration.1
Push your code to a Git provider
Push your repository to GitHub, GitLab, or Bitbucket.
2
Import your repository on Vercel
Go to vercel.com/new, import your repository, and click Deploy. Vercel detects that you are using Next.js automatically.
3
Set environment variables
In your Vercel project settings, add any environment variables from your
.env.local that are needed in production.- Building and deploying on every push.
- Serverless Functions for API routes and SSR pages.
- Incremental Static Regeneration on the edge.
- Image optimization.
- Preview deployments for pull requests.
Deploying to a Node.js server
For a self-hosted Node.js deployment:1
Build the application
.next/ directory.2
Start the production server
PORT environment variable to change it:Keeping the server running
Use a process manager like PM2 to keep your server running and restart it on crash:Deploying with Docker
Docker lets you package your application with its dependencies and deploy it anywhere containers run. Create aDockerfile at the root of your project:
Dockerfile
next.config.js so Next.js produces a self-contained server.js file:
next.config.js
Static export
If your application has no server-side requirements, you can export it as static HTML files. Static exports work without a Node.js server. Setoutput: 'export' in next.config.js:
next.config.js
out/ directory that you can deploy to any static hosting service (Nginx, Apache, S3, GitHub Pages, Cloudflare Pages, etc.).
Limitations of static export
The following features are not compatible withoutput: 'export' because they require a server:
getServerSideProps- API routes (
pages/api/) - Image Optimization (unless you use a custom image loader)
- Incremental Static Regeneration
- Internationalized routing
Serving the export
To serve theout/ directory locally with Nginx:
Configuring for production
Environment variables
Set production environment variables on your hosting platform. Never commit.env.local to version control.
Caching
Next.js includes an HTTP response cache. For SSR pages, you can setCache-Control headers in getServerSideProps:
Health checks
Create a health check endpoint for load balancers:pages/api/health.ts
For a full production deployment checklist, see the Next.js production checklist.
