Skip to main content

How to Deploy Next.js on a VPS with Nginx: A Professional Guide

Leo Liebert
NR Studio
5 min read

Deploying a Next.js application to a Virtual Private Server (VPS) instead of a managed platform like Vercel offers CTOs and founders complete control over infrastructure, security, and cost. While managed platforms simplify deployment, they can introduce vendor lock-in and unexpected scaling costs. For high-traffic applications or specialized enterprise environments, a self-managed VPS setup provides a predictable, performance-optimized foundation.

This guide outlines the technical requirements and configuration steps to successfully host a production-ready Next.js application behind an Nginx reverse proxy. We will focus on process management, security hardening, and performance optimization, ensuring your infrastructure is as robust as your code.

Prerequisites and Infrastructure Requirements

Before starting, ensure your VPS meets the minimum performance requirements. A standard Next.js application requires a Node.js runtime and a process manager. We recommend a minimum of 2GB RAM for production builds to avoid memory-related crashes during the compilation phase.

  • Node.js and npm/yarn/pnpm: Use a version manager like nvm to ensure compatibility with your local development environment.
  • Nginx: Acts as the reverse proxy to handle SSL termination, load balancing, and static file caching.
  • PM2: An industry-standard production process manager that ensures your Next.js application restarts automatically after crashes or system reboots.
  • Firewall: UFW (Uncomplicated Firewall) configured to allow only SSH, HTTP, and HTTPS traffic.

Building the Application for Production

Next.js requires a build step to generate the production-ready static assets and server-side bundles. Never run next dev in a production environment, as it is unoptimized and insecure.

npm run build

Once the build completes, the .next folder is generated. You only need to transfer this folder, the public directory, package.json, and node_modules to your server. Using a CI/CD pipeline or a simple rsync script is recommended to automate this transfer rather than manual file uploads.

Managing Application Processes with PM2

Running a Node.js process directly in the terminal is insufficient for production. PM2 handles log management, process monitoring, and auto-restart policies. After installing PM2 globally (npm install -g pm2), start your application using the following command:

pm2 start npm --name "my-app" -- run start

To ensure the application persists after a server reboot, generate and execute the PM2 startup script:

pm2 save
pm2 startup

This ensures your application remains online even after an unexpected server restart, fulfilling basic high-availability requirements.

Configuring Nginx as a Reverse Proxy

Nginx acts as the gateway for your application. It forwards incoming traffic from port 80/443 to the local port where your Next.js application is running (typically 3000). Create a configuration file in /etc/nginx/sites-available/your-domain.com:

server {
listen 80;
server_name your-domain.com;

location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

This configuration handles header forwarding, which is critical for correctly capturing client IP addresses and protocol information in your application logs.

Security and Performance Considerations

Security is a shared responsibility. When using a VPS, you must manage your own SSL certificates using Certbot. Additionally, implement rate limiting in Nginx to mitigate brute-force attacks on your API routes.

For performance, configure Nginx to serve static assets directly from the .next/static directory. This bypasses the Node.js runtime entirely for CSS, JS, and image files, significantly reducing server load. Ensure that Cache-Control headers are set appropriately to allow browsers to cache these assets effectively.

Tradeoffs: Managed Platforms vs. Self-Hosted VPS

The core tradeoff is Operational Overhead vs. Cost/Control. Managed platforms like Vercel remove the burden of server management, patching, and scaling, but they charge a premium for this convenience. A self-hosted VPS is significantly cheaper at scale, but it requires internal expertise to maintain security patches, manage OS updates, and configure load balancing.

Factor Managed Platform Self-Hosted VPS
Maintenance Low High
Cost Variable/High Fixed/Low
Customization Limited Unlimited

Factors That Affect Development Cost

  • Server resource allocation (CPU/RAM)
  • Traffic volume and bandwidth usage
  • Maintenance labor hours for security patching
  • CI/CD pipeline complexity

VPS costs are generally fixed monthly fees, whereas managed platforms scale costs with traffic or usage, making VPS more predictable for high-traffic systems.

Frequently Asked Questions

Why do I need Nginx if Next.js has a built-in server?

While the built-in server is sufficient for development, Nginx provides essential production features like SSL termination, static file caching, load balancing, and protection against common network-level attacks. It acts as a hardened buffer between the internet and your Node.js application process.

How do I update my application on a VPS without downtime?

To achieve zero-downtime deployments, you should use a CI/CD pipeline that builds your new version, pulls the code, installs dependencies, and then reloads the PM2 process. Alternatively, you can use blue-green deployment strategies to swap traffic between two different instances of your application.

Is hosting on a VPS actually cheaper than Vercel?

For small projects, the time cost of managing a VPS may exceed the subscription cost of a managed platform. However, for applications with high traffic, heavy data processing, or complex architectural requirements, a VPS is significantly more cost-effective as you pay for raw resources rather than per-request or per-build usage.

Deploying Next.js on a VPS offers the architectural freedom required for complex, high-traffic applications. By combining PM2 for process management and Nginx for robust traffic handling, you build a reliable foundation that avoids the limitations of managed platforms. While this approach requires more hands-on maintenance, the resulting performance and cost-efficiency are often worth the investment for growth-focused companies.

If you are looking to scale your infrastructure or need expert guidance on optimizing your deployment pipeline, the team at NR Studio specializes in high-performance web development and infrastructure architecture. Let us help you build a system that scales with your business needs.

NR Studio builds custom web apps, mobile apps, SaaS platforms, and internal tools for growing businesses. If you’re working through a technical decision, feel free to reach out — no commitment required.

References & Further Reading

NR Studio Engineering Team
2 min read · Last updated recently

Leave a Comment

Your email address will not be published. Required fields are marked *