【问题标题】:Simple reverse proxy example with TraefikTraefik 的简单反向代理示例
【发布时间】:2020-02-14 13:26:55
【问题描述】:

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

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

这是他们最好的例子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;
        }
    }
}

【问题讨论】:

    标签: traefik


    【解决方案1】:

    对于这个简单的示例,您需要在静态配置中创建一个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"
    

    【讨论】:

      【解决方案2】:

      来自他们网站的一些参考: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"
      

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

      【讨论】:

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