Sub topic 2
What a Reverse Proxy Does and a First Proxied Site
Prerequisites and the request path
This chapter assumes that you can use the Linux command line, edit files with sudo, and understand basic HTTP requests, responses, headers, ports, and IP addresses. You also need Nginx installed and a web application listening on a local port, such as 127.0.0.1:3000.
A reverse proxy is a server that receives requests for an application and forwards those requests to another server. The client connects to the reverse proxy, not directly to the application. Nginx is the reverse proxy in this tutorial.
A forward proxy works in the opposite direction. A client deliberately sends its outbound requests through a forward proxy. The proxy might enforce company policy or hide the client's address from websites. A reverse proxy represents the application to its clients. It can provide one public entry point, TLS encryption, routing, caching, and load balancing.
The basic request path is:
Nginx accepts the public connection, selects a server block, matches a location, and runs the proxy settings in that location. The application then handles the forwarded request and sends its response back through Nginx.
Create the first proxied site
Suppose the application is already running on port 3000. Create a server configuration such as /etc/nginx/sites-available/example.com:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1: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;
}
}The listen directive makes Nginx accept HTTP connections on port 80. The server_name directive identifies the hostname that this block handles. DNS for example.com must point to the Nginx server.
The location / block matches paths beginning at the site root. Its proxy_pass directive sends matching requests to the application. For example, a request for /orders is forwarded to the application as /orders with this form of proxy_pass.
The proxy headers preserve information that would otherwise describe only the Nginx connection:
Hostcontains the hostname requested by the client.X-Real-IPcontains the address of the client that connected to Nginx.X-Forwarded-Forrecords the client address and any earlier proxies.X-Forwarded-Prototells the application whether the client used HTTP or HTTPS.
Your application must be configured to trust forwarded headers only from proxies you control. Otherwise, a client could send a forged header directly to the application if the application is publicly reachable.
Enable the site using the layout provided by your Linux distribution. On Debian-based systems, create a symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
sudo nginx -t && sudo systemctl reload nginxnginx -t checks the configuration before the reload. The && operator reloads Nginx only when the check succeeds. Test both the public hostname and the local application directly. If the local request works but the public request fails, inspect Nginx's error log and confirm that the application is listening on the expected address and port.
Exercise
What is the difference between a forward proxy and a reverse proxy, and which one is Nginx providing in this configuration?
Show answer
A forward proxy represents clients when they access other services. A reverse proxy represents a service when clients access it. Nginx is providing the reverse proxy.