如何使用traefik使内容同时显示在www和非www网址上

时间:2019-02-11 09:20:53

标签: traefik

谢谢你表现出兴趣,我很着急。任何帮助都会很棒。 目前,用户无法访问www.example.com,但可以访问example.com。

其中之一都可以:

1)接受来自WWW和非www网址的所有流量,并提供相同的内容。

2)将用户从WWW重定向到非www URL以显示内容。

注意:使用“加密”

我当前的配置是

traefik.toml

defaultEntryPoints = ["http", "https"]

[entryPoints]
  [entryPoints.dashboard]
    address = ":8080"
    [entryPoints.dashboard.auth]
      [entryPoints.dashboard.auth.basic]
        users = ["admin:key"]
  [entryPoints.http]
    address = ":80"
      [entryPoints.http.redirect]
      regex = "^https://www.(.*)"
      replacement = "https://$1"
      permanent=true
        entryPoint = "https"
  [entryPoints.https]
    address = ":443"
      [entryPoints.https.tls]

[api]
entrypoint="dashboard"

[acme]
email = "mail@gmail.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
  [acme.httpChallenge]
  entryPoint = "http"
[[acme.domains]]
  main = "*.example.com"
  sans = ["example.com"]
[[acme.domains]]
  main = "*.example1.com"
  sans = ["example1.com"]
[docker]
domain = "example.com"
watch = true
network = "proxy"`

docker-compose.yml:

 version: '2'
services:
  traefik:
    image: traefik
    restart: always
    command: --docker
    ports:
      - 80:80
      - 443:443
    networks:
      - proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - $PWD/traefik.toml:/traefik.toml
      - $PWD/acme.json:/acme.json
    container_name: trefik
    environment:
      DO_AUTH_TOKEN: TOKEN
    labels:
      - traefik.frontend.rule=Host:monitor.example.com
      - traefik.port=8080
  example1:
    image: wordpress:4.7.5-apache
    restart: always
    environment:
      WORDPRESS_DB_PASSWORD: something
    labels:
      - traefik.backend=example1
      - traefik.frontend.rule=Host:example1.com
      - traefik.docker.network=proxy
      - traefik.port=80
    networks:
      - internal
      - proxy
    depends_on:
      - mysql
  example:
   image: tutum/apache-php
   restart: always
   labels:
     - traefik.backend=example
     - traefik.frontend.rule=Host:example.com, www.example.com
     - traefik.docker.network=proxy
     - traefik.port=80
   networks:
     - internal
     - proxy

编辑#1:

Your config Redirects:

http://example.com => [no redirect]

https://www.example.com => [timeout]

http://www.example.com => [timeout]

http://example.com => [no redirect]

My Config Redirects:

http://example.com => https://example.com:443/

https://www.example.com => [timeout]

http://www.example.com => [timeout]

http://example.com => https://example.com:443/

1 个答案:

答案 0 :(得分:3)

  1. 您不能在同一入口点上同时使用重定向entrypointregex
  

请注意,如果为重定向定义了入口点,则不必在重定向结构中设置regexreplacement(在这种情况下将不使用它们)。

     

https://docs.traefik.io/v1.7/configuration/entrypoints/#redirect-http-to-https

  1. 无法通过HTTP挑战获得通配符证书:https://docs.traefik.io/v.7/configuration/acme/#wildcard-domains
    您必须使用DNS质询https://docs.traefik.io/v1.7/configuration/acme/#dnschallenge(已编辑) 并且在您问:不能同时使用(HTTP质询和DNS质询)。

修改

我将通过2个简单的配置来说明重定向(自签名证书而不是acme,但这是同一回事)。

这2种配置无需任何更改即可工作,您只需要执行docker-compose up

请注意,重定向对HTTP质询(ACME)没有影响。

Strip www和HTTPS重定向

目标:

$ curl --insecure -L http://www.whoami.docker.localhost
# http://www.whoami.docker.localhost -> https://whoami.docker.localhost

$ curl --insecure -L https://www.whoami.docker.localhost
# https://www.whoami.docker.localhost -> https://whoami.docker.localhost

$ curl --insecure -L http://whoami.docker.localhost
# http://whoami.docker.localhost -> https://whoami.docker.localhost

$ curl --insecure -L https://whoami.docker.localhost
# https://whoami.docker.localhost -> https://whoami.docker.localhost

I。没有TOML的示例:(docker-compose.yml

version: "3"

services:
  reverseproxy:
    image: traefik:v1.7.8
    command:
      - --logLevel=INFO
      - --defaultentrypoints=http,https
      - --entrypoints=Name:http Address::80 Redirect.Regex:^http://(?:www\.)?(.+) Redirect.Replacement:https://$$1 Redirect.Permanent:true
      - --entrypoints=Name:https Address::443 TLS Redirect.Regex:^https://www\.(.+) Redirect.Replacement:https://$$1 Redirect.Permanent:true
      - --docker
      - --docker.domain=docker.localhost
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  whoami:
    image: containous/whoami
    labels:
      - "traefik.frontend.rule=Host:whoami.docker.localhost"

II。 TOML的示例:(docker-compose.yml + traefik.toml

version: "3"

services:
  reverseproxy:
    image: traefik:v1.7.8
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml

  whoami:
    image: containous/whoami
    labels:
      - "traefik.frontend.rule=Host:whoami.docker.localhost"
defaultEntryPoints = ["http", "https"]

[entryPoints]
  [entryPoints.http]
    address = ":80"
    [entryPoints.http.redirect]
      regex = "^http://(?:www\.)?(.+)"
      replacement = "https://$1"
      permanent = true
  [entryPoints.https]
    address = ":443"
    [entryPoints.https.redirect]
      regex = "^https://www\\.(.+)"
      replacement = "https://$1"
      permanent = true
    [entryPoints.https.tls]

[api]

[docker]
domain = "docker.localhost"