首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Traefik的简单反向代理示例

Traefik的简单反向代理示例
EN

Stack Overflow用户
提问于 2020-02-14 21:26:55
回答 2查看 3.6K关注 0票数 2

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

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

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

代码语言:javascript
复制
## Forwarding all (non-tls) requests on port 3306 to a database service
## Static configuration
entryPoints:
  web:
    address: ":80"
  mysql:
    address: ":3306"   

我想要来自NGINX的类似如下的东西:

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2020-02-28 17:33:26

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

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

代码语言:javascript
复制
# 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

代码语言:javascript
复制
# 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"
票数 4
EN

Stack Overflow用户

发布于 2021-03-30 23:59:35

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

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

代码语言:javascript
复制
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"

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60227270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档