如何配置Traefik以使用Django Sites Framework

时间:2019-06-04 12:21:06

标签: django docker traefik cookiecutter-django django-sites

我正在使用Docker-compose和Traefik和Let'sencrypt在生产中测试cookiecutter-django。我正在尝试使用Django网站将其配置为与2个域(mydomain1.com和mydomain2.com)一起使用。

如何配置Traefik,以便它可以将流量转发到必要的域?

这是我的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"

现在所有域都通过ssl工作,但我只能看到mydomain1.com,而mydomain2.com显示ERR_TOO_MANY_REDIRECTS。

1 个答案:

答案 0 :(得分:0)

您尝试了什么?什么没用?通过阅读您的问题很难说出来。

您似乎在cc-django存储库中打开了一个答案in the issue

首先,我将尝试把Traefik排除在方程之外,并按照建议的方式在本地进行这项工作。一旦在本地工作,就可以将正确的端口/容器映射到Traefik中的正确域。

假设您已配置docker-compose在端口5000和5001上运行django容器,我认为您需要调整后端和前端部分,如下所示:

[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"

我没有尝试这些,但这是我要做的第一件事。另外,看起来我们可以指定rules on frontends来调整路由。