Introduction

NestJS is a framework for building efficient, reliable, and scalable server-side applications with Node.js. In this guide, we'll walk through the steps to deploy a NestJS application with Nginx on Ubuntu Linux and configure it for production use.

Prerequisites

Before proceeding, ensure you have:

  1. An Ubuntu Linux server
  2. A NestJS application ready for deployment
  3. Basic knowledge of Nginx and Ubuntu server administration

Steps to Deploy a NestJS Application with Nginx

    1. Install Node.js and npm: If Node.js and npm are not already installed on your Ubuntu server, install them using the following commands:
sudo apt update
sudo apt install nodejs npm
    1. Clone your NestJS application: Clone your NestJS application repository to your Ubuntu server.
    2. Install dependencies and build: Navigate to your NestJS application directory and install dependencies using npm:
cd /path/to/nestjs-app
npm install
npm run build
    1. Configure Nginx: Create a new server block configuration file for your NestJS application in Nginx:
sudo nano /etc/nginx/sites-available/nestjs-app

Inside the file, configure Nginx to proxy requests to your NestJS application:

server {
    listen 80;
    server_name example.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;
    }
}
    1. Enable the server block: Create a symbolic link to enable the server block configuration:
sudo ln -s /etc/nginx/sites-available/nestjs-app /etc/nginx/sites-enabled/
    1. Test Nginx configuration: Test the Nginx configuration for syntax errors:
sudo nginx -t
    1. Reload Nginx: Reload Nginx to apply the changes:
sudo systemctl reload nginx
    1. Start your NestJS application: Start your NestJS application in production mode:
npm run start:prod

Conclusion

Congratulations! You have successfully deployed your NestJS application with Nginx on Ubuntu Linux and configured it for production use. Your application should now be accessible through the Nginx reverse proxy, providing improved performance and security.

Was this answer helpful? 0 Users Found This Useful (0 Votes)