NGINX 101 Baby
Kb Bohara

Kb Bohara @thekbbohara

About: FullStack web2 and 3 developer

Location:
Nepal,Kathmandu
Joined:
Jul 28, 2023

NGINX 101 Baby

Publish Date: Jan 30
0 0

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
Enter fullscreen mode Exit fullscreen mode

Check if it’s running:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

🎉 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
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

Test and reload NGINX:

sudo nginx -t
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Add:

server {
    listen 80;
    server_name be.example.com;

    location / {
        proxy_pass http://localhost:8080;
    }
}
Enter fullscreen mode Exit fullscreen mode

Enable it:

sudo ln -s /etc/nginx/sites-available/be.example.com /etc/nginx/sites-enabled/
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

5️⃣ Enable HTTPS (SSL) for Security 🔒

Install Let’s Encrypt (Certbot):

sudo apt install certbot python3-certbot-nginx -y
Enter fullscreen mode Exit fullscreen mode

Generate an SSL certificate:

sudo certbot --nginx -d demo.example.com -d be.example.com
Enter fullscreen mode Exit fullscreen mode

This automatically configures HTTPS and redirects HTTP to HTTPS.


6️⃣ Managing NGINX

Check status:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Restart NGINX:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

View logs:

sudo tail -f /var/log/nginx/error.log
Enter fullscreen mode Exit fullscreen mode

🚀 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! 🚀🔥

Comments 0 total

    Add comment