Docker

What are the Steps to Set Up a Docker Nginx Reverse Proxy Server?

A reverse proxy server is a server type that connects a client with a web server. It gets requests from the client and redirects them to the relevant backend server. Then, send back the response to the reverse proxy server. It then passes the response to the client, making it appear as if it came from the reverse proxy server itself. Moreover, it improves scalability and performance by dividing the load among numerous backend servers.

This write-up will explain the step-by-step procedure to set up a Docker Nginx reverse proxy server.

What are the Steps to Set Up a Docker Nginx Reverse Proxy Server?

To set up a Docker Nginx reverse proxy server, check out the below-provided steps:

Step 1: Create Docker File

On Visual Studio Code, create a file named “Dockerfile” and paste the following snippet into it to containerize the “main.go” program:

FROM golang:1.8    // Define Base Image
WORKDIR /go/src/app
COPY main.go .
RUN go build -o webserver .   // Perform Compilation
ENTRYPOINT ["./webserver"]

In the above code:

  • The “FROM” command defines the base image to utilize, which is “golang:1.8” in our case.
  • WORKDIR” sets the working directory of the Docker container to “/go/src/app”.
  • The “COPY” command copies the “go” file from the local directory into the Docker container’s working directory.
  • The “RUN” instruction runs the “go build” command inside the Docker container to compile the Go application.
  • The “ENTRYPOINT” sets the entry point for the Docker container to run the “webserver” binary, which was compiled in the previous step.

In our case, there are two distinct “main.go” programs in two distinct directories. Both programs use the same Docker file in their folders. However, the initial program uses the Docker file to set up the service and the second program is utilizing Docker file to build the new Docker image “go1-image” that will be utilized to set up another service in the YAML file.

Step 2: Make Compose File

Then, build a new file named “docker-compose.yml” with some service configurations. For instance, we have configured the following services:

version: "alpine"
services:
  web:
    build: .
  web1:
    image: go1-img
  nginx:
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - web
      - web1
    ports:
      - 8080:8080

In the above code:

  • The “services” is used to define services. Here, we have three services: “web”, “web1”, and “nginx”.
  • The “web” service is made from the current directory and will run the first program.
  • The “web1” service is created from a pre-built Docker image named “go1-img” and will execute the second program.
  • The “nginx” service is created from the official Nginx Docker image. It mounts volumes and specifies ports.
  • The “volumes” mount the local file “conf” to the container’s “/etc/nginx/nginx.conf” file in read-only mode.
  • The “depends_on” key defines that the “nginx” service is depending on two services i.e., “web” and “web1” services.
  • The “ports” key assigns the port of the nginx reverse proxy which is like the “conf” file:

This compose file has defined a simple architecture where a web server (“web” or “web1”) and a NGINX reverse proxy server (“nginx”) are deployed together.

Step 3: Create “nginx.conf” File

Next, create the “nginx.conf” file to configure upstream services. Then, set the reverse proxy and load the upstream services on the particular port. For instance, the relevant code is mentioned below:

user nginx;
events {
 worker_connections 1000;  // configure event value
}

http {

 upstream all {
  server web:8080;     // configure port value
  server web1:8080;
}
 server {
  listen 8080;        // configure port value
  location / {
   proxy_pass http://all/;
  }
 }

In the above code:

  • Firstly, the “user nginx” will run as the nginx user.
  • The “events” block specifies the maximum number of concurrent connections that each worker process can manage.
  • The “http” block is the main configuration block for HTTP requests.
  • The “upstream all” block sets a named upstream group that includes two servers i.e., “web” and “web1” listening on port “8080”.
  • The “server” block defines the configuration for the HTTP server that listens on port “8080”.
  • The “location” block specifies the URL path for Nginx proxy requests. In this case, the path for the proxy request is to the “all” upstream group.

Step 4: Start Containers

Finally, write out the “docker-compose up” command to start all services in particular containers:

docker-compose up --scale web=2

Here, the “scale” option is utilized to specify the number of containers that need to run for a particular service. In this case, “–scale web=2” tells Docker Compose to execute two containers for the web service:

Step 5: Verification

Lastly, redirect to the allocated exposing port of the “nginx” service container to ensure that the Nginx reverse proxy server is working properly or not:

In the above output, we have successfully executed multiple containers or services on the same port by setting the Docker Nginx server.

Conclusion

To set up a Docker Nginx reverse proxy server, first, create a Docker file. Then, create a compose file to configure services. Next, create an “nginx.conf” file to set the reverse proxy server by configuring the load balancers configurations. It includes upstreaming services, proxy to upstream the service, and listening ports. This write-up has explained the step-by-step procedure to set up a Docker Nginx reverse proxy server.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.