Nginx as reverse proxy on docker

About nginx

Nginx is an open source web server like apache. Even though nginx and apache can be used as web servers, their functionalities and architectures are different. Following are some main functionalities of nginx.

  1. Web serving
  2. Load balancing
  3. Reverse proxying

Whit this post I’m gonna introduce how to use nginx as a reverse proxy. In here all the nginx related configurations done via docker.

About reverse proxy

Reverse proxy is an intermediate server which takes client request and forward them to one or more backend servers and forward the server responses to clients.

nginx reverse proxy

There are various advantages of serving web application in this way(with nginx)

  1. Provide load balancing since nginx can evenly distribute request to different backend servers
  2. Provide more security since nginx reverse proxy can hide the identity of the backend servers.
  3. Better performance since nginx can server static contents(such as images, css etc) fast
  4. Easy to audit and logging since nginx exposes single point(port 80 and 443) to the outside world

Reverse proxy(single domain)

nginx with web app
“I have a web app which written with scala and running on docker. I want to serve this app with nginx reverse proxy. I have purchased a domain(www.bankz.com) which needs to serve this web app.
All the requests comes to that domain first come to nginx, nginx will forward them to my dockerized scala web app”

Following are the steps to implement and configure this application

1. Nginx config

Following is a simplest nginx config file

With this configuration nginx exposes port 80 to outside, all the request comes to the domain bankz.com forwarded to my dockerized web app which runs on http://web:3000(in here web is the container name of my dockrized web app. I’m using linked containers to communicate between nginx and web app)

2. Dockrize nginx

I want to use dockrized nginx in this scenario(since all my applications are running with docker). Following is the docker file for my nginx server

Main thing to notice here is, I’m using customized niginx config file nginx.conf which configure serving the web application with nginx

3. Docker compose

I’m running this application with docker compose. Following is a simple docker compose file

Main thing to notice here is links defines in nginx. By linking web container to nginx docker adds a host entry for web to the /etc/hosts of the nginx container.

4. How to run

docker-compose up -d will up and run these two containers. If you go to thewww.bankz.com it should serve the web application.

With this setup I need to add the www.bankz.com domain to my DNS server(with is the ip of the machine which running my nginx container).

If you want to test it on your local environment(with out DNS) you can add a host entry to /etc/hosts file like below

192.168.59.103 www.bankz.com

In here 192.168.59.103 is my docker host ip, since I'm running docker with boot2docker on OSX. If you running docker without boot2docker you can simply give the ip of the machine which nginx container is running.

Reverse proxy(multiple domain)

With reverse proxy we can serve multiple web applications(with different domains) on a single server which has a public IP address.

Following is real scenario that I have used reverse proxy to serve multiple domains with nginx.

I have two web apps which written with scala and golang. They are running on docker. I want to serve this apps with nginx reverse proxy. I have purchased two domains(rahasak.com, scorelab.org) for serve this web apps.
All the requests comes to these domains first comes to nginx. Nginx will forward them to corresponding dockerized web app.

Following are the steps to implement and configure this application

1. Nginx configs

In this scenario I need to add two different config files for each domains.

Following is the config file for rahasak.com

With this config all the requests coming to rahasak.com (port 80) forward to rahasakweb:8080 (rahasakweb is the container name of my dockerize scala web app).

Following is the config file for scorelab.org

With this config all the requests coming to scorelab.org (port 80) forward to studentportal:3000 (studentportal is the container name of my dockerize golang web app).

2. Dockrize nginx

Then I need to dockerize the nginx with the above two config files. Following is the Dockerfile

In here I’m adding config files for sites-available in nginx.

3. Docker compose

4. How to run

I’m running this setup on aws. docker-compose up -d will up and run all containers. rahasak.com will serve the scala web application and scorelab.org will serve the golang web application

Reference

  1. http://www.yannmoisan.com/docker.html
  2. https://www.keycdn.com/support/nginx-reverse-proxy/
  3. https://www.digitalocean.com/community/tutorials/apache-vs-nginx-practical-considerations
Like what you read? Give λ.eranga a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.