【问题标题】:Traefik v2 as a reverse proxy without dockerTraefik v2 作为没有 docker 的反向代理
【发布时间】:2020-02-18 03:09:45
【问题描述】:

我已阅读文档,但不知道如何在不涉及 Docker 的情况下配置 Traefik v2 以替换 Nginx 作为网站(虚拟主机)的反向代理。理想情况下也会有 let'sencrypt https。

我有一个在 http://127.0.0.1:4000 运行的服务,我想将其反向代理到 http://myhost.com:80

这是我目前想出的配置:

[Global]
checkNewVersion = true

[log]
  level = "DEBUG"
  filePath = "log-file.log"

[accessLog]
  filePath =  "log-access.log"
  bufferingSize =  100

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

[http]
    [http.routers]
       [http.routers.my-router]
          rule = "Host(`www.myhost.com`)"
          service = "http"
          entrypoint=["http"]

    [http.services]
          [http.services.http.loadbalancer]
            [[http.services.http.loadbalancer.servers]]
              url = "http://127.0.0.1:4000"

【问题讨论】:

标签: reverse-proxy traefik


【解决方案1】:

我想通了, 首先要注意的是,在 traefik v2 中有两种类型的配置,静态和动态。所以我创建了两个文件,traefik.toml 和 traefik-dynamic.toml。

traefik.toml 的内容:

[log]
  level = "DEBUG"
  filePath = "log-file.log"

[accessLog]
  filePath =  "log-access.log"
  bufferingSize =  100

[providers]
  [providers.file]
    filename = "traefik-dynamic.toml"

[api]
  dashboard = true
  debug = true

[entryPoints]
  [entryPoints.web]
    address = ":80"
  [entryPoints.web-secure]
    address = ":443"
  [entryPoints.dashboard]
    address = ":8080"

[certificatesResolvers.sample.acme]
  email = "myemail@example.com"
  storage = "acme.json"

  [certificatesResolvers.sample.acme.httpChallenge]
    # used during the challenge
    entryPoint = "web"

traefik-dynamic.toml:

[http]
    # Redirect to https
    [http.middlewares]
      [http.middlewares.test-redirectscheme.redirectScheme]
        scheme = "https"

    [http.routers]
       [http.routers.my-router]
          rule = "Host(`www.example.com`)"
          service = "phx"
          entryPoints = ["web-secure"]
       [http.routers.my-router.tls]
          certResolver = "sample"

    [http.services]
          [http.services.phx.loadbalancer]
            [[http.services.phx.loadbalancer.servers]]
              url = "http://127.0.0.1:4000"

【讨论】:

【解决方案2】:

您还可以使用 Traefik v2 反向代理到在 localhost 上运行的服务,而无需使用 Nginx,正如此处使用 Traefik 的 File(而不是 Docker 提供程序)所解释的那样。

首先,通过更新/etc/hosts 将呼叫路由到myhost.comlocalhost,例如:

127.0.0.1 myhost.com

创建一个最小的docker-compose.yml 喜欢:

version: "3.7"
services:

  proxy:
    image: traefik:2.0
    command:
      - "--providers.file.filename=/etc/traefik/proxy-config.toml"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
    volumes:
      - ./proxy-config.toml:/etc/traefik/proxy-config.toml:ro

此 Compose 文件创建一个只读卷,其中包含根据请求代表 Nginx 的 Traefik 反向代理的动态配置。它使用 Traefik 的 File 提供程序而不是 Docker 和映射到端口 80 的空白 HTTP 地址作为入口点。这本身就是一个完整的 Compose 文件。除此之外,还需要 Traefik 的反向代理配置。

在同目录下配置Traefik反向代理proxy-config.toml

[http.routers.test-streamrouter]
  rule = "Host(`myhost.com`)"
  service = "test-loadbalancer"
  entryPoints = ["web"]

[[http.services.test-loadbalancer.loadBalancer.servers]]
  url = "http://host.docker.internal:4000"

这是一个完整的示例反向代理。它可以通过中间件进行增强,以执行 URL 重写、更新域名甚至重定向用户(如果这是您的目标)。如this answer 所示,使用单个负载均衡器。而host.docker.internal用于返回宿主的internal networking address

注意:在撰写本文时,"host.docker.internal" 仅适用于 Docker for Mac,并且在 Linux 上会失败。但是,您可以改用 Compose 服务名称(即"proxy")。

一旦你得到这个工作,你就可以设置 Let's Encrypt 东西或使用 TRAEFIK_PROVIDERS_FILE_FILENAME 环境变量在开发和生产配置之间交换。

【讨论】:

  • 我试过了,但我收到了502 Bad Gateway' caused by: dial tcp 127.0.0.1:8001: connect: connection refused" 错误。我的情况是我正在发送curl http://example.com:5050/storeData,并且我在端口:8001 上运行go application server ,并且此端口未公开。所以我尝试使用traefik 来侦听example.com:5050,然后将其转发到我的go application server 侦听端口8001
  • 我的 proxy.toml 文件 ``` [http.routers] [http.routers.myrouter] rule = "Host(example.com) && Path(/storeData))" service = "goserver " entryPoints = ["web"] [http.routers.myrouter1] rule = "Host(example.com) && Path(/getData))" service = "goserver" entryPoints = ["web"] [http.services] [http.services.goserver.loadBalancer] [[http.services.goserver.loadBalancer.servers]] url = "localhost:8001" ``
  • 我能够修复它。在proxy-config.toml 文件中,我将url 更改为http://instance_priv_IP:8001
【解决方案3】:

你可以

  1. 在同一桥接网络中使用容器名称而不是本地主机
  2. 链接不带@file 后缀的中间件和服务

请注意,在 yaml 和 toml 文件中,您需要注意属性的小写。而在docker中是loadbalancer,你需要在配置文件中写loadBalencer

http:
  middlewares:
    docs:
      stripPrefix:
        prefixes:
          - "/docs"
    restapi:
      stripPrefix:
        prefixes:
          - "/api/v1"
  routers:
    restapi:
      rule: "PathPrefix(`/api/v1`)"
      middlewares:
        - "restapi"
      service: "restapi"
      entryPoints:
        - http
    docs:
      rule: "PathPrefix(`/docs`)"
      middlewares:
        - "docs"
      service: "docs"
      entryPoints:
        - http
    client:
      rule: "PathPrefix(`/`)"
      service: "client"
      entryPoints:
        - http
    help:
      rule: "PathPrefix(`/server/sicon/help`)"
  services:
    restapi:
      loadBalancer:
        servers:
          - url: "http://sicon_backend:1881"
    docs:
      loadBalancer:
        servers:
          - url: "http://sicon_backend:1882"
    client:
      loadBalancer:
        servers:
          - url: "http://sicon_client"

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多