我将Traefik配置为在我的开发环境中充当反向代理。我目前有在不同端口和不同PATH上运行的应用程序。

我的环境:

Traefik在主机(192.168.0.10)上运行。监听端口80、443和8080(traefik仪表板)。

我的应用程序在其他主机(192.168.0.11)上运行。

Web应用程序:192.168.0.11:8200/web1
后端:192.168.0.11:8210/api1
其他网络应用程序:192.168.0.11:8300/web2
其他后端:192.168.0.11:8310/api2

我想使用Traefik + LetsEncrypt(acme)通过同一个子域(dev.domain.com)重定向所有这些应用程序。

例如:

当我访问dev.domain.com/web1时,我想将所有访问重定向到192.168.0.11:8200/web1

当我访问dev.domain.com/api1时,我想将所有访问重定向到192.168.0.11:8210/api1

等等..

以下是我正在使用的设置,Traefik版本等。

traefil.toml

debug = true
logLevel = "DEBUG"
InsecureSkipVerify = false
defaultEntryPoints = ["https", "http"]

[api]
  entryPoint = "traefik"
  dashboard = true
  address = ":8080"

[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]

[file]
  directory = "/etc/traefik/rules.d"
  watch = true

[acme]
email = "[email protected]"
storage="/etc/traefik/acme/acme.json"
entryPoint = "https"
acmeLogging=true
onDemand = true
[acme.dnsChallenge]
  provider = "godaddy"
  delayBeforeCheck = 0
[[acme.domains]]
   main = "domain.com"
[[acme.domains]]
   main = "*.domain.com"

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "domain.com"
watch = true
exposedbydefault = false

rules.d目录具有多个.toml文件。

web1.toml
loglevel = "ERROR"

[backends]
  [backends.web-backend]
    [backends.web-backend.servers.backend_web-backend1]
      url = "http://192.168.0.11:8200/web1"

[frontends]
  [frontends.web-frontend]
    backend = "web-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.web-frontend.routes.frontend_web-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/web1"

web2.toml
loglevel = "ERROR"

[backends]
  [backends.web-backend]
    [backends.web-backend.servers.backend_web-backend1]
      url = "http://192.168.0.11:8300/web2"

[frontends]
  [frontends.web-frontend]
    backend = "web-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.web-frontend.routes.frontend_web-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/web2"

api1.toml
loglevel = "ERROR"

[backends]
  [backends.api-backend]
    [backends.api-backend.servers.backend_api-backend1]
      url = "http://192.168.0.11:8210"

[frontends]
  [frontends.api-frontend]
    backend = "api-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.api-frontend.routes.frontend_api-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/api1"

api2.toml
loglevel = "ERROR"

[backends]
  [backends.api-backend]
    [backends.api-backend.servers.backend_api-backend1]
      url = "http://192.168.0.11:8310"

[frontends]
  [frontends.api-frontend]
    backend = "api-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.api-frontend.routes.frontend_api-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/api2"

acme目录可以!证书创建没有错误!

docker-compose.yml
version: "2.1"

services:
  traefik:
    hostname: traefik
    image: traefik:latest
    container_name: traefik
    restart: always
    domainname: ${DOMAINNAME}
    networks:
      - default
      - traefik_proxy
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    environment:
      - GODADDY_API_KEY=${GODADDY_API_KEY}
      - GODADDY_API_SECRET=${GODADDY_API_SECRET}
    labels:
      - "traefik.enable=true"
      - "traefik.backend=traefik"
      - "traefik.frontend.rule=Host:traefik.${DOMAINNAME}"
      - "traefik.port=8080"
      - "traefik.docker.network=traefik_proxy"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /opt/traefik:/etc/traefik
      - /opt/traefik/shared:/shared

  helloworld:
    image: matheuscarino/simple-nodejs-app:latest
    container_name: helloworld
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - FOO=BAR
    networks:
      - traefik_proxy
    labels:
      - "traefik.enable=true"
      - "traefik.backend=helloworld"
      - "traefik.frontend.rule=Host:helloworld.${DOMAINNAME}"
      - "traefik.port=3000"

networks:
  traefik_proxy:
    external:
      name: traefik_proxy
  default:
    driver: bridge

当我需要将请求重定向到通过Docker(通过Labels)在主机上运行的应用程序时,Traefik可以正常工作。我的helloworld.domain.com应用程序有效!

当我仅重定向一个应用程序时,Traefik可以正常工作。从我在同一子域中配置第二个应用程序的那一刻起,traefik在通过PATH的重定向中迷失了。

我在互联网上搜索了像我这样的用例,但没有找到使用Traefik来将应用程序重定向到Docker引擎,Kubernetes等外部的人。

最佳答案

您需要在前端“AddPrefix:/myprefix”中添加此参数,并删除后端URL中的路径,如下所示:
(url =“http://192.168.0.11:8200/myprefix”)到(url =“http://192.168.0.11:8200)

如果您的URL中包含PATH,则只需在前端配置中将此“路径”移动到“AddPrefix”参数。

其他所有配置都可以!!

web1.toml

loglevel = "ERROR"

[backends]
  [backends.web-backend]
    [backends.web-backend.servers.backend_web-backend1]
      url = "http://192.168.0.11:8200"

[frontends]
  [frontends.web-frontend]
    backend = "web-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.web-frontend.routes.frontend_web-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/web1;AddPrefix:/web1"

web2.toml
loglevel = "ERROR"

[backends]
  [backends.web-backend]
    [backends.web-backend.servers.backend_web-backend1]
      url = "http://192.168.0.11:8300"

[frontends]
  [frontends.web-frontend]
    backend = "web-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.web-frontend.routes.frontend_web-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/web2;AddPrefix:/web2"

api1.toml
loglevel = "ERROR"

[backends]
  [backends.api-backend]
    [backends.api-backend.servers.backend_api-backend1]
      url = "http://192.168.0.11:8210"

[frontends]
  [frontends.api-frontend]
    backend = "api-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.api-frontend.routes.frontend_api-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/api1;AddPrefix:/api1"

api2.toml
loglevel = "ERROR"

[backends]
  [backends.api-backend]
    [backends.api-backend.servers.backend_api-backend1]
      url = "http://192.168.0.11:8310"

[frontends]
  [frontends.api-frontend]
    backend = "api-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.api-frontend.routes.frontend_api-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/api2;AddPrefix:/api2"

关于reverse-proxy - 如何将traefik配置为单个域和多个路径的反向代理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52083071/

10-16 23:46