What is a reverse proxy and how does Nginx act as one?
Understand what a reverse proxy is, how it differs from a forward proxy, and how Nginx uses proxy_pass and upstreams for load balancing and security.
Expected Interview Answer
A reverse proxy is a server that sits in front of one or more backend servers, receiving client requests and forwarding them to the appropriate backend, then returning the response — so clients only ever talk to the proxy, not the real servers behind it.
Unlike a forward proxy that represents the client, a reverse proxy represents the servers. Nginx acts as one using the proxy_pass directive inside a location block, optionally defining an upstream group of backends for load balancing. In this role Nginx can terminate TLS, cache responses, compress output, buffer slow clients, rewrite headers, and hide the backend's identity and topology, improving security, scalability, and performance.
- Hides backend servers and centralizes entry
- Enables load balancing across multiple backends
- Offloads TLS termination and caching
- Buffers slow clients away from application servers
- Adds a security and header-control chokepoint
AI Mentor Explanation
A reverse proxy is like the team captain fielding every question from the umpire and press on behalf of the players. Outsiders speak only to the captain, who relays messages to the right teammate and returns the answer, so the squad stays organized and individual players are shielded from the crowd.
Step-by-Step Explanation
Step 1
Client hits the proxy
The browser connects to Nginx, believing it is the actual application server.
Step 2
Nginx matches a location
A location block with proxy_pass decides which upstream backend should handle the request.
Step 3
Request is forwarded
Nginx opens a connection to the chosen backend and passes along the request with adjusted headers.
Step 4
Backend responds
The upstream server processes the request and returns a response to Nginx, which may cache or compress it.
Step 5
Proxy returns the result
Nginx relays the response to the client, keeping the backend's address and topology hidden.
What Interviewer Expects
- Defines a reverse proxy as fronting the servers, not the client
- Contrasts it clearly with a forward proxy
- Names proxy_pass and upstream in Nginx config
- Lists real benefits: load balancing, TLS, caching, security
- Explains header handling like X-Real-IP or Host
Common Mistakes
- Confusing a reverse proxy with a forward proxy
- Forgetting to set forwarding headers like Host and X-Real-IP
- Thinking a reverse proxy exposes backend addresses to clients
- Assuming proxy_pass alone provides load balancing without an upstream block
Best Answer (HR Friendly)
“A reverse proxy is a middleman server that stands in front of your real servers, taking all incoming requests and passing them to the right one behind the scenes. Nginx does this to balance traffic, add security, and keep the backend servers hidden and protected.”
Code Example
upstream app_servers {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Follow-up Questions
- How does a reverse proxy differ from a forward proxy?
- How do you configure load balancing across upstreams in Nginx?
- Why are X-Real-IP and X-Forwarded-For headers important?
- How does response caching work in an Nginx reverse proxy?
- How would you set up TLS termination at the proxy?
MCQ Practice
1. A reverse proxy primarily represents:
A reverse proxy sits in front of and represents the backend servers, unlike a forward proxy which represents the client.
2. Which Nginx directive forwards a request to a backend?
proxy_pass inside a location block sends the request to a defined backend or upstream group.
3. Which header preserves the original client IP through the proxy?
Setting X-Real-IP (or X-Forwarded-For) lets the backend see the client's original IP address behind the proxy.
Flash Cards
What is a reverse proxy? — A server in front of backends that receives client requests and forwards them, hiding the real servers.
Reverse vs forward proxy? — A reverse proxy represents the servers; a forward proxy represents the client.
Which Nginx directive enables it? — proxy_pass in a location block, often with an upstream group for load balancing.
Why set X-Forwarded-For? — So the backend can see the original client IP instead of the proxy's IP.