Skip to content

๐Ÿš€ END-TO-END: NGINX REVERSE PROXY TUTORIAL


โœ… 1. What is a Reverse Proxy (Easy Explanation)

A reverse proxy sits in front of your backend app and forwards requests.

Flow:

Client โ†’ NGINX โ†’ Backend Server (Node/Golang/Python)

NGINX handles:

  • Routing

  • SSL/TLS

  • Load balancing

  • Caching

  • Security

  • Rate limiting

Your backend only handles business logic.


โœ… 2. Example Setup

We assume:


๐Ÿ“Œ 3. Install NGINX

Ubuntu / Debian:

sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

๐Ÿ“ 4. Create Reverse Proxy Config

Create a new NGINX server block:

sudo nano /etc/nginx/sites-available/myapp

Paste this:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Replace example.com with your domain or IP.


๐Ÿ”Œ 5. Enable the Reverse Proxy

Enable config:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

Remove default:

sudo rm /etc/nginx/sites-enabled/default

Test config:

sudo nginx -t

Reload:

sudo systemctl reload nginx

๐Ÿ”ฅ Now your backend is live!

Visit:

๐Ÿ‘‰ http://your-domain
NGINX โ†’ Forwards โ†’ Backend server


โšก 6. Add SSL (HTTPS) Automatically

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Run:

sudo certbot --nginx -d example.com

Certbot will:

โœ” Install SSL
โœ” Configure HTTPS
โœ” Redirect HTTP โ†’ HTTPS


๐Ÿ“Œ 7. Optional: Proxy for Specific Paths

Example:

location /api/ {
    proxy_pass http://127.0.0.1:3000/;
}

location /auth/ {
    proxy_pass http://127.0.0.1:4000/;
}

๐Ÿงช 8. Optional: Reverse Proxy for WebSocket (Real-Time apps)

Use this if your backend uses WebSocket:

location /ws/ {
    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

๐Ÿ“Š 9. Logs for Debugging

Access logs:

tail -f /var/log/nginx/access.log

Error logs:

tail -f /var/log/nginx/error.log

๐Ÿ›ก 10. Bonus: Improve Reverse Proxy Performance

Add to nginx.conf โ†’ inside http {}:

proxy_read_timeout 60s;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;

client_max_body_size 20M;

๐ŸŽ‰ Reverse Proxy Setup Completed!

You now have:

โœ” NGINX reverse proxy
โœ” Backend linked
โœ” SSL enabled
โœ” Path-based routing
โœ” WebSocket support
โœ” Optimized configuration