gpt4 book ai didi

reverse-proxy - 如何将 traefik 配置为单个域和多个路径的反向代理

转载 作者:行者123 更新时间:2023-12-03 01:35:11 25 4
gpt4 key购买 nike

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

我的环境:

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

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

网络应用程序: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 = "my-email@domain.com"
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(通过标签)将请求重定向到在主机本身上运行的应用程序时,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”参数即可。

其他配置都OK!!

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com