如何用Traefik强制www

问题描述 投票:0回答:1

我一直在学习Traefik作为各种Wordpress网站的反向代理,我在服务器上的Docker容器中运行。目前我在同一个ip上有2个域设置Wp并且工作正常。

我有问题让域重定向到www所以现在在http://example.com它只显示'索引'我试图通过htaccess强制重定向但这不工作所以我假设我在Traefik设置中必须做的事情?

这是我的docker-compose.yml文件,但我只在这里包含了重要的部分:

services:
  reverse-proxy:
    image: traefik # The official Traefik docker image
    command: --api --docker # Enables the web UI and tells Træfik to listen to docker
    ports:
      - "80:80"     # The HTTP port
      - "8080:8080" # The Web UI (enabled by --api)
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
      - ./traefik.toml:/traefik.toml

wp-web2:
    container_name: cowork-wp
    image: wordpress:latest
    restart: always
    environment:
      WORDPRESS_DB_HOST: mysql-db-cowork
      WORDPRESS_DB_USER: foo
      WORDPRESS_DB_NAME: foo
      WORDPRESS_DB_PASSWORD: passwd123
    depends_on:
      - mysql-db-cowork
    labels:
      - "traefik.frontend.rule=Host:example.com"
      - "traefik.frontend.rule=Host:www.example.com"
      - "traefik.frontend.forcehost:www.example.com"
      #- "traefik.frontend.rule=Path: /"
    volumes:
      - ./src-tdm:/var/www/html

正如你在标签中看到的,我一直在尝试不同的主机以及'forcehost',但这些似乎都没有起作用。

这是toml文件:

defaultEntryPoints = ["http"]

[entryPoints]
  [entryPoints.http]
  address = ":80"

#Define Docker Backend Configuration
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "example.com"
watch = true
exposedByDefault = true

最后这是我从.htaccess强制www重写规则

#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301,NC]
.htaccess docker traefik
1个回答
0
投票

仔细检查文档表明,需要在前端Host:参数中使用逗号定义不同的域,例如:

- "traefik.frontend.rule=Host:www.example.com, example.com"
© www.soinside.com 2019 - 2024. All rights reserved.