Forward traffic to internal host

Hello. Wondering if someone could point me in the right direction. I'm pretty new with this.
I have an internal server called "map.internal.com" that I want to expose through traefik. I'm not quite sure how to go about this. Does anyone have an example or even suggestions how to accomplish this? I'm using an yaml file.

1 Like

I would like a straight up example of the following NGINX configuration:

events { }

http {
    upstream mop {
        server mop:3000;
    }
    server {
        listen   80;
        server_name  localhost;
        location /mop {
            proxy_pass http://mop;
        }
        location /mop/1 {
            proxy_pass http://mop;
        }
        location /mop/2 {
            proxy_pass http://mop;
        }
    }
}

Are you asking a question or could this be a potential solution to the problem?

I'm asking a question. I'm using v2.1.4 and I can't for the sake of saving my own life get a simple config that works!

I have succeed doing this by the following steps:

  1. define in your docker-compose.yml a traefik file provider for your traefik service. For this I have added the following 2 lines to my command section.
      - "--providers.file.filename=config.yml"
      - "--providers.file.watch=true"
  1. I have created a config.yml with following content (this adds 2 routes to my node-red UI running on 2 raspberry pi nodes).
http:
  routers:
    pi3one:
      entryPoints:
        - "websecure"
      rule: "Host(`pi3one.XXXXX`)"
      middlewares:
        - traefik_auth@docker
        - add_ui_to_path
      tls:
        certResolver: mytlschallenge
      service: pi3one
    pi3three:
      entryPoints:
        - "websecure"
      rule: "Host(`pi3three.XXXXX`)"
      middlewares:
        - traefik_auth@docker
        - add_ui_to_path
      tls:
        certResolver: mytlschallenge
      service: pi3three

  services:
    pi3one:
      loadBalancer:
        servers:
          - url: "http://192.168.1.131:1880"
        passHostHeader: true
    pi3three:
      loadBalancer:
        servers:
          - url: "http://192.168.1.33:1880"
        passHostHeader: true

  middlewares:
    add_ui_to_path:
      replacePathRegex:
        regex: "^/(.*)"
        replacement: "/ui/$1"
  1. Then you must assure that the above config.yml file is copied to the /config.yml so that traefik service finds this at the right location. For this I have created a simple Dockerfile with following content:
FROM traefik
COPY ./config.yml /config.yml
  1. So I have put above Dockerfile and config.yml in a traefik subfolder.
    image

  2. The full definition of my traefik service in the docker-compose.yml =

  traefik:
    build: traefik
    container_name: "traefik"
    command:
      #- "--log.level=DEBUG"
      #- "--accesslog=true"
      - "--api.dashboard=true"
      #- "--api.insecure=true"
      - "--providers.file.filename=config.yml"
      - "--providers.file.watch=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.mytlschallenge.acme.tlschallenge=true"
      - "--certificatesresolvers.mytlschallenge.acme.email=${MY_EMAIL_ADDRESS}"
      - "--certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json"
    ports:
      - "80:80"
      - "443:443"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./letsencrypt:/letsencrypt"
    labels:
      - "traefik.enable=true"
    # Tell Traefik to use the port opened by portainer to connect to `my-container`
      - traefik.http.services.traefik.loadbalancer.server.port=8080
    # HTTP (LAN) connection
      - "traefik.http.routers.traefik_lan.rule=Host(`traefik.${LAN_HOSTNAME}`)"
      - "traefik.http.routers.traefik_lan.service=api@internal"
      - "traefik.http.routers.traefik_lan.entrypoints=web"
    # HTTPS (WAN) connection
      - "traefik.http.routers.traefik_wan.rule=Host(`traefik.${WAN_HOSTNAME}`)"
      - "traefik.http.routers.traefik_wan.service=api@internal"
      - "traefik.http.routers.traefik_wan.entrypoints=websecure"
      - "traefik.http.routers.traefik_wan.tls.certresolver=mytlschallenge"
      - "traefik.http.routers.traefik_wan.middlewares=traefik_auth"
      - "traefik.http.middlewares.traefik_auth.basicauth.users=${BASIC_AUTH_USER_PASSWORD}"
      - "traefik.http.middlewares.traefik_auth.basicauth.removeheader=true"

Nice! Thank you very much!

1 Like