Publishing NestJS with Nginx (Ubuntu VPS)
What will you learn in this guide?
In this guide, you will learn how to run a NestJS-based backend application on Ubuntu VPS.
Nginx reverse proxy, PM2 process management and HTTPS configuration are explained step by step.
You'll end up with a secure and production-ready installation.
Technical Summary
Main topic: Deploying NestJS application with Nginx
Solved problem: Secure and continuous operation of the backend application
Gain: Scalable, manageable and HTTPS supported streaming
Steps followed:
- Prepare NestJS application
- Running the application with PM2
- Nginx reverse proxy configuration
- Add SSL certificate
Prerequisites
- A VPS with Ubuntu 22.04 installed
- Node.js and npm must be installed
- Nginx web server must be installed
- The domain name must be directed to the server
1. Preparing the NestJS Application
NestJS CLI installation
npm install -g @nestjs/cli
- This command installs the NestJS command line tool.
Creating a new project
nest new backend-app
- This command creates a new NestJS project structure.
- Enter the project directory and start the application.
cd backend-app
npm run start
- This command runs the application on localhost:3000.
2. Running the Application with PM2
- PM2 manages Node.js applications in the background.
PM2 setup
npm install -g pm2
- This command installs the PM2 process manager.
Starting the application with PM2
pm2 start npm --name "backend-app" -- start
- This command makes the NestJS application always running.
- For automatic startup on server reboots:
pm2 startup
pm2 save
- These commands make the PM2 configuration permanent.
3. Nginx Reverse Proxy Configuration
- Nginx routes incoming requests to the NestJS application.
Firewall settings
sudo ufw allow 'Nginx Full'
- This command allows HTTP and HTTPS traffic.
Nginx configuration file
sudo nano /etc/nginx/sites-available/api.ornek.com
server {
server_name api.ornek.com www.api.ornek.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 routes requests to the NestJS application.
- Activating the site
sudo ln -s /etc/nginx/sites-available/api.ornek.com /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default
- These commands activate the new site.
- Restarting Nginx
sudo nginx -t
sudo systemctl restart nginx
- These commands test and apply the configuration.
4. Adding HTTPS (SSL) – Let's Encrypt
- HTTPS is mandatory for production environment.
Certbot installation
sudo apt install certbot python3-certbot-nginx
- This command installs the SSL certificate manager.
Get a certificate
sudo certbot --nginx -d api.ornek.com -d www.api.ornek.com
- This command creates an SSL certificate for the domain.
- Certificates are automatically renewed.
Frequently Asked Questions
1. Why is a reverse proxy used? Backend closes the application to direct external access.
2. What does PM2 do? It ensures continuous operation of the application.
3. Why is NestJS preferred? It offers modular structure and TypeScript support.
4. Is SSL mandatory? It is absolutely necessary in production environments.
Result
With this guide, you have published your NestJS application on Ubuntu VPS. Thanks to Nginx and PM2, a safe and stable structure was established. Communication security is ensured with HTTPS.
You can easily implement this architecture in the GenixNode infrastructure.

