简体   繁体   中英

How to configure Traefik to use Django Sites Framework

I'm testing cookiecutter-django in production using Docker-compose and Traefik with Let'sencrypt. I'm trying to configure it to work with 2 domains (mydomain1.com and mydomain2.com) using Django sites.

How to configure Traefik so it could forward traffic to necessary domain?

This is my traefik.toml

logLevel = "INFO"
defaultEntryPoints = ["http", "https"]

# Entrypoints, http and https
[entryPoints]
  # http should be redirected to https
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
  # https is the default
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]

# Enable ACME (Let's Encrypt): automatic SSL
[acme]
# Email address used for registration
email = "mail@mydomain1.com"
storage = "/etc/traefik/acme/acme.json"
entryPoint = "https"
onDemand = false
OnHostRule = true
  # Use a HTTP-01 acme challenge rather than TLS-SNI-01 challenge
  [acme.httpChallenge]
  entryPoint = "http"

[file]
[backends]
  [backends.django]
    [backends.django.servers.server1]
      url = "http://django:5000"

[frontends]
  [frontends.django]
    backend = "django"
    passHostHeader = true
    [frontends.django.headers]
      HostsProxyHeaders = ['X-CSRFToken']
    [frontends.django.routes.dr1]
      rule = "Host:mydomain1.com"

Now all domains working through ssl,but I can see only mydomain1.com, and mydomain2.com shows ERR_TOO_MANY_REDIRECTS.

What have you tried? What didn't work? By reading your question it's hard to tell.

There is an element of answer in the issue you seem to have opened in cc-django repo.

First things first, I would try to take Traefik out of the equation and make this work locally by doing something as suggested. Once it works locally, it's a matter of mapping the right port/container to the right domain in Traefik.

Assuming you've configure docker-compose to run the django containers on port 5000 and 5001, I think you would need to adjust you backends and frontends section as below:

[backends]
  [backends.django1]
    [backends.django1.servers.server1]
      url = "http://django:5000"
  [backends.django2]
    [backends.django2.servers.server1]
      url = "http://django:5001"

[frontends]
  [frontends.django1]
    backend = "django1"
    passHostHeader = true
    [frontends.django1.headers]
      HostsProxyHeaders = ['X-CSRFToken']
    [frontends.django1.routes.dr1]
      rule = "Host:mydomain1.com"
  [frontends.django2]
    backend = "django2"
    passHostHeader = true
    [frontends.django2.headers]
      HostsProxyHeaders = ['X-CSRFToken']
    [frontends.django2.routes.dr1]
      rule = "Host:mydomain2.com"

I didn't try these, but that would be the first thing I would do. Also, it looks like we can specify rules on frontends to adjust routing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM