Skip to main content

How to install Nginx as a reverse proxy server with Docker

How to install Nginx as a reverse proxy server with Docker


On a single docker host machine, we can run 100’s of containers and each container can be accessed by exposing a port on the host machine and binding it to the docker port.

This is the most standard practice which is used and we use docker run command with -p option to bind docker port with and host machine port. Now if we have to do this with a couple of services this process might work well but if we had to cater a large number of containers, remembering port numbers and managing them could be a hurricane task.

This problem can be dealt by installing Nginx, which is a reverse proxy server and directs the client requests to the appropriate docker container

Installing Nginx Base Image

Nginx Image can be downloaded from docker hub and can be installed by simply using. docker run nginx Nginx Configuration is stored in file /etc/nginx/nginx.conf. This file holds a reference to default.conf
include /etc/nginx/conf.d/*.conf;

Follow the below steps to run a nginx server and have a peek around nginx configuration

  • Run the latest Nginx docker Image using

  • Open the bash console for accessing Nginx configuration

Navigate to /etc/nginx/conf.d and  cat default.conf copy file contents.We will use this file contents in next steps.

Creating our own custom Nginx Image

In this step we will try to modify the base nginx image, with changes to default.conf

Create a simple project in eclipse(File->New ->General-> Project) and create a new file called default.conf in the project directory.
In this file add the location block where

for eg,

where the app1 is the URL and microservice1 is the docker container name and 8080 is the docker port , this info can be found using docker ps-a

While running a docker container make sure that you use — name attribute so the docker container name remains consistent. If no name is given then docker usually assign a random name to container

This is how the default.conf looks like for 2 docker containers named microservice1 and microservice2

Creating Docker Image

Next step is to create a Dockerfile where we will replace the default configuration file default.conf in the nginx base image with our version of default.conf

Create a file called Dockerfile and add below contents. Make sure the file is at same level as default.conf and under project root directory.

Now all we need is to build this docker image. Open terminal/command prompt and navigate to project directory

If above command was suucessful, our own custom nginx image is ready with our configuration.

Running our own Custom Nginx Image

Each Docker Container is a seperate process and is unawaare of other docker container. However docker has –link attribute which can be used to create links between 2 docker containers and make them aware about the existence of other containers.

Before we run our image we need to make sure that the services mentioned in the location block are up and running , in our case microservice1 and microservice2.
docker ps -a

Next we need to link these 2 docker containers with our nginx container using this command

Make sure that you stop the default nginx image, created in Step-1 as it might be running on port 80 and 443

If the above command was successful with no errors we have successfully installed nginx as reverse proxy server and can be tested by opening a browser and accessing

For reference , please see below video.


Related Posts

7 thoughts to “How to install Nginx as a reverse proxy server with Docker”

    1. Do you mean you want to use nginx on virtual domain or forward incoming requests to virtual domain.

      If you want to forward it to virtual domain you need to put URL in proxy pass block.
      Proxy pass

      If you question was related to using Nginx on Virtual domain then all you need is point DNS to the server where nginx image is running. Since we have exposed http port 80 and https port 443 for incoming traffic to nginx. All requests will be forwarded through nginx.

    1. This is what a quick search on internet led me to , could this be answer to your problem ?

      server {
      listen 8001 default_server;
      location / {
      proxy_pass http://web1.local:80;
      proxy_set_header Host web1.local:80;

      server {
      listen 8002 default_server;
      location / {
      proxy_pass http://web2.local:80;
      proxy_set_header Host web2.local:80;

      server {
      listen 8003 default_server;
      location / {
      proxy_pass http://web3.local:80;
      proxy_set_header Host web3.local:80;

      upstream main {

      server {
      listen 80;
      location / {
      proxy_pass http://main;
      As you can see, the trick is to create a local server responding to a particular port that will proxy the server by rewriting the right Host for each servers. Then, you can use this local servers in your upstream and finally use that upstream in the real proxy.

  1. hi

    i’m a newbie i don’t understand the use of eclipse to create our own nginx custom image
    why can’t we use a simple text editor ?

    thanks for your answer

Leave a Reply

Your email address will not be published. Required fields are marked *