Logo

dev-resources.site

for different kinds of informations.

Deploy NestJS and NextJS application in same server using pm2 and Nginx

Published at
1/2/2025
Categories
nestjs
nextjs
pm2
nginx
Author
fahim_hasnainfahad_7e50d
Categories
4 categories in total
nestjs
open
nextjs
open
pm2
open
nginx
open
Author
24 person written this
fahim_hasnainfahad_7e50d
open
Deploy NestJS and NextJS application in same server using pm2 and Nginx

NextJS is a popular framework of React for Frontend Web Development.
NestJS is another popular framework of NodeJS, widely used for Backend development.

In many projects, we need to deploy NestJS and NextJS applications in a same server in different ports and access them from outside.

PM2 is a production level process management tool for NodeJS.
NginX is going to be used as our reverse proxy for this system.

Requirements

  1. Frontend (NextJS) project will be run at 3001 port.
  2. Backend (NestJS) project will be run at 8000 port.
  3. Frontend will be accessible from the '/' route.
  4. Backend will be accessible from the '/api' route.

System Architecture

User hits the server via Nginx and Nginx reroutes the request to different ports based on the route.

Run the Backend

git clone your-backend-repo.git
cd your-backend-repo
npm install
# Run commands for DB migration if needed...
npm run build

# Start project with pm2
pm2 start dist/main.js --name backend # provide a name you like
pm2 save

Enter fullscreen mode Exit fullscreen mode

Make sure in the NestJS src/main.ts file, you mention the port in this line or in .env:

await app.listen(process.env.PORT ?? 8000);
Enter fullscreen mode Exit fullscreen mode

This will run the backend repo using pm2 in port 8000.

Run the Frontend

git clone your-frontend-repo.git
cd your-frontend-repo
npm install
npm run build

# Start project with pm2
pm2 start npm --name "frontend" -- start # provide a name you like, runs the start command inside package.json
pm2 save

Enter fullscreen mode Exit fullscreen mode

Make sure in the package.json file, there is a command in the scripts object called start which will look like this:

"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start -p 3001"
  },
Enter fullscreen mode Exit fullscreen mode

This will run the Frontend project in 3001 port.

Nginx Configuration

First, create an Nginx configuration file.

sudo vim /etc/nginx/sites-available/project-name
Enter fullscreen mode Exit fullscreen mode

Then paste this following configuration:

server {
    listen 80;
    server_name your-domain-name; # put your domain name here

    # NextJs Application Route
    location / {
        proxy_pass http://localhost:3001;
        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;
    }

    # NestJS Application Route
    location /api {
        proxy_pass http://localhost:8000;
        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;
    }
}
Enter fullscreen mode Exit fullscreen mode

After this, we need to create a symbolic link to enable the nginx configuration in our server.

sudo ln -s /etc/nginx/sites-available/project-name /etc/nginx/sites-enabled/ #update your file name
Enter fullscreen mode Exit fullscreen mode

After this we need to test the Nginx configuration using the following command:

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

If this command returns that everything is correct, we need to restart the nginx using following command:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Great!! we can now access our APIs from your-domain-name/api route and see the UI from your-domain-name/ route.

nestjs Article's
30 articles in total
Favicon
Understanding CORS and Setting it up with NestJS + Fastify ๐Ÿš€
Favicon
Crudify: Automate Your Mongoose CRUD Operations in NestJS
Favicon
Building "Where Am I?": A GeoGuessr Alternative for Mobile
Favicon
Utilizando la librerรญa Mongoose
Favicon
How to easily send emails in NestJS?
Favicon
Why NestJS Is The New Gold Standard For Node Backend Development
Favicon
Designing RBAC Permission System with Nest.js: A Step-by-Step Guide
Favicon
How to work with CAR files with NestJS
Favicon
Handle Payments Module in NestJS
Favicon
What we're working on: Database Entities
Favicon
Deploy NestJS and NextJS application in same server using pm2 and Nginx
Favicon
Advance nest.js
Favicon
Converting date by user time zone in "NestJS", and entering and displaying date in "Angular"
Favicon
Web Performance Optimization Tips from My Latest Project
Favicon
Blogsphere | A blogging website made with MERN stack. includes user management.
Favicon
nest.js
Favicon
[Boost]
Favicon
Wanna test NestJS out - any tips
Favicon
nestJS Modern Framework for Scalable Applications
Favicon
๐Ÿš€ Applying SOLID Principles in NestJS: A Practical Guide
Favicon
Nestjs
Favicon
Which One Should You Choose NEST JS or EXPRESSย JS?
Favicon
Integrating and storing the selected user language into the database in a full-stack application on "Angular" and "NestJS"
Favicon
Hello,help review my fullstack website stack : nestjs,mongodb and reactjs. https://events-org-siiv.vercel.app/
Favicon
The Ultimate Tech Stack for Startups in 2025
Favicon
NestJS and more
Favicon
Como implementar Feature Flags em seu Backend NestJS
Favicon
Building an Image Moderation System with AWS Rekognition, Nest.js, and React
Favicon
How to Create a quick Authentication library for NestJS/MongoDB application
Favicon
Understanding DTOs (Data Transfer Objects) in NestJS๐Ÿš€

Featured ones: