NestJS is a powerful Node.js framework for building server-side applications, often deployed on platforms like AWS EC2. To serve a NestJS app securely, you can use Nginx as a reverse proxy to handle incoming requests and Let’s Encrypt to provide free SSL/TLS certificates for HTTPS. This ensures encrypted communication between your app and its users.
Unlike static websites, NestJS apps generate dynamic content, so you don’t need to serve files like index.html. Instead, Nginx forwards requests to your NestJS app running on a local port (e.g., 3000). This guide will walk you through installing Nginx, deploying a NestJS app on an EC2 Ubuntu 20.04 instance, and securing it with Let’s Encrypt, with automatic certificate renewals.
Before starting, ensure you have:
- An Ubuntu 20.04 EC2 instance set up with a non-root user (sudo privileges) and a firewall (UFW)
- A registered domain (e.g., example.com) with an A record in your DNS provider (like Route 53) pointing to your EC2 instance’s public IP. Include www.example.com if desired.
- Node.js and npm installed on your EC2 instance.
- A NestJS app ready to deploy, either cloned from a repository or created locally.
Nginx will act as a reverse proxy, forwarding requests to your NestJS app. Install it on your EC2 instance.
Update the package index:
Install Nginx:
Nginx starts automatically. Verify it’s running:
You should see:
If it’s not running, start it:
Enable Nginx to start on boot:
Allow HTTP traffic through UFW:
Check the firewall:
Expected output includes:
Visit http://your_server_ip to see Nginx’s default page, confirming it’s installed. You don’t need to test http://example.com yet, as we’ll configure that next.
Deploy your NestJS app to run on the EC2 instance. This guide assumes your app listens on port 3000 (NestJS’s default), but you can adjust as needed.
-
Install Node.js (if not already installed):
Verify:
-
Copy Your NestJS App:
-
Clone your app’s repository or upload it to the server. For example:
-
Install dependencies:
-
-
Build the App (if using TypeScript):
-
Start the App:
-
Run it temporarily to test:
-
You should see output indicating the app is listening on http://localhost:3000. Press Ctrl+C to stop for now.
-
For production, use a process manager like PM2 to keep the app running:
-
Save the PM2 process list to restart on reboot:
Follow the outputted command to enable PM2 on boot.
-
-
Test Locally:
-
From the EC2 instance, run:
-
If your app has a root endpoint (e.g., returning “Hello World”), you’ll see the response. If not, check your app’s routes or logs (pm2 logs nestjs-app).
-
You don’t need to create static files (like index.html) because NestJS serves dynamic content, similar to how your OpenProject setup relied on the app’s internal server.
Set up Nginx to forward requests for example.com to your NestJS app on localhost:3000. This is similar to your OpenProject setup, where Nginx proxied to a Docker container.
Create a configuration file:
Add:
This config:
- Listens on port 80 for example.com and www.example.com.
- Proxies requests to http://127.0.0.1:3000, where NestJS runs.
- Sets headers to preserve client information and support WebSockets (if used).
Save and close (Ctrl+O, ENTER, Ctrl+X).
Enable the config:
Test the configuration:
Expect:
Reload Nginx:
Visit http://example.com. If your NestJS app is running, you should see its response (e.g., your API’s root endpoint). If it’s not working, check:
- PM2 status: pm2 status.
- Nginx logs: sudo tail /var/log/nginx/error.log.
- DNS propagation for example.com.
Install Certbot to obtain an SSL certificate from Let’s Encrypt.
Add the Certbot PPA for the latest version:
Press ENTER to confirm.
Install Certbot with the Nginx plugin:
Verify:
Use Certbot’s Nginx plugin to get and install the SSL certificate automatically, similar to your OpenProject setup but leveraging --nginx for simplicity (unlike your --standalone approach).
Run:
Follow the prompts:
- Email: Enter a valid email for renewal notices.
- Terms of Service: Type A to agree.
- EFF Newsletter: Choose Y or N.
- Redirect: Select 2 to redirect HTTP to HTTPS.
Certbot will:
- Obtain a certificate for example.com and www.example.com.
- Modify /etc/nginx/sites-available/nestjs to add SSL settings.
- Set up an HTTP-to-HTTPS redirect.
You’ll see:
Your updated Nginx config will look like:
Visit https://example.com to confirm the padlock icon appears. Test with SSL Labs if desired.
Allow HTTPS traffic:
Verify:
Output includes:
Since you chose redirection, you can remove HTTP:
Let’s Encrypt certificates expire every 90 days, but Certbot automates renewals. Test it:
Look for:
Check the renewal timer:
If inactive, enable it:
Your certificates will renew automatically.