Traefik 的简单反向代理示例 [英] Simple reverse proxy example with Traefik

查看:113
本文介绍了Traefik 的简单反向代理示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个简单的配置示例,将端口 80 上的传入 http 请求重定向到端口 8080 上的特定静态 ip 以供 traefik 使用?

Does anyone have a dead simple configuration example of an incoming http request on port 80 to be redirect to a specific static ip on port 8080 for traefik?

我在任何地方都找不到它!已经在他们的网站上呆了几个小时了.

I can't find it absolutely anywhere! Been on their website for a few hours now.

这是他们最好的例子 https://docs.traefik.io/routing/routers/#configuration-example:

This is their best example from here https://docs.traefik.io/routing/routers/#configuration-example:

## Forwarding all (non-tls) requests on port 3306 to a database service
## Static configuration
entryPoints:
  web:
    address: ":80"
  mysql:
    address: ":3306"   

我想要来自 NGINX 的类似以下内容:

I would like something like the following from NGINX:

events { }

http {
    upstream mop {
        server mop:3000;
    }
    server {
        listen   80;
        server_name  localhost;
        location /mop {
            proxy_pass http://mop;
        }
        location /mop/1 {
            proxy_pass http://mop;
        }
        location /mop/2 {
            proxy_pass http://mop;
        }
    }
}

推荐答案

对于这个简单的例子,在你的静态配置中你需要创建一个 entryPoint 和一个 provider.

For this simple example, in your static configuration you need to create an entryPoint and a provider.

entryPoint 定义了 traefik 将接受传入请求的端口和provider 定义了一些现有的基础设施组件,traefik 可以查询服务发现.这可以是一些编排系统,例如 DockerKubernetes.但在这个简单的例子中,我们需要一个 File 提供者.

The entryPoint defines on which port traefik will accept incoming requests and the provider defines some existing infrastructure component that traefik can query for service discovery. This can be some orchestration system like Docker or Kubernetes. But in this simple example we need a File provider.

# Static configuration
[entryPoints]
  [entryPoints.web]
    address = ":80"

[providers]
  [providers.file]
    filename = "/path/to/dynamic/conf.toml"

在文件提供程序中,我们定义了动态配置的路径.在动态配置中,我们创建了一个 service(或后端).这是我们希望 traefik 将请求路由到的服务器.

In the file provider we define the path to the dynamic configuration. In the dynamic configuration we create a service (or backend). This is the server to which we want traefik to route request.

我们还需要创建一个router.路由器匹配域或路径等规则,并将这些请求路由到所需的service.在这个例子中,我们将路径前缀 /(所有请求)路由到服务 my-service,然后指向 http://localhost:8080代码>.

We also need to create a router. The router matches rules such as domain or path and routes those requests to the desired service. In this example we route the path prefix / (all requests) to the service my-service, which then points to http://localhost:8080.

# Dynamic configuration
[http]
  [http.routers]
    [http.routers.my-router]
      rule = "PathPrefix(`/`)"
      service = "my-service"

    [http.services]
      [http.services.my-service.loadBalancer]
        [[http.services.my-service.loadBalancer.servers]]
          url = "http://localhost:8080"

这篇关于Traefik 的简单反向代理示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆