【发布时间】: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