Hello,
I'm new to traefik and was trying to use it to add https redirection to a service that I'm running on a VM.
I have no trouble accessing the service using https://VM_IP_address:8443 or https://vm_name:8443 but I want to add htpp -> https redirection and IP -> vm's name (=domain name) redirection. When I'm accessing http://vm_name I am redirected toward https://vm_name as I want. However, when I am trying to access this URL (https://vm_name) as well as http://VM_IP_address:8443 http://vm_name:8443 I am getting an "Error 400 - bad request - The plain HTTP request was sent to HTTPS port".
Ideally I would like all those requests to redirect me toward https://vm_name(:8443).
Well, you can’t use http protocol with https port/entrypoint, in your case 8443, which expects a TLS/SSL connection. Usually http uses port 80 (or 8080).
Also https://<IP> redirection does not work, as TLS certs are linked to domain names, so you will only see a cert error in your browser when accessing.
So it's impossible to redirect from the http to the https, and it's also impossible to redirect from https://ip: to https://domain_name?
The first one is not a big problem but it would have been great to be able to do the second redirection...
But thanks for the answer! I not longer need to spend any more time on it.
Create a router with Host(<IP>) which uses redirect middleware to domain (doc).
Place http-to-https redirect on router with domain name (doc), not on http entrypoint.
I commented the definition of the ports for the backend and the frontend in the dockerfile, then I stopped and removed the containers and remade them but:
curl -v http://ip -> 404 page not found
curl -v http://domain -> 404 page not found
curl -v https://ip -> certificate error (normal) and 404 page not found
curl -v https://domain -> 400 plain HTTP request sent to HTTPS port
So without defining the ports for the frontend and backend, nothing works anymore.
Aww sorry for the dumb mistake, I missed it so many times...
So now I have access to the dashboard and I have the same error on the router and the middlewares for the Host(IP): "cannot create middleware: multi-types middleware not supported, consider declaring two different pieces of middleware instead"
So now I no longer have any error on the dashboard and I have the redirection from http://ip toward http://domain. However the http://domain page and the https://ip pages are returning me a 404 page not found error. The https://domain page is returning me a " 400 Bad Request
The plain HTTP request was sent to HTTPS port"
So I have a domain redirection but the https redirection is not working and I can't access the service (only accessible through port 8443 when I define the ports for all the services in the docker-compose).
Now http://ip and http://domain are redirected toward https://domain with the service running perfectly.
There's just the https://ip that still gives out a 404 page not found. Is it possible to juste change the regex in the following part or do I have to create a new router?
frontend:
build: ./frontend
image: pwndoc-ng/pwndoc-ng:frontend
container_name: pwndoc-ng-frontend
restart: always
labels:
- traefik.enable=true
# Router to redirect from IP to domain
- traefik.http.routers.pwndoc-ng-frontend-http.rule=Host(`ip`)
- traefik.http.routers.pwndoc-ng-frontend-http.middlewares=redirect
- traefik.http.middlewares.redirect.redirectregex.regex=^http://ip/(.*)
- traefik.http.middlewares.redirect.redirectregex.replacement=http://domain/$${1}
# Router to redirect from http domain to https domain
- traefik.http.routers.pwndoc-ng-frontend-https.rule=Host(`domain`)
- traefik.http.routers.pwndoc-ng-frontend-https.middlewares=redirect-secure
- traefik.http.middlewares.redirect-secure.redirectscheme.scheme=https
- traefik.http.middlewares.redirect-secure.redirectscheme.permanent=true
- traefik.http.routers.pwndoc-ng-frontend-https.tls=true
- traefik.http.services.pwndoc-ng-frontend.loadBalancer.server.port=8443
- traefik.http.services.pwndoc-ng-frontend.loadBalancer.server.scheme=https