Search code examples
dockernginxdocker-composedockerfiletraefik

Use nginx on route and traefik for subdomain


How would the docker-compose file and nginx configuration look if I want to use traefik to proxy requests for my subdomains and use nginx on my root.

So, i want to serve up some static files to: domain.com using nginx but i want traefik to handle traffic to: app.domain.com, app2.domain.com

here is what i have in my composer file....

version: '3'

networks:
  proxy:
    external: true
  internal:
    external: false
services:
  traefik:
    image: traefik:alpine
    ports:
      - "8080:8080"
      - "80:80"
      - "443:443"
    restart: always
    labels:
      - logLevel="DEBUG"
      - "traefik.backend=monitor"
      - "traefik.frontend.rule=Host:monitor.domain.com"
      - "traefik.port=8080"
      - "traefik.frontend.entryPoints=http,https"
      - "traefik.enable=true"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "./traefik.toml:/traefik.toml"
      - "./acme.json:/acme.json"
    expose:
      - "8080"
    networks:
      - internal
      - proxy
  custom-badge:
    image: app
    environment:
      PORT: 3000
    ports:
      - "3000:3000"
    labels:
      - traefik.enabled=true
      - traefik.backend=custom-badge
      - traefik.frontend.rule=Host:app.domain.com
      - traefik.docker.network=proxy
      - traefik.port=3000
    networks:
      - internal
      - proxy
  server:
    image: nginx:alpine
    ports:
       - "80:80"
    labels:
      - traefik.enabled=true
      - traefik.backend=
      - traefik.frontend.rule=domain.com
      - traefik.docker.network=proxy
      - traefik.port=80
    volumes:
      - "./apps/root:/etc/nginx/html:ro"
      - "./nginx.conf:/etc/nginx/nginx.conf:ro"
    environment:
      - NGINX_HOST=domain.com
      - NGINX_PORT=80
    command: [nginx-debug, '-g', 'daemon off;']
    depends_on:
      - traefik

and my nginx.conf

    http {
      server {
        listen          80;
        server_name     domain.com www.domain.com;
        location / {
            proxy_pass  domain.com:80/;
        }
      }
    }

Im getting port conflict errors, what am i doing wrong?


Solution

  • You cannot have two services - traefik and nginx use same host port.

    You must have only one service listening on 80 port.

    I would suggest configuring traefik to proxy all communication and in case of lack of subdomain forward to nginx - and what I can see - you did.

    To fix your error simply remove port section from server (nginx) service definition.