【问题标题】:Apache2 reverse proxy to Traefik with Docker使用 Docker 到 Traefik 的 Apache2 反向代理
【发布时间】:2021-11-14 22:44:11
【问题描述】:

我几乎得到了我想要的东西,但有一件事我无法工作,这让我很烦恼。

我的服务器上运行着 Docker,example.com 并设置了 Traefik 来进行反向代理。我希望配置尽可能少,所以我希望我添加到的任何容器 container 都可以通过 container.services.example.com 自动访问。

所以我有一个traefik.toml 文件:

defaultEntryPoints = ["https","http"]

[entryPoints]
  [entryPoints.http]
  address = ":8050"
  [entryPoints.https]
  address = ":8051"
  [entryPoints.https.tls]

[retry]

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "services.example.com"
watch = true
#exposedByDefault = false

[acme]
email = "services@example.com"
storage = "acme.json"
entryPoint = "https"
caServer = "https://acme-staging-v02.api.letsencrypt.org/directory"
onHostRule = true
[acme.httpChallenge]
  entryPoint = "http"

并在 Apache 中创建了一个 VHost 来进行反向代理:

<VirtualHost *:80>
 ProxyPreserveHost On
 ProxyRequests Off

 ServerName services.example.com
 ServerAlias *.services.example.com

 ProxyPass / http://localhost:8050/
 ProxyPassReverse / http://localhost:8050/

 ErrorLog ${APACHE_LOG_DIR}/services.example.com.error.log
 CustomLog ${APACHE_LOG_DIR}/services.example.com.access.log combined

 <Location />
   Order deny,allow
   Deny from all
   Allow from 10.0.1
 </Location>
 <Location /.well-known/acme-challenge/>
   Order deny, allow
   Allow from all
 </Location>
</VirtualHost>

<VirtualHost *:443>
 ProxyPreserveHost On
 ProxyRequests Off

 ServerName services.example.com
 ServerAlias *.services.example.com

 ProxyPass / https://localhost:8050/
 ProxyPassReverse / https://localhost:8050/

 ErrorLog ${APACHE_LOG_DIR}/services.example.com.error.log
 CustomLog ${APACHE_LOG_DIR}/services.example.com.access.log combined

 <Location />
   Order deny,allow
   Allow from all
 </Location>
</VirtualHost>

这很好用:

  • Traefik 找到我的容器(例如 whoami)并使用路由规则 Host:whoami.services.example.com 创建 HTTP 和 HTTPS 端点。
  • 我可以通过 HTTP 在http://whoami.services.example.com 访问它们。
  • 在我的 acme.json 中,我看到 Let's Encrypt 为 whoami.services.example.com 请求了 SSL 证书。

唯一不起作用的是通过 HTTPS 端点访问主机。当我转到https://whoami.services.example.com 时,我得到一个SSL_ERROR_BAD_CERT_DOMAIN

怀疑问题在于,我没有在 Apache 中配置 SSLCertificateFile。我希望它为 Docker 容器中的代理服务器提供服务,但我不知道该怎么做。你能帮帮我吗?

我考虑过的事情:

  • 正在为 *.services.example.com 设置通配符证书,但我的托管服务提供商没有可用于 DNS 质询的 API。
  • 直接将 Traefik 暴露给外界,但我不想在 URL 中输入端口号
  • 使用替代规则,以便我可以在services.example.com/whoami 访问容器,但我不知道如何设置它以及如果不在其子域的根目录调用所有服务是否将继续工作。
  • 为每个服务手动创建一个虚拟主机,但我不想在每次添加或删除 docker 时都考虑这一点。

【问题讨论】:

    标签: docker-compose apache2 reverse-proxy traefik


    【解决方案1】:

    经过更多修改后,我找到了一个运行良好的解决方案:我只是 mod_rewrite Traefik HTTPS 端点的所有内容,除了需要在端口 80 上的 ACME 挑战。

    <VirtualHost *:80>
     ServerName services.example.com
     ServerAlias *.services.example.com
    
     ProxyRequests Off
     <Location />
       # By default, redirect requests to the Traefik HTTPS endpoint
       RewriteEngine On
       RewriteRule (.*) https://%{SERVER_NAME}:8051%{REQUEST_URI} [QSA,R=301,L]
     </Location>
    
     # ACME HTTP challenge only works on port 80 so only proxy that through
     <Location /.well-known/acme-challenge/>
       # Override <Location /> setting:
       RewriteEngine Off
    
       ProxyPreserveHost On
       ProxyPass http://localhost:8050/.well-known/acme-challenge/
       ProxyPassReverse http://localhost:8050/.well-known/acme-challenge/
     </Location>
    
    </VirtualHost>
    

    现在我将端口(8050、8051、8082)更改为其他端口,我很高兴(够了)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 2020-02-18
      相关资源
      最近更新 更多