Home > Net >  Allow both http and https with Traefik on Docker
Allow both http and https with Traefik on Docker

Time:09-24

I am using Traefik in my VPS to route the traffic to my websites, after hours of messing around with it I finally managed to get it working with https using Le's Encrypt.

Now, one thing that I need to do is be able to also access my website via plain http as this is a hobby project for older browsers and the only reason I added tls is because Firefox doesn't like my website without it.

The problem is that, with my current configuration, I can access my website via https normally but when I try it with plain http I get a 404 error.

Here's what my config on docker-compose looks like:

version: "3"
services:
  traefik:
    image: "traefik:v2.5"
    container_name: "traefik"
    command:
      #- "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.teeresolver.acme.tlschallenge=true"
      - "[email protected]"
      - "--certificatesresolvers.teeresolver.acme.storage=/letsencrypt/acme.json"
    ports:
      - "443:443"
      - "80:80"
      - "8080:8080"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      - mywebsite
  # [...]
  mywebsite:
    image: my-web-site/site
    build:
      context: ~/mywebsite-runner/_work/my-web-site-php/my-web-site-php
      dockerfile: Dockerfile
    volumes:
      - ./tee-downloads:/var/www/build/downloads
      - ./tee-contents:/var/www/build/contents
    ports:
      - "0.0.0.0:8001:80"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.themywebsite.rule=Host(`mywebsite.com`, `www.mywebsite.com`)"
      - "traefik.http.routers.themywebsite.entrypoints=websecure,web"
      - "traefik.http.routers.themywebsite.tls.certresolver=teeresolver"
    networks:
      - mywebsite

networks:
  mywebsite:

I have been searching for a solution for hours but the only things I can find on google are configs to redirect http to https, which I can't do.

Does anyone know how to do that?

Thanks in advance for the help.

CodePudding user response:

In traefik , each router defines a set of policies to apply depending of rules and entrypoints.

If you want 2 policies, one for http and one for https, you need to define 2 traefik routers :

  mywebsite:
    ...
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.themywebsite.entrypoints=websecure"
      - "traefik.http.routers.themywebsite.tls.certresolver=teeresolver"
      - "traefik.http.routers.themywebsite.rule=Host(`mywebsite.com`, `www.mywebsite.com`)"
      - "traefik.http.routers.httpwebsite.entrypoints=web"
      - "traefik.http.routers.httpwebsite.rule=Host(`mywebsite.com`, `www.mywebsite.com`)"
  • One called themywebsite
  • Another one called httpwebsite

As a result, you do not use a certresolver for httpwebsite router.

  • Related