Hi, I'm new to Traefik and I can't configure it for HTTP to HTTPS redirection, and www to no-www redirection.
I have read almost every doc and forum, and I think I might have a problem with the regexp.
Situation:
I have a docker-compose file in where I describe (among many) two services: Traefik, and a Nginx that hosts a web page. It is not the only web page under the domain (lets call it domain.com.ar), but every web page works fine, under https://domain.com.ar.
The domain is certified with Letsencrypt.
My issues:
-Whenever I type http://domain.com.ar, it redirects me to a 404 not found.
-Whenever I type https://www.domain.com.ar, it redirects me to a 404 not found (same as above).
So, http as well as www point me to a 404 not found.
My desired solution:
I would like the URL to be transformed to https://domain.com.ar/whatever_path.
I would like to set things with middlewares, since that is the approach that gave me the closest to a solution. But any help is appreciated.
This is the docker compose file.
services:
traefik:
image: "traefik:v2.10"
command:
- "--log.level=TRACE"
- "--api.dashboard=true"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=true"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.myresolver.acme.tlschallenge=true"
- "--certificatesresolvers.myresolver.acme.email=some-email@gmail.com"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
- "--entrypoints.web.http.tls.certresolver=myresolver"
ports:
- "80:80"
- "443:443"
volumes:
- "./letsencrypt:/letsencrypt"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
labels:
- "traefik.enable=true"
- "traefik.http.middlewares.stripprefix.stripprefix.prefixes=/traefik"
- "traefik.http.routers.traefik.rule=Host(`domain.com.ar`) && (PathPrefix(`/traefik`) || Headers(`Referer`, `https://domain.com.ar/traefik/dashboard/`))"
- "traefik.http.routers.traefik.entrypoints=websecure"
- "traefik.http.routers.traefik.tls.certresolver=myresolver"
- "traefik.http.routers.traefik.service=api@internal"
- "traefik.http.routers.traefik.middlewares=traefik-auth, traefik-stripprefix, non_www, https_redirect"
#two middleware declaration: https_redirect, that redirects http to https. And non_www, that redirects www.domain.com.ar to domain.com.ar
- "traefik.http.routers.http_catchall.rule=HostRegexp(`{any:.+}`)"
- "traefik.http.routers.http_catchall.entrypoints=web"
- "traefik.http.routers.http_catchall.middlewares=https_redirect"
- "traefik.http.middlewares.https_redirect.redirectscheme.scheme=https"
- "traefik.http.middlewares.https_redirect.redirectscheme.permanent=true"
- "traefik.http.middlewares.non_www.redirectregex.regex=^(http|https)://(?:www\\.)?(.+)"
- "traefik.http.middlewares.non_www.redirectregex.replacement=https://$${2}"
- "traefik.http.middlewares.non_www.redirectregex.permanent=true"
web:
image: nginx:latest
restart: always
volumes:
- ../ofivirtual/src/oficina-virtual/www/:/usr/share/nginx/html
labels:
- "traefik.enable=true"
- "traefik.http.routers.nginx.rule=Host(`domain.com.ar`)"
- "traefik.http.routers.nginx.entrypoints=websecure"
- "traefik.http.routers.nginx.tls.certresolver=myresolver"
- "traefik.http.routers.nginx.middlewares=https_redirect@docker,non_www@docker"
I just want to know if this is the correct approach or if I'm missing something important.