首页 > 解决方案 > Traefik v2 作为没有 docker 的反向代理

问题描述

我已经阅读了文档,但我不知道如何在不涉及 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-proxytraefik

解决方案


我想通了,首先要注意的是,在 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"


推荐阅读