Nginx as reverse proxy on docker

Happy devOps

(λx.x)eranga
Effectz.AI
3 min readJun 23, 2017

--

Background

In this post I’m gonna discuss about using nginx as a reverse proxy without SSL. In another post I have discussed about using nginx as a reverse proxy with enabling SSL. All the source codes which related to this post available in gitlab. Please clone the repo and continue the post.

About ginx

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

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.

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.

Scenario

I have two domains that needs to be serve through nginx(without SSL). Architecture of the system show in following diagram. Requests come to http://lekana.com:80 redirects to http://lekana-api:7654 and requests come to http://siddhi.com:80 redirects to http://siddhi-api:7655 via nginx. siddhi-api and lekana-api are simple REST APIs which built with golang.

Nginx config

I have created nginx config files to serve lekana and siddhi domains. These config file defines the serving port, domain names, and redirecting hosts. Following is the config file for lekana domain. With this config all the requests coming to lekana.com (port 80) forward to lekana-api:7654 (lekana-api is the container name of my dockerize app).

Following is the config file for siddhi domain. With this config all the requests coming to siddhi.com (port 80) forward to siddhi-api:7655 (siddhi-api is the container name of my dockerize app).

Dockerize nginx

Then I have dockerized the nginx. Following is the nginx Dockerfile. It adds config files into sites-available directory in nginx.

Deploy services

I have three services nginx, lekana-api and siddhi-api. Following is the docker-compose.yaml file that I have used to run these services. I have used linked containers to communicate between nginx and API services.

Following is the way to start and run these services via docker-compose. If want you can start all the services at once by docker-compose up -d command.

Test services

To test the services in local environment I had to define lekana.com and siddhi.com domains on /etc/hosts file with my local machine ip. Then the requests comes to those domains redirects to my local machine(in production environment I setup the domains with DNS server). Following are the entries I have added to /etc/hosts file.

I have tested the lekana-api and siddhi-api via curl. Following is the way I have consumed the APIs.

Reference

  1. https://medium.com/rahasak/set-up-ssl-certificates-on-nginx-c51f7dc00272
  2. http://www.yannmoisan.com/docker.html
  3. https://www.keycdn.com/support/nginx-reverse-proxy/
  4. https://www.digitalocean.com/community/tutorials/apache-vs-nginx-practical-considerations

--

--