I'm using Traefik with a few FastAPI Docker containers (not using swarm). Everything is working perfectly except my Jinja2 templates are rendering http
prefixes when I make references like {{ url_for('assets', path='css/styles.css') }}
and {{request.url}}
. From what I've read, this means that I need to force-set X-Forwards*.
I can't seem to find any documentation about using the docker-compose style settings that I've used thus far. My current docker-compose for the fastapi container that isn't forwarding
fastapi:
container_name: "fastapi"
image: "fastapi:latest"
env_file:
- .env
build:
context: ./
dockerfile: Dockerfile
labels:
- fastapi
volumes:
- .:/app
restart: unless-stopped
labels:
# Enable Traefik for this specific "backend" service
- traefik.enable=true
# Define the port inside of the Docker service to use
- traefik.http.services.fastapi.loadbalancer.server.port=80
# Make Traefik use this domain in HTTP
- traefik.http.routers.fastapi-http.entrypoints=http
- traefik.http.routers.fastapi-http.rule=Host(`${WEBSITE_URL?Variable not set}`)
# Use the traefik-public network (declared below)
- traefik.docker.network=traefik-public
# Make Traefik use this domain in HTTPS
- traefik.http.routers.fastapi-https.entrypoints=https
- traefik.http.routers.fastapi-https.rule=Host(`${WEBSITE_URL?Variable not set}`)
- traefik.http.routers.fastapi-https.tls=true
# Use the "le" (Let's Encrypt) resolver
- traefik.http.routers.fastapi-https.tls.certresolver=le
# https-redirect middleware to redirect HTTP to HTTPS
- traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
- traefik.http.middlewares.https-redirect.redirectscheme.permanent=true
# Middleware to redirect HTTP to HTTPS
- traefik.http.routers.fastapi-http.middlewares=https-redirect
networks:
- traefik-public
networks:
traefik-public:
external: true
I've tried adding all of the following without any success:
- traefik.http.middlewares.https-redirect.headers.customrequestheaders.X-Forwarded-Proto=http
- traefik.http.middlewares.https-redirect.headers.customrequestheaders.X-Forwarded-Proto=https
- traefik.http.routers.fastapi-https.rule=Host(`${WEBSITE_URL?Variable not set}`) && Headers(`X-Test`, `https`)
- traefik.http.routers.fastapi-https.rule=Host(`${WEBSITE_URL?Variable not set}`) && Headers(`X-Forwarded-Proto`, `https`)
Would really appreciate it if anyone knows how to fix this.