Traefik as reverse proxy server to external web server

Hello, I'm looking into Traefik as a reverse proxy server where I can inject routes via API (other options are Consul with Nginx and I think, Envoy).

I want to have a proxy.example.com proxy that terminates SSL (irrelevant for this question) and as a backend has a number of ephemeral web servers http://<ip>:<port> which are independent (not part of a LB pool) and are not part of the proxy server or related to Kubernetes or Docker. So when the user goes to [proxy.example.com/](https://proxy.example.com/)<uuid> (using a path) or [proxy.example.com](https://proxy.example.com/)?uuid=<uuid> (using query) or using header key/values, the user connects transparently with one of the servers.

I'm trying Traefik for this, not even using API but hardcoding an example, and I can't make it work. Traefik forwards traffic using PathPrefix for example and I can see the index but then all the files in the destination web server (./js/somefile.js) are 404 since it's the incorrect path.

I've tried stripping the path in the middleware like this Reverse proxy to external host - #4 by ldez and like they say, it doesn't work (using the Query in the router is the same thing).
I've also looked at How to use traefik to proxy a https request to an external server? and similar posts here but I can't find a solution.

I've been searching and I can't find the solution for this, any ideas or Traefik not the right tool? thanks!

My basic config is below, but I've tried other things:

cat docker-compose.yaml

version: '3'

services:
  reverse-proxy:
    # The official v2 Traefik docker image
    image: traefik:v2.8
    container_name: traefik
    command:
      - "--configFile=/etc/traefik/traefik.yml"
    ports:
      # The HTTP port
      - "80:80"
      # The Web UI (enabled by api.insecure)
      - "8080:8080"
    volumes:
      - /etc/traefik/traefik.yaml:/etc/traefik/traefik.yaml
      - /var/log/traefik/:/var/log/traefik/

cat /etc/traefik/traefik.yaml

log:
  level: DEBUG  # DEBUG, INFO, WARNING, ERROR, CRITICAL
  format: common  # common, json, logfmt
  filePath: /var/log/traefik/traefik.log

accesslog:
  format: common  # common, json, logfmt
  filePath: /var/log/traefik/access.log

api:
  insecure: true

entryPoints:
  web:
    address: ":80"
  traefik:
    address: ":8080"

providers:
  file:
    filename: /etc/traefik/dynamic.yaml

cat /etc/traefik/dynamic.yaml

http:
  routers:
    my-router:
      rule: "PathPrefix(`/foo`)"
      service: my-service
  services:
    my-service:
      loadBalancer:
        servers:
          - url: "http://2.142.149.48:8080/"

I too am having similar issues. Did you manage to figure it out? Hoping someone can chime in here.

If you want to forward requests to dedicated servers, you need to create a dedicated router+service for every one. In a dynamic config, loaded via provider.file for example.

Just conceptually, not real config;

routers:
  uuid1:
    rule: Host(`example.com`) && PathPrefix (`/uuid1`)
    service: uuid1
  uuid2:
    rule: Host(`example.com`) && PathPrefix (`/uuid2`)
    service: uuid2

services:
  uuid1:
    loadbalancer.server.url: http://1.2.3.4
  uuid2:
    loadbalancer.server.url: http://1.2.3.5

If you don’t want the path prefix in the target request, you can use middleware stripprefix to remove it.

Note that regular web-apps are not easily running behind path prefix. They are usually not path aware and may send redirects and links without the prefix, breaking functionality. Use sub-domains instead. It usually works for pure API request.