利用 traefik 2 实现反向代理

traefik是go语言开发的一个类似ngnix的反向代理软件,本文采用windows单机安装traefik 2 实现反向代理(未采用docker),安装配置如下:
1、下载traefik二进制可执行文件
下载地址
2、解压后traefik目录下包括traefik.exe文件,在该目录下建立两个文件分别为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 = "[email protected]"
  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.router-1]
          rule = "Host(`www.zhizhicloud.com`)"
          service = "console"
          entryPoints = ["web-secure"]
       [http.routers.router-2]
          rule = "Host(`www.zhizhicloud.com`) && PathPrefix(`/huhehaote/yxhk/`)"
          service = "huhehaote"
          entryPoints = ["web"]
       [http.routers.router-1.tls]
          certResolver = "sample"

    [http.services]
          [http.services.console.loadbalancer]
            [[http.services.console.loadbalancer.servers]]
              url = "http://127.0.0.1:11778"
          [http.services.huhehaote.loadbalancer]
            [[http.services.huhehaote.loadbalancer.servers]]
              url = "http://127.0.0.1:8000/huhehaote/yxhk/"

    [http.routers.my-api]
      rule = "Host(`traefik.zhizhicloud.com`)"
      entrypoints = ["web", "web-secure"]
      service = "api@internal"
      middlewares = ["auth"]

    [http.middlewares.auth.basicAuth]
      users = [
        "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
      ]

traefik-dynamic.toml文件中的www.zhizhicloud.com和traefik.zhizhicloud.com域名需要指向本机地址。
通过访问http://traefik.zhizhicloud.com/dashboard/可以访问到traefik的dashboard页面如下:

利用 traefik 2 实现反向代理_第1张图片
image.png

www.zhizhicloud.com域名反向到127.0.0.1:11778地址及端口。
www.zhizhicloud.com/huhehaote/yxhk/反向到 http://127.0.0.1:8000/huhehaote/yxhk/地址。
这样通过traefik实现了基本的域名及路由地址到指定地址端口的反向代理。
总结:
正向代理就是本机访问外部服务配置代理转向,反向代理就是外部(如互联网手机端)通过域名或IP访问本地服务所需代理解析。本文只用traefik 2配置域名反向代理访问本机服务,正向代理配置基本相同不赘述。

你可能感兴趣的:(利用 traefik 2 实现反向代理)