【问题标题】:How to configure Traefik to use Django Sites Framework如何配置 Traefik 以使用 Django Sites Framework
【发布时间】:2019-06-04 12:21:06
【问题描述】:

我正在使用 Docker-compose 和带有 Let'sencrypt 的 Traefik 在生产环境中测试 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。

【问题讨论】:

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


    【解决方案1】:

    你试过什么?什么没用?通过阅读您的问题,很难说。

    有一个回答元素in the issue你似乎已经在 cc-django repo 中打开了。

    首先,我会尝试将 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 来调整路由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 2023-04-03
      • 2017-12-16
      • 1970-01-01
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多