【问题标题】:Traefik - proxy to backend with pathTraefik - 带路径的后端代理
【发布时间】:2018-10-29 12:22:40
【问题描述】:

我需要将http://www.example.com/foo 代理到http://<backend>/bar,使用 nginx 我有这个配置:

server {
    listen       80;
    client_max_body_size 2048M;
    server_name www.example.com;
    location /foo {
        proxy_pass      http://container_name/bar/;
        proxy_set_header Host            $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

我会将其转换为 traefik。那里有我的撰写文件:

version: '3'
services:
 traefik:
   image: traefik:1.7.3-alpine
   command: --api --docker
   ports:
    - "80:80"
    - "8080:8080"
   volumes:
    - /var/run/docker.sock:/var/run/docker.sock
   labels:
    - traefik.enable=false

 webapp:
   ...
   ...
   labels:
    - traefik.backend=bar
    - traefik.frontend.rule=Host:www.example.com;Path:/foo/
    - traefik.port=80

有办法解决吗?

谢谢

【问题讨论】:

    标签: reverse-proxy traefik


    【解决方案1】:

    您可以对前端规则使用 ReplacePathRegex 执行类似的操作(无需在后端执行任何操作):

    PathPrefix:/foo;ReplacePathRegex:^/foo(.*) /bar$$1
    

    【讨论】:

    • 我猜答案是正确的,但是在make up -d时出现错误:ERROR: Invalid interpolation format for "labels" option in service "webapp": "traefik.frontend.rule=Host:www.example.com; PathPrefix:/foo; ReplacePathRegex:^/foo(.*) /bar$1"
    • 我的错:我忘了加一个“$”=>你需要用..本身来逃避它!我已经更正了我的答案。这是一个关于它的解释:docs.docker.com/compose/compose-file/#variable-substitution
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多