【问题标题】:Docker service redirection based on url path基于 url 路径的 Docker 服务重定向
【发布时间】:2018-01-17 04:34:32
【问题描述】:

我正在使用 docker swarm 并部署 3 个 tomcat 服务,每个服务在容器内的 8443 和主机容器上的 8444,8445,8446 上运行。

我希望使用在 8443 上运行的代理服务器,它将根据 url 路径将传入的请求重定向到相应的服务

https://hostname:8443/a – > https://hostname:8444/a
https://hostname:8443/b – > https://hostname:8445/b
https://hostname:8443/c – > https://hostname:8446/c

我的示例 Docker-compose 文件

  version: "3"
services:
 tomcat1 :
    image: tomcat:1
    ports:
      - "8446:8443"

  tomcat2 :
    image: tomcat:1
    ports:
      - "8444:8443"

   tomcat3 :
    image: tomcat:1
    ports:
      - "8445:8443"

我已经探索过 traeffik 和 nginx,但无法找到基于 url 的重新路由。任何建议。

【问题讨论】:

    标签: docker nginx docker-swarm traefik


    【解决方案1】:

    您可以使用带有标签 HostPath 的基于规则的 traefik http://docs.traefik.io/basics/#frontends

    类似

    version: "3"
    services:
      traefik:
        image: traefik
        command: --web --docker --docker.swarmmode --docker.watch --docker.domain=hostname
        ports:
          - "80:80"
          - "8080:8080"
          - "443:443"
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
        deploy:
          placement:
            constraints: [node.role == manager]                                                        
          restart_policy:
            condition: on-failure
      tomcat1:
        image: tomcat:1
        labels:
        - traefik.backend=tomcat1
        - traefik.frontend.rule=Host:hostname;PathPrefixStrip:/a
        - traefik.port=8443
    

    【讨论】:

      【解决方案2】:

      你可以试试我用 nginx 做的方法。

      在 Ubuntu 上 在 /etc/nginx/sites-available 中,您将找到默认文件。 在 server 块内添加一个新的 location 块。

      server {
          listen 8443;
          #this is a comment
          location /a {     
             proxy_pass http://[::]:8444/.;
             
             #i have commented these out because i don't know if you need them
             #proxy_http_version 1.1;
             #proxy_set_header Upgrade $http_upgrade;
             #proxy_set_header Connection keep-alive;
             #proxy_set_header Host $host;
             #proxy_cache_bypass $http_upgrade;
             #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             #proxy_set_header X-Forwarded-Proto $scheme;
          }
      
           location /b {     
             proxy_pass http://[::]:8445/.;
          }
          
          location /c {     
             proxy_pass http://[::]:8446/.;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-09-14
        • 2013-07-18
        • 1970-01-01
        • 2014-02-12
        • 1970-01-01
        • 2012-04-16
        • 2013-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多