如何在docker-compose.yml中正确配置traefik?
How to properly configure traefik in docker-compose.yml?
我正在玩 Traefik 并尝试设置它,但我感觉它看不到 traefki.yml 配置文件,因为从 http 到 https 的重定向不起作用,当我尝试访问 mydomain.com 时,我得到“无法访问此站点”或“404 未找到”。我不明白是什么问题,看来我是按照文档做的。
我的docker-compose.yml:
version: "3.8"
services:
traefik:
image: "traefik:v2.6"
container_name: "reverse-proxy"
command:
# Enable Docker in Traefik, so that it reads labels from Docker services
- "--providers.docker=true"
# Do not expose all Docker services, only the ones explicitly exposed
- "--providers.docker.exposedbydefault=false"
- "--providers.file.directory=/etc/traefik/dynamic"
ports:
# Listen on port 80, default for HTTP, necessary to redirect to HTTPS
- 80:80
# Listen on port 443, default for HTTPS
- 443:443
volumes:
# Add Docker as a mounted volume, so that Traefik can read the labels of other services
- /var/run/docker.sock:/var/run/docker.sock:ro
# Mount the dynamic configuration
- ./data/traefik.yml:/etc/traefik/dynamic/traefik.yml
# Mount the directory containing the certs
- ./certs:/etc/certs/
networks:
# Use the public network created to be shared between Traefik and
# any other service that needs to be publicly available with HTTPS
- reverse-proxy-public
landing:
container_name: "landing-page"
restart: always
build:
context: "landing/nginx"
dockerfile: "Dockerfile"
volumes:
- ./landing/nginx/conf.d:/etc/nginx/conf.d
labels:
- "traefik.enable=true"
- "traefik.http.routers.landing.entrypoints=websecure"
- "traefik.http.routers.landing.rule=Host(`mydomain.com`)"
- "traefik.http.services.landing.loadbalancer.server.port=8000"
- "traefik.http.routers.landing.tls=true"
networks:
- reverse-proxy-public
networks:
# Use the previously created public network "reverse-proxy-public", shared with other
# services that need to be publicly available via this Traefik
reverse-proxy-public:
external: true
我的traefik.yml:
global:
checkNewVersion: true
sendAnonymousUsage: false # true by default
# Log information
# ---
log:
level: ERROR # DEBUG, INFO, WARNING, ERROR, CRITICAL
format: common # common, json, logfmt
filePath: /var/log/traefik/traefik.log
# Accesslog
# ---
accesslog:
format: common # common, json, logfmt
filePath: /var/log/traefik/access.log
# Entry Points configuration
# ---
entryPoints:
web:
address: :80
# Redirect to HTTPS
# ---
http:
redirections:
entryPoint:
to: websecure
scheme: https
websecure:
address: :443
# Overwrite Default Certificates
# ---
tls:
stores:
default:
defaultCertificate:
certFile: /etc/certs/mydomain.com.crt
keyFile: /etc/certs/mydomain.com.key
您将 traefik.yml
复制到 /etc/traefik/dynamic
。
启动时,Traefik 在以下位置搜索名为 traefik.yml(或 traefik.yaml 或 traefik.toml)的文件:
- /etc/traefik/
- $XDG_CONFIG_HOME/
- $HOME/.config/
- 。 (工作目录)。
您可以使用 configFile 参数覆盖它,因此只需添加:
version: "3.8"
services:
traefik:
image: "traefik:v2.6"
container_name: "reverse-proxy"
command:
- "--configFile=etc/traefik/dynamic/traefik.yml"
...
我正在玩 Traefik 并尝试设置它,但我感觉它看不到 traefki.yml 配置文件,因为从 http 到 https 的重定向不起作用,当我尝试访问 mydomain.com 时,我得到“无法访问此站点”或“404 未找到”。我不明白是什么问题,看来我是按照文档做的。
我的docker-compose.yml:
version: "3.8"
services:
traefik:
image: "traefik:v2.6"
container_name: "reverse-proxy"
command:
# Enable Docker in Traefik, so that it reads labels from Docker services
- "--providers.docker=true"
# Do not expose all Docker services, only the ones explicitly exposed
- "--providers.docker.exposedbydefault=false"
- "--providers.file.directory=/etc/traefik/dynamic"
ports:
# Listen on port 80, default for HTTP, necessary to redirect to HTTPS
- 80:80
# Listen on port 443, default for HTTPS
- 443:443
volumes:
# Add Docker as a mounted volume, so that Traefik can read the labels of other services
- /var/run/docker.sock:/var/run/docker.sock:ro
# Mount the dynamic configuration
- ./data/traefik.yml:/etc/traefik/dynamic/traefik.yml
# Mount the directory containing the certs
- ./certs:/etc/certs/
networks:
# Use the public network created to be shared between Traefik and
# any other service that needs to be publicly available with HTTPS
- reverse-proxy-public
landing:
container_name: "landing-page"
restart: always
build:
context: "landing/nginx"
dockerfile: "Dockerfile"
volumes:
- ./landing/nginx/conf.d:/etc/nginx/conf.d
labels:
- "traefik.enable=true"
- "traefik.http.routers.landing.entrypoints=websecure"
- "traefik.http.routers.landing.rule=Host(`mydomain.com`)"
- "traefik.http.services.landing.loadbalancer.server.port=8000"
- "traefik.http.routers.landing.tls=true"
networks:
- reverse-proxy-public
networks:
# Use the previously created public network "reverse-proxy-public", shared with other
# services that need to be publicly available via this Traefik
reverse-proxy-public:
external: true
我的traefik.yml:
global:
checkNewVersion: true
sendAnonymousUsage: false # true by default
# Log information
# ---
log:
level: ERROR # DEBUG, INFO, WARNING, ERROR, CRITICAL
format: common # common, json, logfmt
filePath: /var/log/traefik/traefik.log
# Accesslog
# ---
accesslog:
format: common # common, json, logfmt
filePath: /var/log/traefik/access.log
# Entry Points configuration
# ---
entryPoints:
web:
address: :80
# Redirect to HTTPS
# ---
http:
redirections:
entryPoint:
to: websecure
scheme: https
websecure:
address: :443
# Overwrite Default Certificates
# ---
tls:
stores:
default:
defaultCertificate:
certFile: /etc/certs/mydomain.com.crt
keyFile: /etc/certs/mydomain.com.key
您将 traefik.yml
复制到 /etc/traefik/dynamic
。
启动时,Traefik 在以下位置搜索名为 traefik.yml(或 traefik.yaml 或 traefik.toml)的文件:
- /etc/traefik/
- $XDG_CONFIG_HOME/
- $HOME/.config/
- 。 (工作目录)。
您可以使用 configFile 参数覆盖它,因此只需添加:
version: "3.8"
services:
traefik:
image: "traefik:v2.6"
container_name: "reverse-proxy"
command:
- "--configFile=etc/traefik/dynamic/traefik.yml"
...