Photo by Olga Subach on Unsplash

Nginx as Reverse Proxy for Docker Containers

Joel Garcia Jr
3 min readNov 11, 2020

--

1.What is this?

How to use NGINX Docker Container as a Reverse Proxy to another applications running in others Dockers containers.

This article is related to this Github Repository.

2.What is a Reverse Proxy?

Definition from NGINX page:

A proxy server is a go‑between or intermediary server that forwards requests for content from multiple clients to different servers across the Internet. A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server. A reverse proxy provides an additional level of abstraction and control to ensure the smooth flow of network traffic between clients and servers.

Common uses for a reverse proxy server include:

  • Load balancing — A reverse proxy server can act as a “traffic cop,” sitting in front of your backend servers and distributing client requests across a group of servers in a manner that maximizes speed and capacity utilization while ensuring no one server is overloaded, which can degrade performance. If a server goes down, the load balancer redirects traffic to the remaining online servers.
  • Web acceleration — Reverse proxies can compress inbound and outbound data, as well as cache commonly requested content, both of which speed up the flow of traffic between clients and servers. They can also perform additional tasks such as SSL encryption to take load off of your web servers, thereby boosting their performance.
  • Security and anonymity — By intercepting requests headed for your backend servers, a reverse proxy server protects their identities and acts as an additional defense against security attacks. It also ensures that multiple servers can be accessed from a single record locator or URL regardless of the structure of your local area network.

3.Containers

  1. Nodejs DCApp running at port 8191 and exposing an endpoint which receives a hero name and returns info about it.
  2. Nodejs Marvel App running at port 8192 and exposing an endpoint which receives a hero name and returns info about it.
  3. NGINX Container working as a reverse proxy listening at port 8080.

4. How it works

Nginx will work as a reverse proxy for every request, so, instead of make a request to http://localhost:8191/get-hero/ or http://localhost:8192/get-hero, it is possible to make a direct request to http://localhost:8080/marvel/get-heroor http://localhost:8080/dc/get-hero . Nginx will intercept the request and make a proxy_pass to the specific container.

5. How to run

  1. Git clone this repository
  1. Access the directory
  2. run docker-compose up -d
  3. Make a POST request for http://localhost:8080/marvel/get-hero or http://localhost:8080/dc/get-hero with the JSON similar to this: { "superhero":"spider" }

6. What you should see

A response similar to this: [ { "superhero": "Spider Man", "publisher": "Marvel Comics", "alter_ego": "Peter Parker", "first_appearance": "Amazing Fantasy #15", "characters": "Peter Parker" } ]

--

--