美文网首页
traefik v2 http 跳转 https

traefik v2 http 跳转 https

作者: 思考蛙 | 来源:发表于2020-02-26 22:23 被阅读0次

    方案一:
    可以将下面信息放置在 dynamic_config.toml 中参见基于 traefik v2 的本地开发、部署一致环境方案

    [http.middlewares.https-redirect.redirectScheme]
      scheme = "https"
    [http.middlewares.content-compress.compress]
     
    # tricks
    # https://github.com/containous/traefik/issues/4863#issuecomment-491093096
    [http.services]
      [http.services.noop.LoadBalancer]
         [[http.services.noop.LoadBalancer.servers]]
            url = "" # or url = "localhost"
     
    [http.routers]
      [http.routers.https-redirect]
        entryPoints = ["http"]
        rule = "HostRegexp(`{any:.*}`)"
        middlewares = ["https-redirect"]
        service = "noop"
    

    方案二: docker-compose 配置文件中加入

    services:
      traefik:
        image: "traefik:v2.1.0"
        # ...
        labels:
          # ...
          
          # middleware redirect
          - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
          - "traefik.http.middlewares.test-compress.compress=true"
    
          # global redirect to https
          - "traefik.http.routers.redirs.rule=hostregexp(`{host:.+}`)"
          - "traefik.http.routers.redirs.entrypoints=http"
          - "traefik.http.routers.redirs.middlewares=redirect-to-https"
    

    一个较完整的参考

    version: "3.3"
    
    services:
      traefik:
        image: "traefik:v2.0.0"
        command:
          - --entrypoints.web.address=:80
          - --entrypoints.websecure.address=:443
          - --providers.docker
          - --api
          - --certificatesresolvers.leresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory
          - --certificatesresolvers.leresolver.acme.email=your@email.com
          - --certificatesresolvers.leresolver.acme.storage=/acme.json
          - --certificatesresolvers.leresolver.acme.tlschallenge=true
        ports:
          - "80:80"
          - "443:443"
          - "8080:8080"
        volumes:
          - "/var/run/docker.sock:/var/run/docker.sock:ro"
          - "./acme.json:/acme.json"
        labels:
          # global redirect to https
          - "traefik.http.routers.http-catchall.rule=hostregexp(`{host:.+}`)"
          - "traefik.http.routers.http-catchall.entrypoints=web"
          - "traefik.http.routers.http-catchall.middlewares=redirect-to-https"
    
          # middleware redirect
          - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
    
      my-app:
        image: containous/whoami:v1.3.0
        labels:
          - "traefik.http.routers.my-app.rule=Host(`whoami.docker.localhost`)"
          - "traefik.http.routers.my-app.middlewares=auth"
          - "traefik.http.routers.my-app.entrypoints=websecure"
          - "traefik.http.routers.my-app.tls=true"
          - "traefik.http.routers.my-app.tls.certresolver=leresolver"
          - "traefik.http.middlewares.auth.basicauth.users=user:$$apr1$$q8eZFHjF$$Fvmkk//V6Btlaf2i/ju5n/" # user/password
    
    # Dashboard (https://localhost:8080)
    # Route
    # Basic auth (login: user | password: password)
    # Let's Encrypt (https://whoami.docker.localhost/)
    # Global HTTP to HTTPS redirection (http://whoami.docker.localhost/)
    
    # touch acme.json; chmod 600 acme.json
    

    相关文章

      网友评论

          本文标题:traefik v2 http 跳转 https

          本文链接:https://www.haomeiwen.com/subject/llnichtx.html