Configure Nginx with Docker Compose: A Step-by-Step Guide (2026)

Learn how to configure Nginx as a reverse proxy with Docker Compose for efficient multi-service applications. Step-by-step guide for beginners.

Configure Nginx with Docker Compose: A Step-by-Step Guide (2026)

In this tutorial, you will learn how to configure Nginx as a reverse proxy using Docker Compose. This is especially useful when you have multiple services running on a single server, such as an AWS EC2 instance, and you want to route traffic to the appropriate service based on the URL or domain. Nginx is a powerful open-source HTTP server and reverse proxy, known for its high performance and stability.

Key Takeaways

  • Learn how to set up Nginx as a reverse proxy using Docker Compose.
  • Understand the configuration of the Nginx config file (nginx.conf).
  • Deploy a multi-container application efficiently.
  • Ensure secure and efficient communication between services.
  • Troubleshoot common issues when configuring Nginx with Docker Compose.

This guide will walk you through the process of setting up Nginx with Docker Compose, explaining each step in detail to ensure you understand not just the "how," but also the "why." By the end of this tutorial, you will be able to configure Nginx for your application, ensuring smooth traffic handling and efficient resource allocation.

Prerequisites

  • Basic understanding of Docker and Docker Compose.
  • An AWS EC2 instance or any server with Docker installed.
  • Your application dockerized with Docker Compose.
  • Basic knowledge of Nginx configuration files.

Step 1: Install Docker and Docker Compose

Before configuring Nginx, ensure that Docker and Docker Compose are installed on your server. Follow the official installation guides for your specific operating system.

# Update your package index
sudo apt-get update

# Install Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io

# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Verify the installation by running:

docker --version
docker-compose --version

Step 2: Create Nginx Configuration File

Create a directory named conf in your project root and create a file named nginx.conf inside it. This file will contain your Nginx configuration.

# conf/nginx.conf
server {
    listen 80;

    location / {
        proxy_pass http://frontend:80;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

This configuration tells Nginx to listen on port 80 and forward all traffic to the frontend service. The proxy_set_header directives ensure that the client’s original request headers are preserved.

Step 3: Update Docker Compose File

Ensure your docker-compose.yml file is properly set up to link Nginx with your application services. Below is an example of how your Docker Compose file might look:

version: '3'
services:
  nginx:
    container_name: nginx
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
    networks:
      - app-network

  frontend:
    container_name: frontend
    image: myfrontend:image
    networks:
      - app-network

  backend:
    container_name: backend
    image: mybackend:image
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

This configuration ensures all services are connected to the same network, allowing Nginx to communicate with them using their service names.

Step 4: Deploy the Application

Now, you can deploy your application using Docker Compose. Navigate to your project directory and run:

docker-compose up -d

This command will start all the services defined in your Docker Compose file in detached mode, allowing them to run in the background.

Step 5: Verify the Setup

To ensure everything is working correctly, open your browser and navigate to your server’s IP address. You should see your frontend application served through Nginx. If everything is set up correctly, Nginx will act as a reverse proxy, forwarding requests to the appropriate service.

Common Errors/Troubleshooting

  • 503 Service Temporarily Unavailable: This usually indicates that Nginx cannot connect to the backend service. Check the service name in your nginx.conf and ensure the container is running.
  • Permission Denied: If Docker Compose fails to mount the configuration file, check the permissions of the nginx.conf file and ensure it is accessible by the Docker process.
  • Connection Refused: Ensure that the service names in the Nginx config match the service names in the Docker Compose file.

By following these steps, you should have a fully functioning Nginx reverse proxy setup using Docker Compose, streamlining your deployment process and improving your application's architecture.

Frequently Asked Questions

What is a reverse proxy?

A reverse proxy is a server that sits between client devices and backend servers, forwarding client requests to the appropriate backend server.

Why use Nginx as a reverse proxy?

Nginx is known for its high performance, stability, and rich feature set, making it an excellent choice for load balancing and reverse proxying.

How does Docker Compose simplify multi-container applications?

Docker Compose allows you to define and run multi-container Docker applications, simplifying the deployment and management of complex applications.