Reverse Proxy

1. Simple Meaning

A Reverse Proxy is like a middleman between the internet user (client) and your actual application/server.

When someone visits your website:

  1. Their request first goes to Nginx (the middleman).

  2. Nginx forwards it to the correct backend server (Node.js, PHP, etc.).

  3. The backend sends the response to Nginx.

  4. Nginx sends it back to the user.

The user never directly talks to your backend server — only to Nginx.


2. Real-Life Example

Imagine:

  • You own a restaurant 🍽️

  • There’s a reception counter at the entrance

  • Customers never directly enter the kitchen

  • They tell the receptionist (reverse proxy) their order

  • The receptionist tells the chef (backend server)

  • The chef prepares the food and gives it to the receptionist

  • The receptionist gives the food to the customer

Here:

  • Customer = User’s browser

  • Receptionist = Nginx Reverse Proxy

  • Chef = Your backend application


3. Why Use a Reverse Proxy?

  • Hide your kitchen → Nobody sees your actual backend server or its IP address.

  • Handle multiple chefs → If you have many backend servers, the receptionist can send customers to the less busy one.

  • Extra services → Receptionist can give menus (static files) directly without disturbing the chef.

  • Security → Receptionist checks if the customer is allowed before passing the order.


4. Beginner-Friendly Example with Nginx

You have:

  • Frontend HTML/var/www/html

  • Backend Node.js → running on port 3000

You want:

  • example.com → show frontend

  • example.com/api → talk to backend

Nginx config:

server {
    listen 80;
    server_name example.com;

    # Serve website files directly
    location / {
        root /var/www/html;
        index index.html;
    }

    # Reverse proxy for backend
    location /api {
        proxy_pass http://localhost:3000;
    }
}

5. How it Works (Step-by-Step)

  1. User types example.com/api/users in browser.

  2. Request goes to Nginx.

  3. Nginx sees /api and forwards it to http://localhost:3000.

  4. Backend sends JSON data to Nginx.

  5. Nginx sends data to user.


6. Extra Things Nginx Can Do in Reverse Proxy

  • HTTPS (secure connection) → So backend only needs HTTP.

  • Caching → Store common responses to reply faster.

  • Load balancing → Distribute requests between multiple backend servers.

No comments:

Post a Comment

What is slice() in JavaScript

What is slice() ? slice() is a method used to copy a portion of an array or string without changing the original . Think of it like cut...