0
0
DockerHow-ToBeginner · 4 min read

Docker Compose for Nginx Reverse Proxy: Setup Guide

Use a docker-compose.yml file to define an nginx service configured as a reverse proxy that forwards requests to backend services. Mount your custom nginx.conf file and expose ports to route traffic properly.
📐

Syntax

A typical Docker Compose setup for an Nginx reverse proxy includes defining the nginx service with the image, ports, volumes for configuration, and networks to connect backend services. The nginx.conf file contains proxy rules directing traffic to target containers.

  • services: Defines containers like nginx and backend apps.
  • image: Specifies the Docker image to use, e.g., nginx:latest.
  • ports: Maps host ports to container ports for external access.
  • volumes: Mounts local config files into the container.
  • networks: Connects containers so Nginx can reach backend services.
yaml
version: '3.8'
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    networks:
      - webnet

  app:
    image: your-backend-image
    networks:
      - webnet

networks:
  webnet:
💻

Example

This example shows a Docker Compose file running Nginx as a reverse proxy forwarding HTTP requests to a backend app container on port 5000. The nginx.conf file configures the proxy rules.

yaml
version: '3.8'
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - app
    networks:
      - webnet

  app:
    image: hashicorp/http-echo
    command: ["-text=Hello from backend"]
    networks:
      - webnet

networks:
  webnet:
Output
Starting app ... done Starting nginx ... done # Access http://localhost in browser or curl # Response: Hello from backend
⚠️

Common Pitfalls

Common mistakes include:

  • Not mounting the nginx.conf correctly, causing Nginx to use default config.
  • Forgetting to connect backend services and Nginx to the same Docker network.
  • Not exposing or mapping ports properly, so requests don't reach Nginx.
  • Using localhost inside Nginx config instead of the backend service name defined in Compose.

Always use the backend service name as the proxy target, not localhost.

nginx
Wrong nginx.conf snippet:

server {
  listen 80;
  location / {
    proxy_pass http://localhost:5000;
  }
}

Right nginx.conf snippet:

server {
  listen 80;
  location / {
    proxy_pass http://app:5000;
  }
}
📊

Quick Reference

Tips for setting up Nginx reverse proxy with Docker Compose:

  • Use depends_on to ensure backend starts before Nginx.
  • Mount nginx.conf as read-only to avoid accidental changes.
  • Use Docker network aliases (service names) in proxy_pass.
  • Expose only necessary ports on host.

Key Takeaways

Define Nginx and backend services in the same Docker Compose network for communication.
Mount a custom nginx.conf file to configure reverse proxy rules correctly.
Use backend service names, not localhost, in Nginx proxy_pass directives.
Expose and map ports properly to allow external access to Nginx.
Use depends_on to control service startup order in Docker Compose.