Search code examples
dockerapachetraefik

Can’t configure traefik to properly redirect to apache: too many redirects


I have a Django application that is used for time tracking at my institution. It's running as a Docker container, with an Apache web server in front of it (it does HTTP basic auth using LDAP and then redirects to http://django:80/). I now need Traefik to be in front of everything, because I need to integrate Authelia instead of HTTP basic auth. I want to test everything locally first, that's why this is localhost.

This is my docker-compose.yml:

version: '3'

services:

  web:
    depends_on:
      - django
      - traefik
    expose:
      - "${VIRTUAL_PORT}"
    networks:
      - default
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.chronocommand.rule=Host(`localhost`)"
      - "traefik.docker.network=proxy"
      - "traefik.http.routers.chronocommand.entrypoints=web,websecure"

  django:
    expose: 
      - "80"
    ports:
      - "8080:80"
      - "8000:8000"
      - "3000:3000"
    volumes:
      - ./Services/Chronocommand:/src
      
  traefik:
    image: "traefik:v3.0"
    ports:
      - "80:80"
      - "443:443"
      - "8888:8080"  # Traefik dashboard
    networks:
      - proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./Services/DevTraefik/:/etc/traefik

networks:
  proxy:
    external: true

this is my traefik config:

api:
  dashboard: true
  insecure: true

global:
  checkNewVersion: true
  sendAnonymousUsage: false
  
entryPoints:
  web:
    address: :80

  websecure:
    address: :443
    http:
      tls: true
  
providers:
  docker:
    exposedByDefault: false
    
  file:
      directory: /etc/traefik
      watch: true

When I try to access https://localhost i expect to see the django webapp, which works if I access it via http://localhost:8080. Instead I get a "too many redirects" error in my browser and I can see in the apache log that it tries to access the server multiple times. What did I do wrong?


Solution

  • I'm guessing a lot of the details of your project. But suppose that it looks something like this:

    ├── docker-compose.yml
    └── Services
        ├── Apache
        │   └── apache.conf
        └── Chronocommand
            ├── chrono
            │   ├── asgi.py
            │   ├── __init__.py
            │   ├── settings.py
            │   ├── urls.py
            │   └── wsgi.py
            ├── db.sqlite3
            ├── Dockerfile
            ├── manage.py
            └── requirements.txt
    

    The contents of the Django application will differ, but I simply created a Django starter project.

    🗎 docker-compose.yml (I'm configuring Traefik directly here rather than using a separate configuration file.)

    version: '3.8'
    
    services:
      apache:
        container_name: apache
        image: httpd:2.4
        ports:
          - "8080:8080"
        volumes:
          - ./Services/Apache/apache.conf:/usr/local/apache2/conf/httpd.conf
        depends_on:
          - django
        labels:
          - "traefik.http.routers.web.rule=Host(`localhost`)"
          - "traefik.http.services.web.loadbalancer.server.port=8080"
    
      django:
        container_name: django
        build:
          context: Services/Chronocommand
        ports:
          - "8000:8000"
        volumes:
          - ./Services/Chronocommand:/src
        command: python manage.py runserver 0.0.0.0:8000
    
      traefik:
        container_name: traefik
        image: traefik:v3.0
        ports:
          - "80:80"
          - "443:443"
          - "8888:8080"
        depends_on:
          - apache
        command:
          - "--api.insecure=true"
          - "--providers.docker=true"
          - "--entrypoints.web.address=:80"
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
    

    🗎 apache.conf

    LoadModule authz_core_module modules/mod_authz_core.so
    LoadModule unixd_module modules/mod_unixd.so
    LoadModule log_config_module modules/mod_log_config.so
    LoadModule mpm_event_module modules/mod_mpm_event.so
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    
    ServerName localhost
    
    ErrorLog /proc/self/fd/2
    CustomLog /proc/self/fd/1 common
    
    LogLevel debug
    
    User www-data
    Group www-data
    
    Listen 8080
    
    <VirtualHost *:8080>
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass / http://django:8000/
        ProxyPassReverse / http://django:8000/
    </VirtualHost>
    

    You can test the following:

    • http://localhost:8000/ will go directly to the Django app;
    • http://localhost:8080/ will go to the Django app via Apache;
    • http://localhost will go through Traefik to Apache and hence to the Django app.

    enter image description here