geekops
geekops

Reputation: 515

Docker service redirection based on url path

I am using docker swarm and deploying 3 tomcat services each of the running on 8443 within the container and on 8444,8445,8446 on host containers.

I am looking to use a proxy server running on 8443 which will redirect the incoming request to the corresponding service based on the url path

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

My sample Docker-compose file

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

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

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

I have explored traeffik and nginx but was not able to find to re route based on url. Any suggestions.

Upvotes: 0

Views: 5194

Answers (2)

salman_sali
salman_sali

Reputation: 401

You can try the way I did it using nginx.

ON UBUNTU Inside the /etc/nginx/sites-available you will find the default file. Inside the server block add a new location block.

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/.;
    }
}

Upvotes: 0

agalisteo
agalisteo

Reputation: 583

You could use traefik based in rule with labels Host and Path http://docs.traefik.io/basics/#frontends

Something like

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

Upvotes: 1

Related Questions