Traefik 路径中的服务不会重定向到其他请求的服务路径

Service in Traefik path does not redirect to service path for additional requests

我有一个带有子域的 VM - 例如mysubdomain.notmydomain.com,如以下日志所示 - 运行单个节点 Docker Swarm(为简单起见)并使用 Traefik 作为各种服务的反向代理。我设法将不同服务的问题简化为我现在在这里分享的服务。

预期行为: 我到达了 mysubdomain.notmydomain。com/test 并且我看到了带有文本和呈现徽标的响应。

实际行为::我看到一个损坏的 link(见照片)

版本

Ubuntu 16.04.1 LTS xenial

Docker version 18.03.0-ce, build 0520e24

Traefik 1.5.4

已截断 docker-撰写文件

version: '3.4'

services:
  test:
    image: tutum/hello-world
    networks:
      - traefik-network
    deploy:
      labels:
        traefik.enable: "true"
        traefik.backend: "test"
        traefik.docker.network: "cat_traefik-network"
        traefik.frontend.rule: "Host:mysubdomain.notmydomain.com;PathPrefix:/test"
        traefik.port: 80
        traefik.frontend.passHostHeader: "true"
        traefik.domain: "mysubdomain.notmydomain.com"
  traefik:
    image: traefik
    ports: ["80:80", "443:443", "8080:8080"]
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - ./traefik/traefik.toml:/traefik.toml
    - ./traefik/acme.json:/acme.json
    networks:
    - traefik-network
    - mosquitto-network
    command:
      --api \
      --docker \
      --docker.swarmmode \
      --docker.domain=mysubdomain.notmydomain.com \
      --docker.watch \
      --logLevel=DEBUG
    deploy:
      placement:
        constraints: [node.role==manager]
      labels:
       traefik.enable: "false"

显示意外行为的屏幕截图

来自 Traefik 的日志

cat_traefik.1.ifbq38ugx76r@cat    | time="2018-04-03T20:42:39Z" level=debug msg="vulcand/oxy/roundrobin/rr: begin ServeHttp on request" Request="{"Method":"GET","URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/test","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-GB,en-US;q=0.9,en;q=0.8"],"Cache-Control":["max-age=0"],"Connection":["keep-alive"],"Cookie":["redirect_to=%252F"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"]},"ContentLength":0,"TransferEncoding":null,"Host":"mysubdomain.notmydomain.com","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"10.255.0.2:54617","RequestURI":"/test","TLS":null}"
cat_traefik.1.ifbq38ugx76r@cat    | time="2018-04-03T20:42:39Z" level=debug msg="vulcand/oxy/roundrobin/rr: Forwarding this request to URL" Request="{"Method":"GET","URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/test","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-GB,en-US;q=0.9,en;q=0.8"],"Cache-Control":["max-age=0"],"Connection":["keep-alive"],"Cookie":["redirect_to=%252F"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"]},"ContentLength":0,"TransferEncoding":null,"Host":"mysubdomain.notmydomain.com","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"10.255.0.2:54617","RequestURI":"/test","TLS":null}" ForwardURL=http://10.0.2.7:80
cat_traefik.1.ifbq38ugx76r@cat    | time="2018-04-03T20:42:39Z" level=debug msg="vulcand/oxy/forward: begin ServeHttp on request" Request="{"Method":"GET","URL":{"Scheme":"http","Opaque":"","User":null,"Host":"10.0.2.7:80","Path":"","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-GB,en-US;q=0.9,en;q=0.8"],"Cache-Control":["max-age=0"],"Connection":["keep-alive"],"Cookie":["redirect_to=%252F"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"]},"ContentLength":0,"TransferEncoding":null,"Host":"mysubdomain.notmydomain.com","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"10.255.0.2:54617","RequestURI":"/test","TLS":null}"
cat_traefik.1.ifbq38ugx76r@cat    | time="2018-04-03T20:42:39Z" level=debug msg="vulcand/oxy/forward/http: begin ServeHttp on request" Request="{"Method":"GET","URL":{"Scheme":"http","Opaque":"","User":null,"Host":"10.0.2.7:80","Path":"","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-GB,en-US;q=0.9,en;q=0.8"],"Cache-Control":["max-age=0"],"Connection":["keep-alive"],"Cookie":["redirect_to=%252F"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"]},"ContentLength":0,"TransferEncoding":null,"Host":"mysubdomain.notmydomain.com","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"10.255.0.2:54617","RequestURI":"/test","TLS":null}"
cat_traefik.1.ifbq38ugx76r@cat    | time="2018-04-03T20:42:39Z" level=debug msg="vulcand/oxy/forward/http: Round trip: http://10.0.2.7:80, code: 200, Length: 478, duration: 8.138087ms"
cat_traefik.1.ifbq38ugx76r@cat    | time="2018-04-03T20:42:39Z" level=debug msg="vulcand/oxy/forward/http: completed ServeHttp on request" Request="{"Method":"GET","URL":{"Scheme":"http","Opaque":"","User":null,"Host":"10.0.2.7:80","Path":"","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-GB,en-US;q=0.9,en;q=0.8"],"Cache-Control":["max-age=0"],"Connection":["keep-alive"],"Cookie":["redirect_to=%252F"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"]},"ContentLength":0,"TransferEncoding":null,"Host":"mysubdomain.notmydomain.com","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"10.255.0.2:54617","RequestURI":"/test","TLS":null}"
cat_traefik.1.ifbq38ugx76r@cat    | time="2018-04-03T20:42:39Z" level=debug msg="vulcand/oxy/forward: completed ServeHttp on request" Request="{"Method":"GET","URL":{"Scheme":"http","Opaque":"","User":null,"Host":"10.0.2.7:80","Path":"","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-GB,en-US;q=0.9,en;q=0.8"],"Cache-Control":["max-age=0"],"Connection":["keep-alive"],"Cookie":["redirect_to=%252F"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"]},"ContentLength":0,"TransferEncoding":null,"Host":"mysubdomain.notmydomain.com","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"10.255.0.2:54617","RequestURI":"/test","TLS":null}"
cat_traefik.1.ifbq38ugx76r@cat    | time="2018-04-03T20:42:39Z" level=debug msg="vulcand/oxy/roundrobin/rr: competed ServeHttp on request" Request="{"Method":"GET","URL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/test","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-GB,en-US;q=0.9,en;q=0.8"],"Cache-Control":["max-age=0"],"Connection":["keep-alive"],"Cookie":["redirect_to=%252F"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"]},"ContentLength":0,"TransferEncoding":null,"Host":"mysubdomain.notmydomain.com","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteAddr":"10.255.0.2:54617","RequestURI":"/test","TLS":null}"

日志显示前几个请求包含 Path:/test 而最后一个包含 Path:/。我认为这就是问题所在。

问题: 我需要如何或重新配置什么才能在包含 logo.png 的 tutum/hello-world 容器上正确加载次级请求?

选择tutum/hello-world图片进行测试,作为一个清晰的例子;实际堆栈包含 mosquitto、grafana、influxdb 和一些自定义服务,它们都显示相同的根行为。 (那些有 frontend.rule 的)`

如有任何建议,我们将不胜感激;我可以在需要时提供更多信息。

来自Github期#3124 by Julien Salleyron

Thanks for your interest in Træfik!

It's not an issue. Træfik does not rewrite the response body. We think that the process of rewriting the response body in a reverse proxy is too heavy/prone to errors.

We encourage you to either:

  • use host based rules only (seems impossible for your use case)
  • use specific Traefik path rewrite headers in you app (X-Forwarded-Prefix, X-Replaced-Path)
  • let your app listen on a specific path