How to forward request to external host

Hi dear Traefik Community,

I want to provide a Traefik endpoint that routes the request to GitHub in order to access the repository content. But I couldn't get it to work.

dynamic.yml

http:
  routers:
    github-router:
      rule: Host(`localhost`)
      service: github-service
      entryPoints:
        - github
      middlewares:
        - github-authentication
      tls: {}
  services:
    github-service:
      loadBalancer:
        servers:
          - url: "https://api.github.com/"
    httpbin-service:
      loadBalancer:
        servers:
          - url: "https://httpbin.org/"
  middlewares:
    github-authentication:
      headers:
        customRequestHeaders:
          Authorization: "Bearer <my-token-goes-here>"
          Accept: "application/vnd.github.v3.raw"
tls:
  certificates:
    - certFile: /etc/ssl/local/pubkey.pem
      keyFile: /etc/ssl/local/private.key

I am always getting a Bad Request response from GitHub when I call the Traefik endpoint with curl https://localhost. If I call curl -H 'Authorization: Bearer <my-token-goes-here>' -H 'Accept: application/vnd.github.v3.raw' https://api.github.com it's working fine.

The routing is working without any problem If I replace the github-service with the httpbin-service in http.routers.github-router.service and call curl https://localhost/get for example.

Could you please show me where I have made a mistake.

Many thanks in advance.

Try to target a Traefik whoami container to check if the headers are set correctly.

I verified the headers already with an Internet service, the httpbin-service. The headers seem to be set correctly:

{
  "args": {}, 
  "headers": {
    "Accept": "application/vnd.github.v3.raw", 
    "Accept-Encoding": "gzip, deflate, br, zstd", 
    "Accept-Language": "en-US,en;q=0.9", 
    "Authorization": "Bearer <token>", 
    "B3": "9cce9feaf14363bc6d0efa12908d2de1-cbbef21b4ec8c655-1", 
    "Cache-Control": "max-age=0", 
    "Host": "localhost", 
    "Priority": "u=0, i", 
    "Sec-Ch-Ua": "\"Not)A;Brand\";v=\"99\", \"Google Chrome\";v=\"127\", \"Chromium\";v=\"127\"", 
    "Sec-Ch-Ua-Mobile": "?0", 
    "Sec-Ch-Ua-Platform": "\"Linux\"", 
    "Sec-Fetch-Dest": "document", 
    "Sec-Fetch-Mode": "navigate", 
    "Sec-Fetch-Site": "none", 
    "Sec-Fetch-User": "?1", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36", 
    "X-Amzn-Trace-Id": "Root=1-66b5a878-2f53ee0d39080f5b107e4568", 
    "X-Forwarded-Host": "localhost:8082", 
    "X-Forwarded-Server": "traefik"
  }, 
  "origin": "172.18.0.1, <public-ip>", 
  "url": "https://localhost:8082/get"
}

Try to set passHostHeader to false, to not include localhost in the headers (doc).

1 Like

Fantastic :partying_face:. Now it works. Many thanks for your help.

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