Attempting to use Host based routing in docker-compose and getting bad gateway

I have attempted to switch from Nginx to Traefik and it is not working. I am getting bad gateway from Traefik and even though I am running the most basic of configurations copied identically from the docker-compose basic Traefik example. It is not clear what I need to do.

I have the desire to run multiple services in docker-compose and each Dockerfile/image/service exposes different ports. let's say four services inside docker each expose their own custom port 801, 802, 803, 804. It would be advantageous to expose 3306 MySQL to the Host, but irrelevant to this particular discussion.

I got this working with nginx, using my-service as an upstream and nginx proxy based on the server name, without issue. With this config in nginx it works as expected...

upstream my.service {
    server my.service:801;

server {
    listen 80;
    server_name my.service;

    location / {
        proxy_pass         http://my.service;
        proxy_redirect     off;

Note: I have added my.service, other.service, third.service and fourth.service to /etc/hosts pointing to

Again, this works fine in nginx, but I'm trying to solve the issue (with Traefik autodiscovery) so as to not need to dynamically switch configurations if I only want my.service and fourth.service, or other.service and third.service.

So I created a branch, ripped out nginx, and tried replacing it with Traefik, working on the first service only, my.service, that exposes port 801 inside its Dockerfile and its apache site.conf, I don't need or want port 801 exposed to the host.

Here is my Traefik config in docker-compose:

    image: "traefik:v2.9"
    container_name: "traefik"
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "80:80"
      - "8080:8080"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "custom-network"

I added the following to my.service definition in docker-compose:

      - "custom-network"
      - "traefik.enable=true"
      - "`my.service`)"
      - ""

I docker-compose up and browse to http://my.service/page.php

  • I can't seem to understand why, but Traefik just gives a "bad gateway" error.
  • I don't want to do routing based on external ports, I want to use hostnames.
  • I don't want to add custom prefixes, either, and with nginx I don't have to.

I want:

  • http:// my .service --> port 80/default --> Traefik --> my.service 801
  • http:// other .service --> port 80/default --> Traefik --> other.service 802
  • http:// third .service --> port 80/default --> Traefik --> third.service 803
  • http:// fourth .service --> port 80/default --> Traefik --> fourth.service 804

(added spaces because I am not allowed to post links as a new user here)

The Traefik interface on 8080 works great and looks amazing. In fact it says successful routing for HTTP and Host(my.service) is listed. It says 3 HTTP services are configured, my.service being one of them.

From the Traefik debug logs:

time="2022-10-19T17:52:00Z" level=debug msg="Creating server 0" serverName=0 serviceName=my-service-folder-name entryPointName=web routerName=my-service@docker
time="2022-10-19T17:52:00Z" level=debug msg="Added outgoing tracing middleware my-service-folder-name" middlewareType=TracingForwarder entryPointName=web routerName=my-service@docker middlewareName=tracing
time="2022-10-19T18:02:48Z" level=debug msg="vulcand/oxy/roundrobin/rr: Forwarding this request to URL" ForwardURL="" Request="{\"Method\":\"GET\",\"URL\":{\"Scheme\":\"\",\"Opaque\":\"\",\"User\":null,\"Host\":\"\",\"Path\":\"/favicon.ico\",\"RawPath\":\"\",\"OmitHost\":false,\"ForceQuery\":false,\"RawQuery\":\"\",\"Fragment\":\"\",\"RawFragment\":\"\"},\"Proto\":\"HTTP/1.1\",\"ProtoMajor\":1,\"ProtoMinor\":1,\"Header\":{\"Accept\":[\"image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8\"],\"Accept-Encoding\":[\"gzip, deflate\"],\"Accept-Language\":[\"en-US,en;q=0.9\"],\"Connection\":[\"keep-alive\"],\"Cookie\":[\"_fbp=fb.1.1665634201114.1532094668; _gid=GA1.2.391617166.1666121711; _ga=GA1.1.993995607.1665634201; _ga_280913663=GS1.1.1666121711.1.0.1666121713.0.0.0\"],\"Referer\":[\"http://my.service/page.php\"],\"User-Agent\":[\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36\"],\"X-Forwarded-Host\":[\"my.service\"],\"X-Forwarded-Port\":[\"80\"],\"X-Forwarded-Proto\":[\"http\"],\"X-Forwarded-Server\":[\"f7e64b0518e1\"],\"X-Real-Ip\":[\"\"]},\"ContentLength\":0,\"TransferEncoding\":null,\"Host\":\"my.service\",\"Form\":null,\"PostForm\":null,\"MultipartForm\":null,\"Trailer\":null,\"RemoteAddr\":\"\",\"RequestURI\":\"/favicon.ico\",\"TLS\":null}"
time="2022-10-19T18:02:48Z" level=debug msg="'502 Bad Gateway' caused by: dial tcp connect: connection refused"

folder-name is the name of my (CWD) directory where the docker-compose file is (aka the cloned repository where my docker environment containing Traefik and the other services.

So somehow Traefik seems to not be getting my service port (801) from configuration correctly.

This seems simple, but I'm not sure what I am missing, if someone could give any insight I would be very grateful.

After adding this,

I get 404 page not found.

Yet, if I add this to my.service:

      - "801"

and browse http:// my .service:801/page.php - the page comes up, so I know the backend is working.

The error in the Traefik logs:

time="2022-10-19T20:37:33Z" level=error msg="field not found, node: port:801" providerName=docker container=my-service-folder-name-eacebede1aa78dd7054c116933c551782ae244e606c56b331130ebcd98557e1b

Should be:

  - ""

Working like a champ now!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.