The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Networking
What is the difference between calling a server directly and calling it through nginx (a proxy)?
Why it exists — If a server program is exposed directly to the internet, then when you add servers or replace them, the address that users connect to changes too, and work such as handling HTTPS or blocking bad connections has to be done separately in each server program. A reverse proxy stands in front of the servers at one address, receives requests, and passes them on, so users connect to the same address even when the servers behind it change.
Where it's used today — A common setup puts nginx in front of a Spring Boot server and uses the proxy_pass setting of nginx to pass requests received on ports 80 and 443 to Spring Boot on port 8080. Spring Boot then sees nginx as the peer that connected to it, so nginx writes the real user's IP address into the X-Forwarded-For header with proxy_set_header and passes it along. There is also a proxy (a forward proxy) that gathers requests going from inside a company to outside sites in one place and filters them.
A proxy server is a server that stands between the side that sends requests (the user) and the real server and passes requests on for it. This lab uses the most common setup, nginx and Spring Boot, and compares calling the server directly with calling it through nginx. The code box has four parts. The first is the address the user calls. The user is on another computer (203.0.113.7), and 192.0.2.10 is the address of the server computer. If the address has no port number, the request goes to port 80, and if it has :8080, it goes to port 8080. A port number is the number of a door given to each program on one computer. The second is the nginx configuration. listen 80 is the line that receives requests on port 80, proxy_pass is the line that passes the received request on to port 8080 of the same computer (127.0.0.1:8080), and proxy_set_header is the line that writes a header onto the request when it is passed on. The third is application.properties, the configuration file of Spring Boot, and the fourth is the controller that receives the request. The controller returns, as text, the address of the peer that connected to it (request.getRemoteAddr()) and the header X-Forwarded-For. In this lab you change the address that is called, the server.address line, and which programs are running, and see what the user receives and whom Spring Boot sees as the peer.
Checked against real execution · Checked against real execution: the nginx configuration on the screen was put as it is into real nginx 1.27.5 (the nginx:1.27-alpine image), and the HelloController and application.properties on the screen into real Spring Boot 4.1.1 (embedded Tomcat 11.0.24), and twelve combinations were run in Docker with the addresses on the screen as they are (user 203.0.113.7, server computer 192.0.2.10) (the effect of getRemoteAddr(), X-Forwarded-For, and server.address, 502, and connection refused, verify/net/cmp_proxy_basic.js)
# The address the user (another computer, 203.0.113.7) calls
# If no port number is written, http goes to port 80
http://192.0.2.10/hello
# nginx configuration (runs on the server computer 192.0.2.10)
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# application.properties (Spring Boot, runs on the same computer)
server.port=8080
# Accepts only connections that come to 127.0.0.1 (the address this computer uses for itself)
server.address=127.0.0.1
// HelloController.java
// HelloController is a name chosen for this example. @RestController and @GetMapping come from
// Spring, and HttpServletRequest comes from the servlet API (jakarta.servlet).
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(HttpServletRequest request) {
// The address of the peer that opened a connection to this server
String peer = request.getRemoteAddr();
// The X-Forwarded-For header that came with the request (null if absent)
String xff = request.getHeader("X-Forwarded-For");
return "Connected peer: " + peer + ", X-Forwarded-For: " + xff;
}
}
The verification run (verify/net/proxy_basic_probe_nginx.js) ran all twelve combinations with real nginx 1.27.5 and real Spring Boot 4.1.1 (Spring 7.0.9, embedded Tomcat 11.0.24). It ran only inside Docker 29.8.1 on the PC used to write this lab and did not use any external network. The nginx configuration on the screen (the server block) was put into the http block of nginx.conf without changing a single character, and the only additions are the frame that the screen does not show (the events block and the braces of the http block) and the lines that keep records (error_log, log_format, access_log). For Spring Boot, the HelloController on the screen was compiled with JDK 21 with only one package line added, and the lines of application.properties on the screen were written out to a file as they are and the server was started (server.port=8080 as it is, too). The Main class that starts it, and the filter that counts /hello requests and writes down the headers that arrived, were added for the run (they are not on the screen). The addresses are also the same as on the screen. A Docker network was created with the documentation address range 203.0.113.0/24, the container that plays the computer of the user was given 203.0.113.7, and 192.0.2.10 was attached to the container that plays the server computer (it was added on top of the address Docker gave, 203.0.113.10, and on the user side the route to 192.0.2.10 was set to this container). nginx (the nginx:1.27-alpine image) and Spring Boot (Java 21.0.13 of the eclipse-temurin:21-jre image) were started as two containers that share the network of the server computer container, which makes them two programs on one computer, so the 127.0.0.1:8080 of proxy_pass worked as it is. The user called the address on the screen as it is with curl 8.12.1 from another container. So "another computer" in this run is not another machine but another container on the same PC (a space with its own separate network). A program that was stopped was handled by removing its container. Checking with netstat on the server computer, nginx waited on 0.0.0.0:80, and Spring Boot waited only on port 8080 of 127.0.0.1 with server.address=127.0.0.1 (::ffff:127.0.0.1:8080), and on port 8080 of all addresses without that line (:::8080). What was checked against (verify/net/cmp_proxy_basic.js, all 13 cases matched) is what the user received (the status code, the body, or the connection refusal), the getRemoteAddr() and X-Forwarded-For that the controller returned, the number of requests left in the access_log of nginx and their $remote_addr (203.0.113.7), the X-Forwarded-For (203.0.113.7) and Host (192.0.2.10) that arrived at Spring Boot, the number of /hello requests Spring Boot received, and whether the code on the screen is the same text that the run read. In the combinations with Spring Boot stopped, the error_log of nginx got connect() failed (111: Connection refused) while connecting to upstream, and the user received 502 and a page made by nginx. The last line of this page was nginx/1.27.5 (the default of server_tokens is on, so the version number is added). Responses that came through nginx had the header Server: nginx/1.27.5, and the response received directly from Spring Boot had no Server header. nginx 1.27.5 passed the request to the Spring Boot behind it with HTTP/1.0 and Connection: close (according to the nginx documentation, the default of proxy_http_version is 1.1 from 1.29.7). Tomcat does not put text such as OK on the status line, so the table shows only the status code 200. What was checked against the documentation is the ngx_http_core_module of nginx.org (the default of listen, server and location, $host, $remote_addr, server_tokens), the ngx_http_proxy_module (proxy_pass; the defaults of proxy_set_header, Host $proxy_host and Connection close; the definition of $proxy_add_x_forwarded_for; proxy_http_version), the description of the settings in the Spring Boot 4.1.1 jar (server.address is the network address the server binds to, and server.port defaults to 8080), and section 15.6.3 (502) of RFC 9110. The user address 203.0.113.7 and the server computer address 192.0.2.10 on the screen are example addresses for documentation. This lab does not cover HTTPS (port 443) and certificates, server.forward-headers-strategy (which makes Spring Boot read X-Forwarded-For and reflect it in getRemoteAddr()) and forged headers, or 504 for slow responses. The first ones are covered in the in-depth lab "Proxies and reverse proxies", splitting among several servers in "Load balancers and scale out", and keeping copies of responses in "CDN".
Previous lab: HTTPS · Next lab: Authentication and authorization
Struck-through text was removed from the first version of the code; highlighted text was added.