代码之家  ›  专栏  ›  技术社区  ›  goblin_rocket

如何用traefik强制WWW

  •  0
  • goblin_rocket  · 技术社区  · 7 年前

    我一直在学习traefik,它可以作为运行在服务器上Docker容器中的各种wordpress站点的反向代理。目前,我在同一个IP上有两个带wp的域,并且工作正常。

    我在让域名重定向到www时遇到问题,请立即访问 http://example.com 它只显示“index of”(索引),我试图通过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
    

    正如你在标签上看到的,我尝试过不同的主机以及“强制主机”,但这些似乎都没有奏效。

    这是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
    

    最后这是我的力量www重写规则从.htaccess

    #Force www:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301,NC]
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   goblin_rocket    7 年前

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

    - "traefik.frontend.rule=Host:www.example.com, example.com"
    
    推荐文章