Nginx Reverse Proxy With Docker

Louis Aldorio
4 min readAug 29, 2021

--

https://www.nginx.com/

Today, I’m going to teach you how to implement the reverse proxy with nginx and docker. We are going to use docker to containerize our very simple application and handle the deployment of the application.

Let’s say you want to have many applications or many seperated services as API, but you want it to be accessed with only one domain.

Example:

Pre-requisites:

  • Basic knowledge of docker(Containerization)
  • knowledge of compose (docker-compose)
  • Docker daemon installed on your local machine(https://www.docker.com/get-started)
  • Basic knowledge of GO Programming Language.

By using reverse proxy we will not expose our application container port to the host, so we can’t directly bind our services of application port to the host(will be localhost in this tutorial).

We will only expose the nginx container port which is port 80 to the host, all traffic will be transmitted through the nginx container(reverse proxy) before reaching the application level. So, it’s safer that we have isolated the application level from outer world.

repository: https://github.com/LouisAldorio/nginx-reverse-proxy

So let’s get started by initilizing the project we are going to work on.

mkdir nginx-reverse-proxy
cd nginx-reverse-proxy
go mod init myapp

create 3 service folders and each folder contain the main.go file and a Dockerfile that will be used to containerize the application to be an image.

├── service-one          
│ ├── main.go
│ └── Dockerfile
├── service-two
│ ├── main.go
│ └── Dockerfile
├── service-three
│ ├── main.go
│ └── Dockerfile
└── go.mod

All docker file will contain the same content, because it’s used to dockerize our GO application to be an image that will be run as a container.

let’s write the simple API, that will return a simple string stating from what service the response come from.

And continue to service two and three.

We have finally finish writing the application and the containerization Dockerfile.

Let’s write a simple .yaml file that will be used for the docker-compose process.

Create a new file named docker-compose.yaml

├── docker-compose.yaml
├── service-one
│ ├── main.go
│ └── Dockerfile
├── service-two
│ ├── main.go
│ └── Dockerfile
├── service-three
│ ├── main.go
│ └── Dockerfile
└── go.mod

Copy and paste the following yaml script to the compose file.

the compose yaml script helps us to build and deploy all the services including the nginx container with one simple docker command.

If you pay attention to the compose file. you will see that we do mounting volumes. let’s create the file we are going to bind mount to the nginx container which are nginx.conf file and access.log.

Explanation:

  • nginx.conf, this file will be used to write the nginx configuration and determine how nginx would behave and do it’s job properly.
  • access.log, this file will be the file where nginx will write all traffic logs that happen. Like the request that come from client, every request to the webserver will be logged here.
├── nginx.conf
├── access.log
├── docker-compose.yaml
├── service-one
│ ├── main.go
│ └── Dockerfile
├── service-two
│ ├── main.go
│ └── Dockerfile
├── service-three
│ ├── main.go
│ └── Dockerfile
└── go.mod

Copy and paste the following script to the nginx.conf.

Now everything is set, let’s wake our docker up. I’m using the WSL with Ubuntu distro.

sudo service docker start

Spin up the services and the nginx container

docker-compose up -ddocker container ls

Explanation:

  • -d, for detach mode, so it will run the container in the background
  • docker container ls”, list out the current running container
running container list

Now all services are running including the nginx, Open your browser and access :

  • localhost , resulting with nginx default html page.
  • localhost/one
service one
  • localhost/two
service two
  • localhost/three
service three

Finally , we have successfully implement the nginx rever proxy with docker, easy right?, we do not have to install nginx locally with our machine.

Thankyou! :)

Stay tuned for more tutorial form me!

--

--

Louis Aldorio

I'm from Indonesia, Living in Medan, Currently working as Golang Backend Developer. Here is my site: https://louisaldorio.site