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:
-
Their request first goes to Nginx (the middleman).
-
Nginx forwards it to the correct backend server (Node.js, PHP, etc.).
-
The backend sends the response to Nginx.
-
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)
-
User types
example.com/api/users
in browser. -
Request goes to Nginx.
-
Nginx sees
/api
and forwards it tohttp://localhost:3000
. -
Backend sends JSON data to Nginx.
-
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