My First Docker Reverse Proxy with Nginx
I built a small Docker setup with Nginx in front of a web app, broke it with localhost, and learned to debug the network path one layer at a time.


The first time I put Nginx in front of a Docker app, I expected the configuration file to be the difficult part. It was only a few lines. The confusing part was understanding where localhost pointed at each stage. My browser, the Nginx container, and the web container were all looking at different network spaces, and a tiny assumption produced a very loud 502 Bad Gateway.
The small setup I actually needed
I was working with a simple application that already ran correctly on its own port. Opening the app directly from the host worked, so I initially wondered whether adding Nginx was unnecessary. I wanted one public entry point, though, with the application kept behind it. That would give me a cleaner place for HTTPS later and avoid exposing every service directly to the machine.
I reduced the setup to two services in Docker Compose: nginx and web. The web service ran the application on port 3000. Nginx listened on port 80 and forwarded requests to it. Both services joined the same Compose network automatically, which meant Nginx could reach the application using the service name web.
The important distinction was exposure. I mapped port 80 from Nginx to the host, but I did not need to publish the web container's port for outside traffic. The application still listened on 3000 inside Docker. That kept the public path simple:
Browser to host port 80, then Nginx to web:3000.
That sentence became more useful than the configuration itself. Whenever something failed, I could ask which part of that path had actually been tested.
The localhost mistake
My first proxy configuration used localhost as the upstream address. It looked reasonable because the application was available at localhost:3000 from my own machine. Nginx returned a 502, though, and I spent too long looking at proxy headers before checking the basic network assumption.
Inside the Nginx container, localhost meant the Nginx container. It did not mean the host machine and it did not mean the web container. There was nothing listening on port 3000 inside that container, so Nginx was trying to connect to itself and failing.
Changing the upstream to the Compose service name fixed the main problem. The proxy target became web:3000. Docker's internal DNS resolved web to the right container because both services were attached to the same network. I did not need to find a container IP, and I did not want to use one. Container IPs can change when services are recreated, while the service name remains the stable part of the setup.
There was another small annoyance: I had briefly typed the service name incorrectly in the proxy target. The error looked like a general upstream failure, not a helpful spelling correction. That sent me back to the application code and then to the headers before I finally compared the Compose file and Nginx configuration character by character.
Testing the path instead of guessing
Once the configuration looked right, I stopped refreshing the browser and tested the system in layers. First I checked whether the application responded from inside its own container. That separated an application startup problem from a proxy problem.
Next, I opened a shell in the Nginx container and used curl against the internal address web:3000. This was the most useful test because it matched the connection Nginx was supposed to make. If that request failed, changing server names, headers, or browser settings would not help.
After the internal request worked, I tested the mapped Nginx port from the host. The public request traveled through the full route, so a failure there could point to the port mapping, Nginx listener, or proxy rule. I also checked the logs with docker compose logs nginx. Nginx usually gave me enough detail to see whether it could resolve the upstream, connect to it, or receive a response.
That order mattered. I had previously treated the browser as the only test, which made every failure look identical. A blank page, a refused connection, and a 502 do not represent the same problem. Testing from the right location made the boundary visible.
What I kept simple on purpose
I was tempted to add HTTPS immediately. I also looked at caching rules, compression, extra security headers, and automatic certificate renewal. Those are useful pieces, but adding them before the plain HTTP route worked would have made the first failure harder to identify.
The initial Nginx configuration only needed a server block, a listen directive, and a proxy_pass pointing to the internal service. I forwarded the common host and client headers, but I did not turn the file into a collection of rules copied from a production template. That restraint made it easier to see what each line was doing.
I also learned that exposing the application port directly can hide mistakes. If port 3000 remains available on the host, I can accidentally bypass Nginx and believe the proxy is working. For this small setup, only Nginx needed a host port. The web service stayed reachable through the Docker network instead.
There are cases where an application needs a different internal port, a separate worker, WebSockets, or a custom network. This guide does not remove those decisions. It gives me a working baseline before those decisions are added.
The final setup was not impressive. That was the point. The browser reached Nginx, Nginx resolved web through Docker's network, and the application answered on its internal port. When I later experiment with TLS or deployment automation, I have a known-good route to protect rather than a pile of settings that might be hiding the original problem.

Rafiqul Islam Rabbi
Full-Stack & E-Commerce Architect building high-converting Shopify stores, WordPress platforms, and Next.js web applications.