I have a domain registered with cloudflare, and from my internal network, I want to be able to type something like portainer.local
into my browser, and then have that be redirected to portainer.mydomain.cc
. I've done this successfully with NGINX Proxy Manager in the past with no cert warnings, etc., but I just can't seem to get it to work with Traefik.
To start, I have local cname record that points portainer.local
to portainer.mydomain.cc
, and a dns entry that points portainer.mydomain.cc
to the my Traefik reverse proxy container.
Here is my Portainer container compose file that shows the Traefik labels:
version: '3.7'
services:
portainer-ee:
container_name: portainer
image: 'portainer/portainer-ee:latest'
restart: always
network_mode: broker
ports:
- '8000:8000'
- '9443:9443'
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
- '/mnt/docker/mapped/config/portainer:/data'
labels:
- 'traefik.enable=true'
- 'traefik.http.routers.portainer.entrypoints=https'
- 'traefik.http.routers.portainer.rule=Host(`portainer.mydomain.cc`)'
- 'traefik.http.routers.portainer.middlewares=portainer-https-redirect'
- 'traefik.http.middlewares.portainer-https-redirect.redirectscheme.scheme=https'
- 'traefik.http.routers.portainer-secure.entrypoints=https'
- 'traefik.http.routers.portainer-secure.rule=Host(`portainer.mydomain.cc`)'
- 'traefik.http.routers.portainer-secure.tls=true'
- 'traefik.http.routers.portainer-secure.service=portainer'
- 'traefik.http.services.portainer.loadbalancer.server.port=9443'
- 'traefik.http.services.portainer.loadbalancer.server.scheme=https'
- 'traefik.docker.network=broker'
For completeness sake, here is my Traefik container with labels:
version: '3.7'
services:
traefik:
image: traefik:latest
container_name: traefik
restart: always
network_mode: broker
security_opt:
- no-new-privileges:true
environment:
- CF_API_EMAIL=myemailaddress@gmail.com
- CF_DNS_API_TOKEN=mycloudflaretoken
ports:
- '80:80'
- '443:443'
volumes:
- '/etc/localtime:/etc/localtime:ro'
- '/var/run/docker.sock:/var/run/docker.sock:ro'
- '/mnt/docker/mapped/config/traefik/config/traefik.yml:/traefik.yml:ro'
- '/mnt/docker/mapped/config/traefik/config/acme.json:/acme.json'
- '/mnt/docker/mapped/config/traefik/data:/data'
labels:
- 'traefik.enable=true'
- 'traefik.http.routers.traefik.entrypoints=http'
- 'traefik.http.routers.traefik.rule=Host(`traefik.mydomain.cc`)'
- 'traefik.http.routers.traefik.middlewares=traefik-https-redirect'
- 'traefik.http.routers.traefik-secure.entrypoints=https'
- 'traefik.http.routers.traefik-secure.rule=Host(`traefik.mydomain.cc`)'
- 'traefik.http.routers.traefik-secure.middlewares=traefik-auth'
- 'traefik.http.routers.traefik-secure.tls=true'
- 'traefik.http.routers.traefik-secure.tls.certresolver=cloudflare'
- 'traefik.http.routers.traefik-secure.tls.domains[0].main=mydomain.cc'
- 'traefik.http.routers.traefik-secure.tls.domains[0].sans=*.mydomain.cc'
- 'traefik.http.routers.traefik-secure.service=api@internal'
- 'traefik.http.middlewares.traefik-auth.basicauth.users=myusername:mypasswordhash'
- 'traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https'
- 'traefik.http.middlewares.sslheader.headers.customrequestheaders.X-Forwarded-Proto=https'
Here is my trafeik.yml
config:
api:
dashboard: true
debug: true
entryPoints:
http:
address: ":80"
http:
redirections:
entryPoint:
to: https
scheme: https
https:
address: ":443"
serversTransport:
insecureSkipVerify: true
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
file:
directory: "/data/"
watch: true
certificatesResolvers:
cloudflare:
acme:
email: myemailaddress@gmail.com
storage: acme.json
dnsChallenge:
provider: cloudflare
resolvers:
- "1.1.1.1:53"
- "1.0.0.1:53"
Last but not least is the yml file I made in the /data/
directory, referenced in the traefik.yml
file:
http:
middlewares:
redirect-portainer-middleware:
redirectRegex:
permanent: true
regex: "http://portainer.local/(.*)"
replacement: "https://portainer.mydomain.cc/${1}"
routers:
redirect-portainer-router:
rule: "Host(`portainer.local`)"
entrypoints:
- http
middlewares:
- redirect-portainer-middleware
tls:
certresolver: "cloudflare"
service: "noop@internal"
I admit I'm a little in over my head as I just started with Traefik the other day, and that NGINX Proxy Manager had been relatively easy to configure in comparison to my attempt here. Using a wildcard cert from letsencrypt (obtained from cloudflare), all I had to do was make a redirection host and point it to my proxy host, and it always worked. Am I on the right track here, or am I way off base? Any help I can get would be appreciated.