Redirect 如何让traefik从docker内部重定向到特定的非docker端口
首先,如果我没有使用正确的术语来问这个问题,我很抱歉,但我不符合现有的术语 我让traefik在docker容器中运行,并使用PathPrefix选项提供一些服务,例如,www.myserver.com/wordpress重定向到运行wordpress的docker容器 但我如何让它重定向到docker容器外部?具体来说,我如何让www.myserver.com重定向到我机器中的8080端口,以提供我在主机操作系统(而不是docker容器)中运行的服务 这是我的Redirect 如何让traefik从docker内部重定向到特定的非docker端口,redirect,networking,traefik,Redirect,Networking,Traefik,首先,如果我没有使用正确的术语来问这个问题,我很抱歉,但我不符合现有的术语 我让traefik在docker容器中运行,并使用PathPrefix选项提供一些服务,例如,www.myserver.com/wordpress重定向到运行wordpress的docker容器 但我如何让它重定向到docker容器外部?具体来说,我如何让www.myserver.com重定向到我机器中的8080端口,以提供我在主机操作系统(而不是docker容器)中运行的服务 这是我的traefik.toml: log
traefik.toml
:
logLevel = "DEBUG"
defaultEntryPoints = ["http", "https"]
[entryPoints]
[entryPoints.http]
address = ":80"
compress = false
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[acme]
email = "mymail@mail.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
#onDemand = true
[[acme.domains]]
main = "www.myserver.com"
[web]
address = ":8888"
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "www.myserver.com"
watch = true
exposedbydefault = false
loglevel = "ERROR"
[backends]
[backends.nasweb]
[backends.nasweb.servers.nasweb]
url = "http://192.168.1.11:8080"
[frontends]
[frontends.domain]
backend = "nasweb"
[frontends.domain.routes.domain]
rule = "Host:www.myserver.com"
traefik容器的mydocker compose.yml
:
version: "2"
services:
traefik:
image: traefik
network_mode: "host"
ports:
- "80:80"
- "443:443"
- "8888:8888"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ${SERVER_DIR}/AppData/traefik:/etc/traefik/
- ${PWD}/acme.json:/acme.json
- ${PWD}/traefik.toml:/etc/traefik/traefik.toml
- ${PWD}/servers.toml:/etc/traefik/servers.toml
restart: never
我已经找到了答案 在
traefik.toml
中添加:
################################################################
# File configuration backend
################################################################
# Enable file configuration backend
# Optional
[file]
filename = "servers.toml"
# Enable watch file changes
watch = true
在docker compose.yml中
将卷:
更改为:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ${SERVER_DIR}/AppData/traefik:/etc/traefik/
- ${PWD}/acme.json:/acme.json
- ${PWD}/traefik.toml:/etc/traefik/traefik.toml
- ${PWD}/servers.toml:/servers.toml
添加文件servers.toml
:
logLevel = "DEBUG"
defaultEntryPoints = ["http", "https"]
[entryPoints]
[entryPoints.http]
address = ":80"
compress = false
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[acme]
email = "mymail@mail.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
#onDemand = true
[[acme.domains]]
main = "www.myserver.com"
[web]
address = ":8888"
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "www.myserver.com"
watch = true
exposedbydefault = false
loglevel = "ERROR"
[backends]
[backends.nasweb]
[backends.nasweb.servers.nasweb]
url = "http://192.168.1.11:8080"
[frontends]
[frontends.domain]
backend = "nasweb"
[frontends.domain.routes.domain]
rule = "Host:www.myserver.com"
对于新的Traefik(v.2),您需要结合使用标签和外部文件,您可以在下面找到我的工作示例
在docker compose中,您需要添加命令来定义外部文件并启用提供程序
- "--providers.file=true"
- "--providers.file.filename=/etc/traefik/rules.toml"
在您的文件(rules.toml)中,输入到外部服务的路由(注意语法,使用char定义主机(`))
例如:
Docker compose:
traefik:
image: "traefik:v2.0.0"
container_name: "traefik"
restart: always
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.myhttpchallenge.acme.httpchallenge=true"
- "--certificatesresolvers.myhttpchallenge.acme.httpchallenge.entrypoint=web"
- "--certificatesresolvers.myhttpchallenge.acme.email=xx@xx.com"
- "--providers.file=true"
- "--providers.file.filename=/etc/traefik/rules.toml"
- "--providers.docker=true"
- "--providers.file.watch=true"
ports:
- "80:80"
- "8080:8080"
- "443:443"
networks:
- proxy
environment:
- CF_API_EMAIL="xx"
- CF_API_KEY="xx"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./traefik/rules.toml:/etc/traefik/rules.toml"
规则.toml
[http.routers]
# Define a connection between requests and services
[http.routers.nasweb]
rule = "Host(`nas.xxxx.com`)"
entrypoints = ["websecure"]
service = "nas"
[http.routers.nasweb.tls]
certResolver = "myhttpchallenge"
[http.services]
# Define how to reach an existing service on our infrastructure
[http.services.nas.loadBalancer]
[[http.services.nas.loadBalancer.servers]]
url = "http://192.168.0.165:80"
找到指向文档的链接: