Nginx Reverse Proxy: How Requests Travel
One request, two connections
A reverse proxy is a server that accepts requests for an application and makes its own requests to the application. Imagine a bookstore with one public entrance and a separate catalog service. Visitors use the entrance; the catalog service handles the data. In this chapter, all addresses belong to an isolated local lab.
Client → 127.0.0.1:18121 (Nginx) → 127.0.0.1:18122 (catalog)Client ← 127.0.0.1:18121 (Nginx) ← 127.0.0.1:18122 (catalog)The frontend is the listener the client connects to. The upstream is the server Nginx contacts on the client’s behalf. These are separate connections, so their ports, protocol settings and failure modes can differ. The app can remain on a private listener.
A first routing rule
This partial server configuration assumes a catalog service on the indicated loopback port. It belongs inside an http block; the complete lab is supplied separately. The proxy module is required.
server { listen 127.0.0.1:18121; server_name lab.test; location /catalog/ { proxy_pass http://127.0.0.1:18122; }}curl --noproxy '*' --max-time 5 -H 'Host: lab.test' http://127.0.0.1:18121/catalog/booksWith the catalog fixture running, the expected response is JSON containing backend=catalog and path=/catalog/books. The browser does not need to know the catalog port. A missing backend can cause a gateway error even though Nginx itself is listening.
Serve locally or delegate
location = /health { return 200 "frontend alive\n"; }location /catalog/ { proxy_pass http://127.0.0.1:18122; }The first route never contacts the app; the second does. A healthy /health therefore does not prove that the catalog works. Choose separate checks for frontend and application health. A reverse proxy alone does not provide authorization, caching or redundancy.
Read the outcome
GET /health → 200 (frontend)GET /catalog/books → 502 (backend refused connection)These are predicted observations, not captured traffic. A 502 indicates a gateway problem, not necessarily a refused connection; use the error log to distinguish causes. A 404 returned by a healthy upstream is a different situation. For application checks, validate the response body as well as status.
Practice: draw both connections and identify which one to investigate if /health works but /catalog/books fails. Next: learn how the location prefix affects the upstream path.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reverse proxy request flow
Choose the best answer based on the lesson and local fixture.
Which endpoint does the client connect to in the diagram?
Who opens the second connection to the app?
A local /health returns 200. What is proven?
The backend is on loopback. What is true?
What should you check for a suspected proxy failure?