Traefik的简单反向代理示例

时间:2020-02-14 13:26:55

标签: traefik

有人对端口80上传入的http请求进行重定向以将其重定向到端口8080上的特定静态ip进行交易吗?

我绝对不能在任何地方找到它!现在已经在他们的网站上停留了几个小时。

这是他们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获得以下内容:

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;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

entryPoint定义了traefik将在哪个端口上接受传入的请求,以及 provider定义了traefik可以查询服务发现的一些现有基础结构组件。这可以是诸如DockerKubernetes之类的编排系统。但是在这个简单的示例中,我们需要一个File提供程序。

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

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

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

我们还需要创建一个router。路由器匹配诸如域或路径之类的规则,并将这些请求路由到所需的service。在此示例中,我们将路径前缀/(所有请求)路由到服务my-service,服务指向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"

答案 1 :(得分:1)

来自他们网站的一些参考:doc.traefik.io

我有一个基于此的 docker-compose.yml 文件。

version: "3.3"

services:

  traefik:
    image: "traefik:latest"
    container_name: "traefik"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    labels:
      # Enable Traefik for this service, to make it available in the public network
      - traefik.enable=true
      # https-redirect middleware to redirect HTTP to HTTPS
      # It can be re-used by other stacks in other Docker Compose files
      - traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
      - traefik.http.middlewares.https-redirect.redirectscheme.permanent=true
      # Dashboard HTTP here
      - traefik.http.routers.traefik.rule=Host(`traefik.your.domain`)
      - traefik.http.routers.traefik.entrypoints=http
      - traefik.http.routers.traefik.middlewares=https-redirect
      # Dashboard HTTPS here
      - traefik.http.routers.traefik-https.rule=Host(`traefik.your.domain`)
      - traefik.http.routers.traefik-https.entrypoints=https
      - traefik.http.routers.traefik-https.tls=true
      # Use the special Traefik service api@internal with the web UI/Dashboard
      - traefik.http.routers.traefik-https.service=api@internal
      # Use the Let's Encrypt resolver created below
      - traefik.http.routers.traefik-https.tls.certresolver=myresolver
    command:
      # Enable the Traefik log, for configurations and errors
      - --log
      # Enable the Dashboard and API
      - --api
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      # Create an entrypoint "http" listening on address 80
      - --entrypoints.http.address=:80
      # Create an entrypoint "https" listening on address 443
      - --entrypoints.https.address=:443
      - "--certificatesresolvers.myresolver.acme.tlschallenge=true"
      # - "--certificatesresolvers.myresolver.acme.httpchallenge.entrypoint=http"
      #- "--certificatesresolvers.myresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
      - "--certificatesresolvers.myresolver.acme.email=your@email.addr"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  yourServices:
    image: yourServices
    container_name: yourServices
    labels:
      - "traefik.enable=true"
      - traefik.http.routers.yourServices-http.rule=Host(`your.domain`)
      - traefik.http.routers.yourServices-http.entrypoints=http
      - traefik.http.routers.yourServices-http.middlewares=https-redirect
      - "traefik.http.routers.yourServices.rule=Host(`your.domain`)"
      - "traefik.http.routers.yourServices.entrypoints=https"
      - "traefik.http.routers.yourServices.tls.certresolver=myresolver"

我知道这很丑陋。我不是这里的专家。幸运的是,它对我有用。希望能帮到你。