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.

nginx Article's
30 articles in total
Favicon
nginx-mod-http-geoip
Favicon
How to run a Nginx-web server
Favicon
ngx whitelist/blacklist module
Favicon
Nginx Simplified: Technical Insights with Real-World Analogies
Favicon
Nginx Configuration Tips for Secure Communication: Enabling mTLS and checking client fingerprint
Favicon
Building a Scalable Reverse Proxy Server like Nginx with Node.js and TypeScript
Favicon
Deploy NestJS and NextJS application in same server using pm2 and Nginx
Favicon
Setting Up an NGINX Reverse Proxy with a Node.js Cluster Using Docker
Favicon
การทำ HTTPS ด้วย Certbot และ Nginx บน Ubuntu Server
Favicon
How to configure Free SSL Certificate on Nginx using Certbot
Favicon
Docker Hands-on: Learn Docker Volume and Bind Mounts with Sample Projects using NGINX
Favicon
自建的git远程仓库,在push时413 Request Entity Too Large
Favicon
Optimize SvelteKit performance with brotli compression
Favicon
I’m running a Spring Boot application inside a Docker container on my VM. The application works fine over HTTP, and I can access all endpoints via http://127.0.0.1:8080. I’ve set up NGINX as a reverse proxy to serve HTTPS requests. No errors for http reqs.
Favicon
Deploying a MERN App on Azure: The Smart Way
Favicon
My First Full-Stack Deployment with Docker and NGINX as Load Balancer
Favicon
Streamlined Release Process for a Web Application: Trunk-Based Development with Feature Flags
Favicon
How to Install NGINX on Ubuntu 22.04
Favicon
Secure Nginx with Let's Encrypt on Ubuntu
Favicon
Kubernetes Ingress Controllers and NGINX Ingress: A Complete Guide
Favicon
What is HTTP 499 Status Code and How to Fix it?
Favicon
Docker Compose Demo: Running Multiple Services with Two Domains on Localhost
Favicon
Building a Production Stack: Docker, Meilisearch, NGINX & NestJS
Favicon
Step-by-Step Guide: Assigning a Namecheap Domain to DigitalOcean Hosting with Nginx
Favicon
Streamlining React CI/CD Pipelines with GitHub Actions
Favicon
Connecting to an EC2 Instance with Ubuntu and Installing NGINX on AWS
Favicon
Installing Nginx Web Server on Linux: A Step-by-Step Guide
Favicon
Hosting multiple Websites on a single Nginx Server
Favicon
Unleashing the Power of NGINX as an API Gateway
Favicon
Installing Wordpress with Nginx in Ubuntu

Featured ones: