jsnow
jsnow

Reputation: 163

Traefik2, two entrypoints (http and https) to one exposed continer port in docker-compose

I am trying to setup Treafik2 as a reverse proxy/SSL termination for a simple flask app (uwsgi is listening on port 8080) in another container. I want to be able to access the site via http or https (I know, I know, we should be using https! EVERYWHERE, trust me there is a good reason for having http and https in this scenario). For the life of me I can't get both the work simultaneously, I can only get http or https. TLS, when used, should terminate at traefik, otherwise the connection should just be unencrypted. Here is my docker-compose file (below only https works, if I comment out the last two router labels (the tls ones) then only http works). Unfortunately I find the traefik documentation a bit lacking, which makes it confusing to me. Any help is appreciated.

version: '3.3'
services:
    traefik:
      image: "traefik:v2.0.0-rc3"
      container_name: "traefik"
      command:
        - "--log.level=DEBUG"
        - "--api.insecure=true"
        - "--providers.docker=true"
        - "--providers.docker.exposedbydefault=false"
        - "--entrypoints.websecure.address=:443"
        - "--entrypoints.web.address=:80"
        - "--certificatesresolvers.mytlschallenge.acme.tlschallenge=true"
        - "--certificatesresolvers.mytlschallenge.acme.caserver=https://acme-v02.api.letsencrypt.org/directory"
        - "[email protected]"
        - "--certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json"
      ports:
        - "443:443"
        - "8080:8080"
        - "80:80"
      volumes:
        - "/mnt/volume_04/letsencrypt:/letsencrypt"
        - "/var/run/docker.sock:/var/run/docker.sock:ro"
    
    example:
      image: exampleflask
      container_name: example
      expose:
        - "8080"
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.example.rule=Host(`example.com`)"
        - "traefik.http.routers.example.entrypoints=web,websecure"
        - "traefik.http.routers.example.tls.certresolver=mytlschallenge"
        - "traefik.http.routers.example.tls.domains[0].main=example.com"
      restart: always

Upvotes: 0

Views: 597

Answers (1)

謝騰緯
謝騰緯

Reputation: 79

You had two entrypoints "web" and "websecure" So you can set two routers, it looks like the below:

For http:

- "traefik.http.routers.exampleforhttp.rule=Host(`example.com`)"
- "traefik.http.routers.exampleforhttp.entrypoints=web"

For https:

- "traefik.http.routers.exampleforhttps.rule=Host(`example.com`)"
- "traefik.http.routers.exampleforhttps.entrypoints=websecure"
- "traefik.http.routers.exampleforhttps.tls.certresolver=mytlschallenge"

Or you want http redirect to https, like the blow:

- "traefik.http.routers.exampleforhttp.rule=Host(`example.com`)"
- "traefik.http.routers.exampleforhttp.entrypoints=web"

- "traefik.http.routers.exampleforhttp.middlewares=http-redirect"
- "traefik.http.middlewares.http-redirect.redirectscheme.scheme=https

- "traefik.http.routers.exampleforhttps.rule=Host(`example.com`)"
- "traefik.http.routers.exampleforhttps.entrypoints=websecure"
- "traefik.http.routers.exampleforhttps.tls.certresolver=mytlschallenge"

Have a good time.

Upvotes: 1

Related Questions