gpt4 book ai didi

docker - 全局使用 traefik 中间件

转载 作者:行者123 更新时间:2023-12-02 19:52:19 35 4
gpt4 key购买 nike

我试图在 traefik.yml 中声明 https 重定向文件。现在我尝试在 中添加这些规则traefik 服务在 docker-compose.yml .这就像一个魅力。虽然我更喜欢在 traefik.yml 中配置这个全局和中间件重定向文件,然后在 docker-compose.yml 上的 traefik 服务中引用它.

我以前吃过的

version: '3'

networks:
web:
external: true

services:
traefik:
image: traefik:v2.1
ports:
- 80:80
- 443:443
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./.traefik/traefik.yml:/traefik.yml
- ./.traefik/acme.json:/acme.json
networks:
- web
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.rule=Host(`$HOSTNAME`)"
- "traefik.http.routers.traefik.service=api@internal"
- "traefik.http.routers.traefik.tls.certresolver=le"
- "traefik.http.routers.traefik.entrypoints=https"
# Global redirect to https
- "traefik.http.routers.http-catchall.rule=hostregexp(`{host:.+}`)"
- "traefik.http.routers.http-catchall.entrypoints=http"
- "traefik.http.routers.http-catchall.middlewares=redirect-to-https"
# Middleware redirect
- "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"

这很容易,并将所有其他域从 http 重定向到 https。

我现在想要什么

我想在 traefik.yml 中声明这些重定向.

到目前为止,我已经这样做了。
api: {}

entryPoints:
http:
address: ":80"
https:
address: ":443"

log:
level: DEBUG

providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
network: web

http:
# Global redirect to https
routers:
http-catchall:
rule: hostregexp(`{host:.+}`)"
entrypoints:
http
middlewares:
- redirect-to-https
# Middleware redirect
middlewares:
redirect-to-https:
redirectScheme:
scheme: https

certificatesResolvers:
le:
acme:
email: john@doe.com
storage: acme.json
# Activate for Development: Certificate will not be valid. It's only for testing if it can be obtained.
#caServer: https://acme-staging-v02.api.letsencrypt.org/directory
httpChallenge:
entryPoint: http

如您所见,我声明了 http 设置。

我现在的问题是如何将这些设置引用到我的 traefik 服务中?

我试过了
- "traefik.http.middlewares=redirect-to-https" - "traefik.http.middlewares.redirect-to-https" - "traefik.http.middlewares.traefik=redirect-to-https@file"
他们都没有工作。有些在仪表板中显示中间件,但它没有链接到任何设置。

有没有人找到解决方案?我无法从有关此的文档中获得任何信息。我认为它必须以某种方式链接到 @file .

谢谢

最佳答案

@file意味着中间件是在文件提供程序中定义的。
您可以在 traefik.yml 中添加这样的文件提供程序。

providers:
file:
directory: "/path/to/dynamic/conf"
使用中间件在该目录中创建一个文件。
http:
middlewares:
redirect-to-https:
redirectScheme:
scheme: https
您现在可以在标签中引用重定向到 https@file。
- "traefik.http.middlewares.traefik=redirect-to-https@file"
注意 :您在 traefik.yml 中的一些配置可能需要移动到您的新 yml 文件中。我是 Traefik 的新手,还不完全了解原因。
请参阅文档中的以下部分:
  • https://docs.traefik.io/middlewares/overview/#provider-namespace
  • https://docs.traefik.io/providers/file/
  • 关于docker - 全局使用 traefik 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59929838/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com