If you’re new to NGINX and need to set up domain forwarding, reverse proxying, and SSL, this guide is for you! Let's break it down into simple steps.
1️⃣ Install & Set Up NGINX
If NGINX isn’t installed yet, run:
sudo apt update && sudo apt install nginx -y
Check if it’s running:
sudo systemctl status nginx
🎉 If you see “active (running)”, you’re good to go!
2️⃣ Configure a Domain to Forward Requests
To forward demo.example.com
to port 3000, edit the NGINX config:
sudo nano /etc/nginx/sites-available/demo.example.com
Add this:
server {
listen 80;
server_name demo.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;
}
}
Save and exit (CTRL + X
, then Y
, then ENTER
).
3️⃣ Enable the Configuration
Activate it with:
sudo ln -s /etc/nginx/sites-available/demo.example.com /etc/nginx/sites-enabled/
Test and reload NGINX:
sudo nginx -t
sudo systemctl reload nginx
4️⃣ Host Multiple Domains on Different Ports
If you want be.example.com
to forward to port 8080, create another config:
sudo nano /etc/nginx/sites-available/be.example.com
Add:
server {
listen 80;
server_name be.example.com;
location / {
proxy_pass http://localhost:8080;
}
}
Enable it:
sudo ln -s /etc/nginx/sites-available/be.example.com /etc/nginx/sites-enabled/
sudo systemctl reload nginx
5️⃣ Enable HTTPS (SSL) for Security 🔒
Install Let’s Encrypt (Certbot):
sudo apt install certbot python3-certbot-nginx -y
Generate an SSL certificate:
sudo certbot --nginx -d demo.example.com -d be.example.com
This automatically configures HTTPS and redirects HTTP to HTTPS.
6️⃣ Managing NGINX
Check status:
sudo systemctl status nginx
Restart NGINX:
sudo systemctl restart nginx
View logs:
sudo tail -f /var/log/nginx/error.log
🚀 That’s It!
You now know how to:
✅ Set up domain forwarding
✅ Use NGINX as a reverse proxy
✅ Host multiple domains on different ports
✅ Secure your site with HTTPS
Go ahead and deploy your Next.js app or backend services like a pro! 💪😎
Let me know if you have any questions! 🚀🔥
Top comments (0)