本文介绍了使用traefik(v2)路由器在特定端口上请求转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用traefik版本2(或2.x),并且我想使用traefik路由器将所有请求从端口80转发到不同的端口,例如8081。因此,像之类的请求将转发到 URL。

I am using traefik version 2(or 2.x) and I want to forward all the request from port 80 to different port like 8081 with traefik router. So request like http://localhost/xx will be forwarded to http://localhost:8081/xx URL.

我是traefik的新手,正在使用docker对于此配置。
以下是我的docker-compose.yml文件配置。配置此traefik仪表板后,将其加载到 URL但是请求转发不起作用。

I am newbie with traefik and I am using docker for this configuration.Below is my docker-compose.yml file configuration. After configuring this traefik dashboard is loaded on http://localhost:8080/dashboard/#/ URL but request forwarding is not worked.

version: "3"

services:
  traefik:
    image: "traefik:v2.1.0"
    container_name: "traefik"
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  whoami:
    image: "containous/whoami"
    container_name: "simple-service"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.entrypoints=web"
      - "traefik.http.services.whoami.loadbalancer.server.port=8081"
      - "traefik.docker.network=proxy"
      - "traefik.http.routers.whoami.rule=Host(`localhost`)"

对此提供任何帮助。

推荐答案

您需要将服务端口映射到 8081

you need to map your service port to 8081

这是一个完全正常/经过测试的示例,您可以访问 whoami

,方法是转到 http://whoami.docker.local:8081 http://whoami.docker.local

this is a fully working/tested example where you can access whoami
by going to http://whoami.docker.local:8081 or http://whoami.docker.local

version: "3"

services:
    traefik:
        image: traefik
        command:
            - --api.insecure=true
            - --providers.docker=true
        ports:
            - "80:80"
            - "8080:8080"
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
        labels:
            - traefik.http.routers.api.rule=Host(`traefik.docker.local`)
            - traefik.http.routers.api.service=api@internal

    whoami:
        image: containous/whoami
        ports:
            - "8081:80"
        labels:
            - traefik.http.routers.whoami.rule=Host(`whoami.docker.local`)
            - traefik.http.routers.whoami.service=whoami@docker
            - traefik.http.services.whoami.loadbalancer.server.port=80

它在端口<$ c $上工作c> 80 以及 8081 ,根据您的请求。

it works on port 80 and also 8081, as per your request.

root@d:~# lsof -i :80,8081

COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
docker-pr 28208 root    4u  IPv6 51666675      0t0  TCP *:tproxy (LISTEN)
docker-pr 28265 root    4u  IPv6 51671715      0t0  TCP *:http (LISTEN)

,但是如果您解释为什么要访问:8081

,则可能会更容易获得帮助,因为<$ c $使用c> traefik ,因此我们不必进行此类重定向。

but it could be easier to help if you explain why you want to access :8081,
because traefik is used so we dont have to do those kind of redirections.

这篇关于使用traefik(v2)路由器在特定端口上请求转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 18:07