dockerswarm.rocks icon indicating copy to clipboard operation
dockerswarm.rocks copied to clipboard

Traefik : redirect all http to https

Open guidtz opened this issue 3 years ago • 2 comments

Hello, I try to force all http traffic to https but it's not works. This is my docker-compose :

services:
traefik:
  image: traefik:v2.5
  ports:
    - 80:80
    - 443:443
  deploy:
    placement:
      constraints:
        - node.role == manager
        - node.labels.traefik == true
    labels:
      - traefik.enable=true
      - traefik.docker.network=traefik-public
      - traefik.http.middlewares.admin-auth.basicauth.users=admin:$$apr1$$eGxxeKwV$$unRwPb4cIWBlWF9ljg2gx1        
      # https-redirect middleware to redirect HTTP to HTTPS
      # It can be re-used by other stacks in other Docker Compose files
      - traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
      - traefik.http.middlewares.https-redirect.redirectscheme.permanent=true
      # traefik-http set up only to use the middleware to redirect to https
      # Uses the environment variable DOMAIN
      - traefik.http.routers.traefik-public-http.rule=Host(`traefik.swm.aukfood.ovh`)
      - traefik.http.routers.traefik-public-http.entrypoints=http
      - traefik.http.routers.traefik-public-http.middlewares=https-redirect
      # traefik-https the actual router using HTTPS
      # Uses the environment variable DOMAIN
      - traefik.http.routers.traefik-public-https.rule=Host(`traefik.swm.aukfood.ovh`)
      - traefik.http.routers.traefik-public-https.entrypoints=https
      - traefik.http.routers.traefik-public-https.tls=true
      # Use the special Traefik service api@internal with the web UI/Dashboard
      - traefik.http.routers.traefik-public-https.service=api@internal
      # Use the "le" (Let's Encrypt) resolver created below
      - traefik.http.routers.traefik-public-https.tls.certresolver=le
      # Enable HTTP Basic auth, using the middleware created above
      - traefik.http.routers.traefik-public-https.middlewares=admin-auth
      # Define the port inside of the Docker service to use
      - traefik.http.services.traefik-public.loadbalancer.server.port=8080
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock:ro
    - traefik:/certificates
  command:
    # Enable Docker in Traefik, so that it reads labels from Docker services
    - --providers.docker
    # Do not expose all Docker services, only the ones explicitly exposed
    - --providers.docker.exposedbydefault=false
    # Enable Docker Swarm mode
    - --providers.docker.swarmmode
    # Create an entrypoint "http" listening on port 80
    - --entrypoints.http.address=:80
    # Create an entrypoint "https" listening on port 443
    - --entrypoints.https.address=:443
    # Create the certificate resolver "le" for Let's Encrypt, uses the environment variable EMAIL
    - [email protected]
    # Store the Let's Encrypt certificates in the mounted volume
    - --certificatesresolvers.le.acme.storage=/certificates/acme.json
    # Use the TLS Challenge for Let's Encrypt
    - --certificatesresolvers.le.acme.tlschallenge=true
    # Enable the access log, with HTTP requests
    - --accesslog
    # Enable the Traefik log, for configurations and errors
    - --log
    # Enable the Dashboard and API
    - --api
  networks:
    - traefik-public

Do you have any idea why redirection is not works ?

Best regards

guidtz avatar Oct 27 '21 05:10 guidtz

@guidtz Hi!

--entrypoints.http.http.redirections.entryPoint.to=https --entrypoints.http.http.redirections.entryPoint.scheme=https --entrypoints.http.http.redirections.entrypoint.permanent=true

Let me know if it helps.

mh3th avatar Jan 20 '22 17:01 mh3th

Here's how we did it https://git.coopcloud.tech/coop-cloud/traefik/src/commit/1d7542cd5f7003454ff5b3773d8c8d42ed4e97e1/traefik.yml.tmpl#L21-L24

decentral1se avatar Feb 21 '22 00:02 decentral1se

Thanks for the help all!

tiangolo avatar Dec 10 '23 13:12 tiangolo

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

github-actions[bot] avatar Dec 21 '23 00:12 github-actions[bot]