簡體   English   中英

Traefik:如何使用 Docker 將 https 重定向到 http?

[英]Traefik: How do I redirect https to http with Docker?

我繼承了一個使用 Traefik 和 Docker 的項目,但我沒有使用 Traefik 的經驗,因此尋求一些幫助。 假設我有域https://xxx.testsite.com我被要求將任何流量從 https 重定向到 http。
我試過配置一個中間件來重定向,但它沒有用。

目前,如果我訪問http://xxx.testsite.com我的網站會加載。
如果我訪問https://xxx.testsite.com ,我會收到 404 頁面未找到。

我怎樣才能配置我的文件來完成這個?

我的項目目錄結構是

my_project/
├─ testsite/
│  ├─ docker-compose.yml
│  ├─ docker-compose-https.yml
│  ├─ web
│  │  ├─index.html
├─ traefik.yml
├─ traefik.toml
├─ acme.json

my_project/traefik.toml

[web]
logLevel = "DEBUG"
defaultEntryPoints = ["https","http"]

[entryPoints]
  [entryPoints.http]
  address = ":80"
  [entryPoints.dashboard]
    address = ":8088"
  [entryPoints.https]
  address = ":443"
  [entryPoints.https.tls]
[retry]

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "jltxxxx.com"
watch = true
exposedByDefault = false
network = "jh"

[acme]
email = "xxxxxx@gmail.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
[acme.httpChallenge]
entryPoint = "http"

my_project/traefik.yml

version: '2'

services:
  traefik:
        image: traefik:v1.7
    restart: unless-stopped
        networks:
          - testsite
    ports:
      - '8100:80'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./acme.json:/acme.json
    logging:
     options:
       max-size: 100m

networks:
  testsite:
    external:
      name: testsite_default

my_project/testsite/docker-compose.yml

version: '2'
services:
    web:
      image: 'flipbox/php:72-apache'
      restart: unless-stopped
      volumes:
          - '.:/var/www/html/'
      labels:
        - 'traefik.enable=true'
        - 'traefik.backend=testsite_web_1'
        - 'traefik.docker.network=testsite'
        - 'traefik.frontend.rule=Host:xxx.testsite.com, www.xxx.testsite.com'
      logging:
       options:
         max-size: 500m

my_project/testsite/docker-compose-https.yml

version: '2'
services:
    web:
      image: 'flipbox/php:72-apache'
      restart: unless-stopped
      volumes:
          - '.:/var/www/html/'
      labels:
        - 'traefik.enable=true'
        - 'traefik.port=80'
        - 'traefik.frontend.entryPoints=https'
        - 'traefik.backend=testsite_web_1'
        - 'traefik.docker.network=testsite'
        - 'traefik.frontend.rule=Host:xxx.testsite.com, www.xxx.testsite.com'    
      logging:
         options:
         max-size: 500m

我不熟悉 Traefik v1 語法,但我希望 Traefik v2 的這段代碼可以讓您了解如何在 v1 中執行此操作。 並為其他使用 Traefik v2 的人回答問題。

首先,這個配置中有很多地方看起來不太對。 舉個例子,static 配置中指定的端口與您在 Traefik 容器中公開的端口不匹配。

但回到重定向部分,有兩種選擇:

您可以通過將以下內容添加到 Traefik 容器的static配置,將所有傳入流量重定向到 HTTPS:

command:
    - "--entrypoints.websecure.address=:443"
    - "--entrypoints.web.address=:80"
    - "--entrypoints.web.http.redirections.entryPoint.to=websecure"
    - "--entrypoints.web.http.redirections.entryPoint.scheme=https"
    - "--entrypoints.web.http.redirections.entrypoint.permanent=true"

或者,您可以通過將以下內容添加到應用程序容器的動態配置中,將單個容器的流量重定向到 HTTPS:

labels:
    - "traefik.enable=true"
    - "traefik.http.middlewares.myapp-redirect.redirectscheme.scheme=https"
    - "traefik.http.middlewares.myapp-redirect.redirectscheme.permanent=true"
    - "traefik.http.routers.myapp.middlewares=myapp-redirect"
    - "traefik.http.routers.myapp.rule=Host(`myapp.localhost`)"
    - "traefik.http.routers.myapp.entrypoints=web"
    - "traefik.http.routers.myapp-secure.rule=Host(`myapp.localhost`)"
    - "traefik.http.routers.myapp-secure.entrypoints=websecure"
    - "traefik.http.routers.myapp-secure.tls=true"
    - "traefik.http.routers.myapp-secure.tls.certresolver=le"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM