docker Traefik中间件从www重定向到非www引发404错误

yduiuuwa  于 2023-03-01  发布在  Docker
关注(0)|答案(1)|浏览(136)

我创建了一个Traefik(v2.9.8)中间件,似乎是正确的检测Traefik(显示在 Jmeter 板中,它被应用到网站的HTTP路由器),但当我去www.example.com Traefik抛出404错误.
Docker组成:

version: "3.1"

services:
  traefik:
    restart: unless-stopped
    image: traefik:v2.9.8
    security_opt:
      - no-new-privileges:true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      # Allow Traefik to use the system's Docker socket
      # MUST be the same as in the config file "traefik.yml"
      - /var/run/docker.sock:/var/run/docker.sock:ro
      # Map the static conf into the container
      - ./config/traefik/traefik.yml:/etc/traefik/traefik.yml:ro
      # Map the dynamic conf into the container
      - ./config/traefik/config.yml:/etc/traefik/config.yml:ro
      - ./logs/traefik:/traefik-logs/
      # Map the certificates into the container
      - ./config/local-certs:/etc/certs:ro
    networks:
      - my_website
    environment:
      # Some env config, this works OK
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.tls=true"
      - "traefik.http.routers.traefik.tls.certresolver=myresolver"
      - "traefik.http.routers.traefik.entrypoints=https"
      - "traefik.http.routers.traefik.rule=Host(`${TRAEFIK_DASHBOARD_SUBDOMAIN}.${ENV_HOST}`)"

# Other services here...

  wordpress:
    build:
      context: ./wordpress
    image: wordpress:v1
    restart: unless-stopped
    volumes:
      # My volumes, working OK
    networks:
      - my_website
    environment:
      # Some env config, this works OK too
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wordpress.tls=true"
      - "traefik.http.routers.wordpress.entrypoints=https"
      - "traefik.http.routers.wordpress.middlewares=redirect-www@file"
      - "traefik.http.routers.wordpress.rule=Host(`${ENV_HOST}`)"

Traefik主文件(EOF处的中间件):

global:
  sendAnonymousUsage: false

api:
  dashboard: true
  insecure: false

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    watch: false
    exposedByDefault: false

  file:
    # We use the same file as in the proxy volume
    directory: /etc/traefik
    watch: true

log:
 # Path inside the container
 filePath: "/traefik-logs/traefik-service.log"
 # Alternative logging levels are: "DEBUG", "PANIC", "FATAL", "ERROR", "WARN", and "INFO".
 level: WARN
 format: common

entryPoints:
  http:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: https
          scheme: https
  https:
    address: ":443"

# Redirect www to non-www
http:
   middlewares:
     redirect-www:
       redirectRegex:
         regex: "^(https:\\/\\/)?www\\.(.+)"
         replacement: "https://${2}"
         permanent: true

我错过什么了吗?

e3bfsja2

e3bfsja21#

是的,我错过了一些东西:添加一个www到我的网站的服务主机标签。

- "traefik.http.routers.wordpress.rule=Host(`${ENV_HOST}`, `www.${ENV_HOST}`)"

这修复了问题,现在可以正常工作并从www重定向到非www。

相关问题