Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Fundamentals › Networking

Proxy servers

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)

Concepts in this lab

Proxy server
A proxy server is an intermediate server that stands between the side that sends requests (the client) and the real server, passes the request it receives on to another server, and returns the response to the client. The client connects to the proxy server, and the proxy server makes a separate connection to the real server. So the peer that the real server sees is the proxy server, not the client. nginx in front of Spring Boot is a typical example. Section 3.7 of RFC 9110 divides such intermediaries into a proxy, which the client chooses by its configuration to go through, and a gateway (reverse proxy), which looks like a server and passes requests on to the servers behind it. If the proxy server goes down, a client that connects to its address does not get a response, even though the servers behind it are alive.
Reverse proxy
Why it exists — If slow clients connect straight to an application server, the request processing threads stay tied up with those connections while the clients slowly upload a large request body or slowly take a response. A reverse proxy such as Nginx, in its default configuration, buffers requests and responses and exchanges data quickly with the backend server, so the backend server's threads are not tied up for long by slow connections.
Where it's used today — It is used to split static file requests and /api requests by path and send them to different servers, or to reject too-large uploads at the front with Nginx's client_max_body_size. In Kubernetes, an Ingress controller takes this role.
A server that receives client requests on behalf of internal servers, forwards them to those servers, and returns their responses to the client. The client does not know the actual servers directly. Its roles include distributing requests across several servers, handling TLS, caching, and protecting the internal servers.
nginx proxy_pass and proxy_set_header
Where it's used today — When you put nginx in front of Spring Boot, the most common configuration is to put listen 80; (or listen 443 ssl;) in a server block and to write proxy_pass and a few proxy_set_header lines inside location /.
proxy_pass and proxy_set_header are directives written in a location block of an nginx configuration file. proxy_pass http://127.0.0.1:8080; means that a request that comes into that location is passed on to the server at the given address, and nginx makes a new connection to that server for this. proxy_set_header writes a header onto the request that is passed on. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; writes the received X-Forwarded-For followed by a comma and the address of the peer that connected to nginx ($remote_addr), or only that address if nothing was received. proxy_set_header Host $host; passes on the name the client wrote in the request as the Host header. If you do not write it, nginx puts the address written in proxy_pass ($proxy_host) into Host and adds Connection: close (documentation of ngx_http_proxy_module).
X-Forwarded-For
Where it's used today — You read this value to record the real user IP in an access log, or to block requests that come too often from one IP. In nginx, the proxy_set_header setting attaches this header and passes it on.
X-Forwarded-For is a request header in which a proxy writes the address of the original client when it passes a request on. The peer that a server behind a proxy sees is the proxy, so the address of the real client is passed on only through this header. When a request goes through several proxies, each proxy appends the address of whoever connected to it at the end, separated by a comma. It is a request header, so a client can also write any value into it; a server must use only the values added by a trusted proxy that it manages (the values on the right), and must not read it at all if it is not behind a proxy. It is a widely used convention, not a standard, and the standard header that does the same job is Forwarded.
502 Bad Gateway
Where it's used today — When the Spring Boot server behind nginx is stopped or still starting, the browser shows the 502 page of nginx. In that case, first check whether the server behind is running and whether the port written in proxy_pass matches server.port.
502 is a status code used when a server acting as a gateway or proxy receives an invalid response from the inbound server it accessed to fulfill the request (RFC 9110 section 15.6.3). When nginx cannot even connect to the server behind it (the connection is refused), it also returns 502 if there is no next server to pass the request to (nginx source ngx_http_upstream.c). The one that makes and sends this response is the proxy server the client connected to, not the real server behind it. So when a client receives 502, it means the client reached the proxy server it connected to, and the problem is between the proxy server and the server behind it.
server.address
Where it's used today — In a setup where Spring Boot and nginx are on the same computer, server.address=127.0.0.1 keeps outside users from calling Spring Boot directly on port 8080 and lets them come in only through nginx. Blocking that port with a firewall is another way.
server.address is a Spring Boot setting that decides on which network address the embedded server (Tomcat) waits for connections. If you do not write it, it waits on all addresses of the computer (0.0.0.0), and if you write server.address=127.0.0.1, it waits only on 127.0.0.1. 127.0.0.1 is the address a computer uses for itself, so only programs on the same computer can connect to that port, and connections from other computers are refused. The port is set with server.port (default 8080).
Origin server
An origin server is a program that can originate authoritative responses for the requested resource, and section 3.6 of RFC 9110 calls it by this name. The Spring Boot application behind nginx is in this place. A proxy server only passes requests on, so the origin server is the one that produces the content of the response. When a proxy server is present, a client may not connect to this server directly, and this server sees the proxy server as the peer that connected to it.

Try these

Running with the default settings

  1. The user (203.0.113.7) sends a GET /hello request to the address in the code box. The address has no port number, so it connects to port 80 of the server computer 192.0.2.10. What waits on port 80 is nginx (listen 80). The address the user uses does not contain the Spring Boot port, 8080.
  2. nginx receives the request, and following the line proxy_pass http://127.0.0.1:8080, it makes a new connection to port 8080 of the same computer and passes on the same GET /hello. This connection is separate from the one the user made, and nginx is the side that made it. When passing the request on, following the line proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for, nginx writes the address of the peer that connected to it (203.0.113.7) into the X-Forwarded-For header. The line Host $host passes on the name the user wrote in the request (192.0.2.10) as a Host header, and this lab does not use it in its results. The place nginx connected to is 127.0.0.1, so Spring Boot with server.address=127.0.0.1 accepts this connection too.
  3. The controller of Spring Boot runs. request.getRemoteAddr() is 127.0.0.1, because the one that opened the connection to this server is not the user but nginx on the same computer. The user's address 203.0.113.7 is obtained with request.getHeader("X-Forwarded-For"), a value that nginx wrote.
  4. nginx receives the response of Spring Boot and returns it to the user. What the user received is status code 200 and the body "Connected peer: 127.0.0.1, X-Forwarded-For: 203.0.113.7". The user talked only with the nginx it connected to.

Result of this run

Code

# 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;
    }
}

Assumptions of this model

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

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these