Traefik and Nginx Webserver - Configuration File

Hi at all,
I have been running some websites for the last two years with an nginx webserver and traefik as reverse proxy in a docker-compose stack (as described in this guide). The Traefik configuration takes place as a dynamic CLI, in the same Docker Compose file.

Now I had to change my server setup and the docker container of nginx webserver runs in a different docker stack (different server) than traefik. However, it can be reached in the LAN via a fixed IP.

I would like to create a yaml file for the dynamic Traefik configuration that forwards the incoming requests to the IP of the nginx web server. The structure of the yaml file looks like this:

http:
  routers:
    nginx-website-rtr:
      rule: "Host(`website.$DOMAINNAME`)"
      entryPoints:
        - websecure
      middlewares:
        - "chain-no-auth@file"
      service: "nginx-svc"
  services:
    nginx-svc:
      loadBalancer:
        passHostHeader: true
        servers:
          - url: "http://192.168.xxx.xxx:80"

Unfortunately, till now I'm not able to open the website and I always get a 404 error message.
Can someone help me by solving this issue? Do I need to make changes in the nginx conf files?

Thanks for your advice in advance. - Daniel

Did you load the dynamic config file in static config with providers.file?

You can connect nodes easily using Docker Swarm, then Traefik can do configuration discovery across nodes, see simple Traefik Swarm example.

What does Traefik debug log and dashboard tell you?

Hi, thanks for the advices. The dynamic config file is loaded correctly. I mounted a "rules" folder, which contains several other config files, that all work well. In traefik logs (debug mode) I have no entrys, related to this issue. Docker Swarm is actually no alternative solution for my setup. - So maybe there are other ideas, how to solve the problem. - Thanks for your help in advance.

Compare to simple Traefik forward example.

Will you target service respond when using the original domain in Host header, or will it 404?

What does Traefik access log in JSON format tell you (OriginStatus, DownstreamStatus)?

Hi, and thanks again for the advices. I was able to solve the issue.

One part of the problem was related to the environment variable DOMAINNAME, which cannot be named $DOMAINNAME in the dynamic Traefik configuration file, otherwise it will not be recognized. Instead, the variable must be addressed as follows: {{env "DOMAINNAME"}}
I'm not sure if this is the only way, but that's definitely how it works and traefik uses the correct domain.

The entire affected line of code then looks like this:

       rule: "Host(`website.{{env "DOMAINNAME"}}`)" 

My second mistake was, that I had to explicitly expose the port in the nginx docker container, through which the container should be accessible:

     ports:
       - 80:80

In the previous CLI traefik configuration, which were running in the same Docker stack, this was understandably not necessary.

Thanks for help and best regards - Daniel

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