docker - 如何使用 Traefik 2.0 和 Docker Compose 标签将 http 重定向到 https?

标签 docker docker-compose traefik

请注意,这是一个 Traefik V2 问题。我在 V1 上有一个解决方案,但 V2 是一个彻底的重整。

以上应该重定向 http://whoami.mysite.com http s ://whoami.mysite.com。

  • http s 工作得很好。
  • http 不会重定向到 https 并引发错误 404。

  • 没有其他文件。一切都在这个 Docker-compose.yml 中,因为它是准备进一步部署的测试。
    version: "3.3"
    
    services:
    
      traefik:
        image: "traefik:v2.0"
        container_name: "traefik"
        command:
          - "--log.level=DEBUG"
          - "--api.insecure=true"
          - "--providers.docker=true"
          - "--providers.docker.exposedbydefault=false"
          - "--entrypoints.web.address=:80"
          - "--entrypoints.web-secure.address=:443"
          - "--certificatesresolvers.myhttpchallenge.acme.httpchallenge=true"
          - "--certificatesresolvers.myhttpchallenge.acme.httpchallenge.entrypoint=web-secure"
          #- "--certificatesresolvers.myhttpchallenge.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
          - "--certificatesresolvers.myhttpchallenge.acme.email=me@mail.com"
          - "--certificatesresolvers.myhttpchallenge.acme.storage=/letsencrypt/acme.json"
        labels:
          - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
        ports:
          - "80:80"
          - "443:443"
          - "8080:8080"
        volumes:
          - "./letsencrypt:/letsencrypt"
          - "/var/run/docker.sock:/var/run/docker.sock:ro"
    
      whoami:
        image: "containous/whoami"
        container_name: "whoami"
        labels:
          - "traefik.enable=true"
          - "traefik.http.routers.whoami.rule=Host(`whoami.mysite.com`)"
          - "traefik.http.routers.whoami.entrypoints=web"
          - "traefik.http.routers.whoami.middlewares=redirect-to-https@docker"
          - "traefik.http.routers.whoami-secured.rule=Host(`whoami.mysite.com`)"
          - "traefik.http.routers.whoami-secured.entrypoints=web-secure"
          - "traefik.http.routers.whoami-secured.tls=true"
          - "traefik.http.routers.whoami-secured.tls.certresolver=myhttpchallenge"
    

    最佳答案

    我建议看看这里的文档 Entrypoint redirect 80 > 443

    这对我有用,如果您希望所有流量从端口 80 重定向到 443,这是最好的解决方案。

    --entrypoints.web.address=:80
    --entrypoints.web.http.redirections.entryPoint.to=websecure
    --entrypoints.web.http.redirections.entryPoint.scheme=https
    --entrypoints.web.http.redirections.entrypoint.permanent=true
    --entrypoints.websecure.address=:443
    

    NOTE:

    there are so many examples around. Just take a look at websecure.

    Sometimes it is written web-secure.



    希望有帮助;o)

    关于docker - 如何使用 Traefik 2.0 和 Docker Compose 标签将 http 重定向到 https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58356714/

    相关文章:

    wordpress - 在Docker容器中托管WordPress网站的视频

    docker - 使用单个 CMD 在 Dockerfile 上定义多个服务

    docker - 子域、Nginx-proxy 和 Docker-compose

    nginx - Docker nginx 无法连接到在单独容器中运行的上游 gunicorn

    Traefik 基本身份验证不适用于 HTTPS

    linux - docker attach - 将 tty false 变为 tty true

    docker - 如何在launchsettings.json中为docker设置ServicePort

    tomcat - 谁能帮我为我的 tomcat 副本创建 Dockerfile?

    cloudflare - Traefik : Auto-add subdomains to cloudflare DNS

    Azure Service Fabric 与 Træfik - 与 Azure LB?