【问题标题】:Can't access project absolute url from inside Django Docker instance无法从 Django Docker 实例内部访问项目绝对 URL
【发布时间】:2018-06-29 13:25:38
【问题描述】:

我有一个从 Cookiecutter Django 开始的项目,我目前正在添加 WeasyPrint 以将一些视图作为 PDF 文件提供。这在开发中运行良好。 Cookiecutter Django 使用 Caddy 作为 HTTP 服务器。 由于无法从 Django docker 实例内部通过其绝对 URL 访问文件,因此我在生产时遇到错误。

从 Django docker 实例内部,这不起作用:

$ curl https://myowndomain.com
curl: (7) Failed to connect to myowndomain.com port 443: Connection timed out

但这确实:

$ curl https://www.google.com

从 Django docker 实例外部,两个 curl 命令都可以正常工作。

我的球童档案:

myowndomain.com {
    proxy / django:5000 {
        header_upstream Host {host}
        header_upstream X-Real-IP {remote}
        header_upstream X-Forwarded-Proto {scheme}
        except /media
        transparent
    }

    log stdout
    errors stdout
    gzip

    header / {
        # Don't show Caddy/Gunicorn as server header.
        -Server

        # Enable HTTP Strict Transport Security (HSTS) to force clients to always connect via HTTPS (do not use if only testing)
        Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"

        # Only send Referer header to same origin.
        # Django CSRF protection is incompatible with referrer policy set to none.
        Referrer-Policy "same-origin"

        # Enable cross-site filter (XSS) and tell browser to block detected attacks.
        X-XSS-Protection "1; mode=block"

        # Prevent some browsers from MIME-sniffing a response away from the declared Content-Type
        X-Content-Type-Options "nosniff"
    }
}

有什么我需要设置的吗?我错过了什么吗?

【问题讨论】:

  • 嗨@Tadeo - 所以连接会以这种方式超时,因为端口可能已关闭并且没有接收到连接 - 您是否使用 nginx 作为代理服务器?
  • @WindUpLordVexxos 感谢您的评论。我使用的是Caddy server,而不是 Nginx
  • Hmmmm,我对 caddy server 不是很熟悉...应该是端口开放的情况...
  • 这是我的第一个想法,但就我可以从 Django 容器中卷曲外部 https:// URL 并且可以从外部卷曲 myowndomain.com 而言,问题似乎是相关的到 docker 容器内的 DNS 解析。感谢您的帮助!
  • 请您在此处添加您的球童服务器配置...

标签: django docker cookiecutter-django weasyprint caddy


【解决方案1】:

可能在开发中它在端口 80 上使用 http,而在生产中它在端口 443 上使用 https。 在 docker-compose.yml 中或调用 docker 时是否公开端口 443?

【讨论】:

  • 是的,我在生产中的 docker-compose.yml 的“Caddy / ports”部分声明了“0.0.0.0:80:80”和“0.0.0.0:443:443”。事实上,该站点可以从 Django docker 容器外部访问,并且其他 https URL 可以从它内部访问。
猜你喜欢
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多