How route a domain to another backend?

Simple enough question, Ive tried googling and searching here...
I got to hosts one with Traefik and one where the app is running.

So I want to direct calls to a specific domain to another host.
This is what I guessed could work...

[frontends]
    [frontends.domain2]
    backend = "domain2"
        [fontends.domain2.routes.test_1]
        rule = "Host: domain2.io"

[backends]
    [backends.nextcloud]
    url = "https://10.20.1.80:8043"]
   
    [backends.domain2]
    url = "http://10.20.1.80:8002"]

But reading on under Traefik Basics... I found this that I need to play with:

[backends.backend1.servers.server1]
   url = "[http://172.17.0.2:80]"

Should I use the hosts IP:port or host-docker-ip:port?

You can use either the ip relative to the host (get it from docker network inspect), or something like "http://containername/urlpath/

You have some configuation errors:

The previous syntax is invalid

A valid example:

[frontends]
  [frontends.domain2]
    backend = "domain2"
      [fontends.domain2.routes.test_1]
        rule = "Host:domain2.io"

[backends]
  [backends.domain2]
    [backends.domain2.servers.server0]
      url = "http://10.20.1.80:8002"

https://docs.traefik.io/v1.7/configuration/backends/file/#reference

1 Like

If your application is a container, I recommend to use the Docker provider.

The IP of a container will change every time the container is restarted.

Or follow ratchet5000 comments:

First of thanks for the help, but I still not have been able to solve it.
In my traefik.toml file I got this:

[file]
  filename = "/home/admin/docker/traefik/rules.toml"
  watch = true

Inside that one I got this part, inspired (and probably messed up...)

[frontends]
  [frontends.cloud.grillgeek.se]
    backend = "nextcloud"
      [fontends.cloud.grillgeek.se.routes.test_1]
        rule = "Host:cloud.grillgeek.se"

[backends]
  [backends.nextcloud]
    [backends.cloud.servers.server0]
      url = "http://10.20.1.80:8040"

This gives me an 404. I know I have messed up... but I can't figure out were though.
Oh yeah forgot to say that this backend connection doesn't appear in the dashboard.

Hello @macmattias,

You have some issues with the naming in your toml.

Give this a try:

[frontends]
[frontends.cloud-grillgeek-se]
backend = "nextcloud"
    [frontends.cloud-grillgeek-se.routes]
      [frontends.cloud-grillgeek-se.routes.route0]
        rule = "Host:cloud.grillgeek.se"

[backends]
[backends.nextcloud]
    [backends.nextcloud.servers]
      [backends.nextcloud.servers.server0]
        url = "http://10.20.1.80:8040"
        weight = 1

No mattor where I place these front- backend the dashboard won't update.
First of it doesn't seem to pickup the files-tag at all.
I tried to put it inside the traefik.toml, nothing.

Here's my current traefik.toml:

logLevel = "DEBUG"

defaultEntryPoints = ["http", "https"]

[entryPoints]
    [entryPoints.http]
    address = ":80"
        [entryPoints.http.redirect]
            entryPoint = "https"
    [entryPoints.https]
    address = ":443"
        [entryPoints.https.tls]
    [entryPoints.dash]
    address = ":8080"
    [entryPoints.dash.auth]
        [entryPoints.dash.auth.basic]
            users = ["mattias:$apr1$va2Qcey9$1M9Or1Cm.O/vETCYJPh8o0"]

[file]
   filename = "rules.toml"
   watch = true

[api]
    entryPoint = "dash"
    dashboard = true

[acme]
    email = "my-mail@mail.com"
    storage = "acme.json"
    entryPoint = "https"
    #onHostRule = true
    [acme.dnsChallenge]
        provider = "cloudflare"
        delayBeforeCheck = 30
        resolvers = ["1.1.1.1"]

        [[acme.domains]]
          main = "*.grillgeek.se"
          sans = ["grillgeek.se"]
        [[acme.domains]]
          main ="sundsvallbbq.se"

[docker]
    domain = "grillgeek.se"
    watch = true
    network = "web"

And the rules path:

host:~/docker$ ll
total 8.0K
-rw-r--r-- 1 ladmin ladmin 1.8K Aug 16 06:19 docker-compose.yml
drwxr-xr-x 2 ladmin ladmin 4.0K Aug 15 15:34 traefik

host:~/docker$ ls -l traefik/
total 44
-rw------- 1 ladmin ladmin 32659 Aug 14 21:50 acme.json
-rw-r--r-- 1 ladmin ladmin   365 Aug 16 06:10 rules.toml
-rw-r--r-- 1 ladmin ladmin  1015 Aug 16 06:13 traefik.toml

And finally my rules.toml:

[frontends]
[frontends.cloud-grillgeek-se]
backend = "nextcloud"
    [frontends.cloud-grillgeek-se.routes]
      [frontends.cloud-grillgeek-se.routes.route0]
        rule = "Host:cloud.grillgeek.se"

[backends]
[backends.nextcloud]
    [backends.nextcloud.servers]
      [backends.nextcloud.servers.server0]
        url = "http://10.20.1.80:8040"
        weight = 1

I just cant seem to find wits wrong. So please help.

I'll throw in my docker-compose as well so you got it all:

version: '3.7'
services:

    traefik:
        image: traefik:v1.7.14-alpine
        container_name: traefik
        restart: always
        networks:
            - web
        ports:
            - 80:80
            - 443:443
            - 8080:8080
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock:ro
            - /home/ladmin/docker/traefik/traefik.toml:/traefik.toml
            - /home/ladmin/docker/traefik/acme.json:/acme.json
        environment:
            - CF_API_EMAIL=${CLOUDFLARE_EMAIL}
            - CF_API_KEY=${CLOUDFLARE_API_KEY}
        labels:
          - 'traefik.port=8080'
          - 'traefik.docker.network=web'
          - 'traefik.enable=true'
          - 'traefik.frontend.rule=Host:dash.localhost,dash.${DOMAINNAME}'

    grillgeek:
        image: swemattias/grillgeek
        container_name: grillgeek
        restart: always
        networks:
            - web
            - default
        expose:
            - 80
        labels:
            - 'traefik.docker.network=web'
            - 'traefik.enable=true'
            - 'traefik.frontend.rule=Host:${DOMAINNAME},www.${DOMAINNAME}'

    whoami:
        image: jwilder/whoami
        expose:
            - 8000
        labels:
            - 'traefik.docker.network=web'
            - 'traefik.enable=true'
            - 'traefik.frontend.rule=Host:whoami.${DOMAINNAME}'

    ombi:
        image: linuxserver/ombi
        container_name: ombi
        environment:
            - PUID=1000
            - PGID=1000
            - TZ=Europe/Stockholm
            - BASE_URL=ombi.grillgeek.se
        volumes:
            - ./ombi/:/config
        ports:
            - 3579:3579
        restart: unless-stopped
        labels:
            - 'traefik.docker.network=web'
            - 'traefik.enable=true'
            - 'traefik.frontend.rule=Host:ombi.${DOMAINNAME}'


#    sundsvallbbq:
#        image: swemattias/sundsvallbbq
#        container_name: sundsvallbbq
#        restart: always
#        networks:
#            - web
#            - default
#        expose:
#            - 80
#        labels:
#            - 'traefik.docker.network=web'
#            - 'traefik.enable=true'
#            - 'traefik.frontend.rule=Host:${DOMAINNAME2},www.${DOMAINNAME2}'

networks:
    web:
        external: true

As you can see I added Ombi, just to try, since whoamI works, I thought, hey, I don't need to add all services via rules.toml. I added Ombi and it appears inside the dashboard all looks as well.
But visiting the url (which you can do your self) gives me a Gateway timeout.
Visiting host-ip:3579 works just fine.

How hard can this be to get correct?

So now I have read the Basics instructions twice, and what I can understand this is how it should look:

[backends]
[backends.nextcloud]
    [backends.nextcloud.servers.server1]
    url = "http://10.20.1.80:8040"

[frontends]
  [frontends.nextcloud]
  backend = "nextcloud"
    [frontends.nextcloud.routes.test_1]
    rule = "Host: cloud.grillgeek.se"

Still it doesn't appear on the dashboard page. Is there anyway to see if traefik reads the rules-file?

Hi @macmattias, switching from file provider to docker provider as @ldez stated earlier is a good progress you made!

The "Bad gateway" issue is not a Traefik configuration issue: it means you did good (I've not read specifically in details though).
This error is related to docker networks: as you declare a network named web in your docker-compose.yml file, you also have to define that each service is connected to this network, which is not the default.

The services whoami and ombi you defined don't have any networks directive, it means they're only part of the default network of this docker-compose stack. As you defined the label traefik.docker.network=web for both, then it tells Traefik to reach the container through the network web.... which they are not part of. Try setting this label to default, or to add the services to the network web instead (both are ok).

I think I mentioned that I think it is a Docker-network problem earlier. Since I wrote I have decided to go Docker Swarm, one part is to simply the Traefik Docker setup but also gain HA and redundancy.