What is Reverse Proxy in Nginx: Simple Explanation and Example
reverse proxy in nginx is a server that receives client requests and forwards them to one or more backend servers. It acts like a middleman, hiding the backend servers from clients and managing traffic, security, and load balancing.How It Works
Imagine you have a receptionist at an office building who takes visitors' requests and directs them to the right department inside. A reverse proxy in Nginx works similarly. It listens for requests from users on the internet and then forwards those requests to one or more internal servers that actually handle the work.
This setup helps keep the internal servers hidden from direct access, improving security. It also allows Nginx to manage traffic by distributing requests evenly, caching responses to speed up delivery, and handling encryption.
Example
This example shows a simple Nginx reverse proxy configuration that forwards requests to a backend server running on localhost port 3000.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost: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;
}
}When to Use
Use a reverse proxy in Nginx when you want to:
- Protect backend servers by hiding them from direct internet access.
- Distribute incoming traffic across multiple servers to balance load.
- Cache responses to improve speed and reduce backend load.
- Enable SSL/TLS encryption at the proxy level.
- Serve multiple applications from one public IP or domain.
For example, a company might use Nginx as a reverse proxy to route requests to different web apps running on separate servers, all while providing a single secure entry point.
Key Points
- A reverse proxy forwards client requests to backend servers.
- It improves security by hiding backend details.
- It can balance load and cache content.
- Nginx is a popular tool for reverse proxying.
- Configuration involves the
proxy_passdirective.