How does Nginx serve as a web server versus a proxy?
Learn how Nginx works as a web server serving static files versus a reverse proxy forwarding requests to backends, with config examples and interview questions.
Expected Interview Answer
As a web server, Nginx directly serves static files and content from its own file system to clients; as a proxy, it forwards client requests to backend servers and returns their responses, often adding load balancing, caching, TLS termination, and buffering.
In web-server mode, a location block maps a URL to files on disk via the root or alias directive, and Nginx handles the request end to end. In reverse-proxy mode, proxy_pass sends the request to an upstream application server (Node, Python, PHP-FPM, etc.), and Nginx sits in front to distribute traffic, cache responses, terminate SSL, and shield backends. The same Nginx instance commonly does both: serving static assets itself while proxying dynamic routes to application servers.
- Serves static content extremely fast
- Shields and load-balances backend servers as a proxy
- Centralizes TLS termination and caching
- Lets one instance mix static and dynamic handling
- Improves security by hiding backend topology
AI Mentor Explanation
As a web server, Nginx is like a groundsman who directly hands you the equipment he keeps in his own store. As a proxy, he is like the team manager who takes your request and passes it to the right specialist coach, then relays their answer back, deciding which coach is free and screening who reaches the players.
Step-by-Step Explanation
Step 1
Web server: map URL to files
A location block uses root or alias to serve static files directly from disk.
Step 2
Web server: handle end to end
Nginx reads the file and returns it to the client with no backend involved.
Step 3
Proxy: forward with proxy_pass
proxy_pass sends the request to an upstream application server.
Step 4
Proxy: add value
Nginx can load balance, cache, terminate TLS, and buffer between client and backend.
Step 5
Combine both
One config serves static assets itself while proxying dynamic routes to app servers.
What Interviewer Expects
- Web server serves static files from disk directly
- Proxy forwards requests to backend servers via proxy_pass
- Reverse proxy adds load balancing, caching and TLS termination
- Awareness that one instance can do both roles
- Understanding of root/alias versus upstream/proxy_pass
- Security benefit of hiding backends behind the proxy
Common Mistakes
- Confusing a reverse proxy with a forward proxy
- Using proxy_pass when static file serving would be faster
- Mixing up the root and alias directives
- Forgetting to set proxy headers like Host and X-Forwarded-For
- Assuming Nginx must choose only one role
Best Answer (HR Friendly)
“As a web server, Nginx hands visitors files it stores itself, like images and pages. As a proxy, it acts as a middleman that passes requests on to other application servers and returns their answers, often adding load balancing, caching and security along the way. The same Nginx can do both at once.”
Code Example
server {
listen 80;
server_name example.com;
# Web server: serve static files directly
location /static/ {
root /var/www;
}
# Reverse proxy: forward dynamic routes to the app
location /api/ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}Follow-up Questions
- What is the difference between a forward proxy and a reverse proxy?
- How does root differ from alias in a location block?
- Which proxy headers should you set and why?
- How does Nginx load balance across multiple upstreams?
- Why terminate TLS at the proxy instead of the backend?
MCQ Practice
1. Which directive makes Nginx forward a request to a backend server?
proxy_pass sends the request to an upstream server, making Nginx act as a reverse proxy.
2. As a plain web server, Nginx primarily serves what?
In web-server mode Nginx maps URLs to static files via root or alias and serves them directly.
3. A reverse proxy sits in front of backends mainly to?
A reverse proxy distributes traffic, caches responses, terminates TLS and hides backend servers.
Flash Cards
Nginx as web server? — Serves static files directly from disk using root or alias.
Nginx as reverse proxy? — Forwards requests to backends via proxy_pass and returns their responses.
Forward vs reverse proxy? — Forward proxies clients outbound; reverse proxies sit in front of servers.
Can Nginx do both? — Yes; one config serves static assets and proxies dynamic routes.