Back to articles
DevOps & Systems

Comprehensive Guide to Deploying Node.js/Next.js Applications on VPS with Docker and Nginx Reverse Proxy

A detailed guide on deploying Node.js/Next.js applications on VPS using Docker and Nginx as a reverse proxy, including configuration, optimization, and security.

DPTCloud Engineering4 min read
Comprehensive Guide to Deploying Node.js/Next.js Applications on VPS with Docker and Nginx Reverse Proxy

Introduction

Deploying Node.js or Next.js applications on a VPS is a crucial step in modern software development. Using Docker along with Nginx as a reverse proxy enables easy management, deployment, and security of your applications. This guide will walk you through deploying a Node.js/Next.js application on a VPS using Docker and Nginx, ensuring high performance and stability.

Prerequisites

  • A VPS (NVMe Cloud VPS for Docker, Nginx & Databases) with Linux operating system (Ubuntu 20.04/22.04 recommended).
  • SSH access to the VPS (usually via SSH key).
  • Node.js and npm installed on the VPS.
  • Docker and Docker Compose installed.
  • Application context: Node.js or Next.js (minimum 12.0, recommended 16.0 or higher).
  • Basic knowledge of creating and managing Nginx configuration files.

Application Preparation and Deployment

First, create a directory for your application and configure Docker.

1. Create Directory and Configure Application

Assume you are deploying a Next.js application. Create a directory:

mkdir my-nextjs-app
cd my-nextjs-app

Create a next.config.js file if customization is needed (e.g., port configuration):

module.exports = {
  port: 3000,
};

2. Create Dockerfile

Create a Dockerfile in the project directory:

FROM node:16-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .
RUN npm run build

EXPOSE 3000

CMD ["npm", "start"]

3. Create docker-compose.yml

Create a docker-compose.yml file:

version: '3.8'

services:
  app:
    build: .
    ports:
      - "3000:3000"
    restart: unless-stopped
    environment:
      - NODE_ENV=production
    volumes:
      - ./app:/app
      - ./node_modules:/app/node_modules

Configuring Nginx as Reverse Proxy

Nginx will receive external requests and forward them to the application container running on port 3000.

1. Install Nginx

sudo apt update
sudo apt install nginx -y

2. Create Nginx Configuration File

Create a configuration file in /etc/nginx/sites-available/myapp:

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

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # Optional: Redirect HTTP to HTTPS
    # return 301 https://$host$request_uri;
}

3. Enable Configuration

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Start the Application

In the project directory, run:

docker-compose up -d

The application will be built, started, and accessible via Nginx at http://your-domain.com.

Testing and Verification

  • Check Docker status: docker-compose ps
  • Access the website to verify the application is running: curl -I http://your-domain.com
  • View logs: docker-compose logs -f app

Troubleshooting

  • Application not accessible: Check if Nginx is running: sudo systemctl status nginx. Verify configuration file syntax: sudo nginx -t.
  • Docker fails to build application: Check Dockerfile for syntax errors. Ensure package.json has all required dependencies.
  • Application runs but does not respond: Verify port mappings in Dockerfile and docker-compose.yml match. Check that the environment variable (NODE_ENV) is correctly set.

Optimization and Security

  • Enable HTTPS using Let's Encrypt: Use certbot or nginx-letsencrypt.
  • Add rate limiting in Nginx to prevent DDoS attacks.
  • Run Docker containers under a non-root user.
  • Monitor logs and application performance using docker logs or tools like ELK.

References

Good luck deploying your Node.js/Next.js application on VPS with Docker and Nginx!

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478