nginx – Little Big Extra http://littlebigextra.com A technology blog covering topics on Java, Scala, Docker, AWS, BigData, DevOps and much more to come. Do it yourself instructions for complex to simple problems for a novice to an expert. Mon, 01 Apr 2019 14:18:04 +0000 en-US hourly 1 https://wordpress.org/?v=5.1.1 http://littlebigextra.com/wp-content/uploads/2023/04/cropped-logo-32x32.png nginx – Little Big Extra http://littlebigextra.com 32 32 How to install Nginx as a reverse proxy server with Docker http://littlebigextra.com/install-nginx-reverse-proxy-server-docker/ http://littlebigextra.com/install-nginx-reverse-proxy-server-docker/#comments Fri, 19 May 2023 15:17:10 +0000 http://littlebigextra.com/?p=989 How to install Nginx as a reverse proxy server with Docker Introduction 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 […]

The post How to install Nginx as a reverse proxy server with Docker appeared first on Little Big Extra.

]]>
How to install Nginx as a reverse proxy server with Docker

Introduction

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

docker run -d --name nginx nginx

  • Open the bash console for accessing Nginx configuration

bash -c "clear && docker exec -it nginx sh"

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

location /<URL-To-BE-ACCESSED> {  
        proxy_pass http://<DOCKER_CONTAINER_NAME>:<DOCKER-PORT>;  
    }

for eg,

location /app1 {  
        proxy_pass http://microservice1:8080;  
    }

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

server {
    listen       80;
    server_name  nginxserver;
    
    location /app1 {  
        proxy_pass http://microservice1:8080;  
    }
    
    location /app2 {  
        proxy_pass http://microservice2:8080;  
    }

    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

   
}

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.

#GET the base default nginx image from docker hub
FROM nginx

#Delete the Existing default.conf
RUN rm /etc/nginx/conf.d/default.conf 

#Copy the custom default.conf to the nginx configuration
COPY default.conf /etc/nginx/conf.d

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

docker build -t  mynginx .

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

docker run -d --name mynginx -p 80:80 -p 443:443 --link=microservice1 --link=microservice2 mynginx

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

http://localhost/app1

http://localhost/app2

For reference , please see below video.


 

The post How to install Nginx as a reverse proxy server with Docker appeared first on Little Big Extra.

]]>
http://littlebigextra.com/install-nginx-reverse-proxy-server-docker/feed/ 7