Bijan Biria

How to Build and Configure an Nginx-Proxy Container for Dockerized Microservices on Ubuntu

Introduction Managing Dockerized servers and ensuring their optimal performance can be challenging, especially when it comes to connecting multiple containers.

bijan biria
Tehran, Iran
(Updated: )
3 min read

Introduction

Managing Dockerized servers and ensuring their optimal performance can be challenging, especially when it comes to connecting multiple containers. Instead of creating separate Nginx instances for each service, a single nginx-proxy container is a smart solution that optimizes server resources and simplifies management. In this article, you’ll learn step-by-step how to implement such a system and enhance your expertise in building an efficient and reliable architecture. Ready to get started? Let’s dive in!

Why Building a Dedicated Nginx-Proxy Container is Essential

When configuring a server using Docker in a microservices architecture, it’s crucial to ensure that each container performs a specific and dedicated task. Instead of creating separate Nginx containers for each application—which consumes more server resources—we can create a single nginx-proxy container. This container will act as a reverse proxy to manage connections for all other containers, significantly optimizing resource utilization and simplifying the configuration.

Prerequisites

I use Ubuntu as my operating system. If your server doesn’t have Docker and Docker Compose installed, you can set them up easily with the following command:

curl -fSsL get.docker.com | bash

Directory Structure

I usually create a user named webmaster on my Ubuntu server. Within the user’s home directory, I create a directory called apps or public to organize my containers. The structure typically looks like this:

/home/webmaster/
└── apps (or public)
    ├── db
    ├── backend
    ├── frontend
    └── nginx-proxy

Adding a Custom Error Page for the Nginx-Proxy

After creating the nginx-proxy directory, the next step is to add a custom HTML file to handle error scenarios. This file will be displayed whenever a user tries to access a domain that doesn’t have a proper backend connection configured.

The custom error page file can be named according to your preference. For this example, we’ll name it backend-not-found.html. The purpose of this file is to improve user experience by showing a friendly error page instead of a generic browser error.

File Location

The file should be created in the following path:

/home/webmaster/public/nginx-proxy/backend-not-found.html

File Content

Below is an example of what the file could look like. Feel free to customize the content and style it according to your requirements:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Proxy Backend Not Found</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        text-align: center;
        background-color: #f8f9fa;
        color: #343a40;
        padding: 50px;
      }
      h2 {
        font-size: 24px;
      }
    </style>
  </head>
  <body>
    <h2>Proxy Backend Not Found</h2>
    <p>The requested domain is not properly configured. Please contact the administrator.</p>
  </body>
</html>

Why Create This File?

  1. User-Friendly Error Handling: Instead of showing a generic browser error, users see a professional error page.
  2. Customizable Content: You can include branding, contact details, or a link to return to your main website.
  3. Improved Debugging: It helps in identifying domains that are not properly configured during development or deployment.

Creating the conf.d Directory and Default Configuration File

Comments

Link copied to clipboard!