I want docker to listen to "http://localhost/user" and forward to "http://portal.local/user" using traefik

I've got my docker environment setup using traefik and I've got two services running at the moment.

I'm using Google OAuth for authentication which redirects to my web application with auth-code. The redirect URL isn't allowed anything but localhost or localhost:<any-port> or any CDN. I've setup my docker for http://portal.local.

I now want http://localhost/user/googleLogin?code=xxxxxxxxxx to be translated to http://portal.local/user/googleLogin?code=xxxxxxxx for further processing of authentication.

Right now, I'm having to manually change localhost to portal.local in browser URL after it gives site not found error, which then takes me to further processing.

Below is my docker-compose.yml file.

version: "3.9"

services:
  portal-traefik:
    container_name: portal-traefik
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
#      - --entrypoints.websecure.address=:443
#      - --certificatesresolvers.myresolver.acme.httpchallenge=true
#      - --certificatesresolvers.myresolver.acme.httpchallenge.entrypoint=web
#      - --certificatesresolvers.myresolver.acme.email=ssl@idealsalessolutions.com
#      - --certificatesresolvers.myresolver.acme.storage=/acme/acme.json
    image: traefik:latest
    networks:
      api_driven:
    ports:
      - "80:80"
      - "8080:8080"
#      - "443:443"
    restart: unless-stopped
    volumes:
      - portal_acme:/acme
      - /var/run/docker.sock:/var/run/docker.sock:ro

  api-i4f:
    container_name: api-i4f
    depends_on:
      - php-i4f
      - portal-traefik
    image: nginx:stable-alpine
    labels:
      - traefik.enable=true
      - traefik.http.routers.api.rule=Host(`api.local`)
    networks:
      api_driven:
    restart: unless-stopped
    volumes:
      - ../docker.sites/api.local:/usr/share/nginx/api.local
      - ./conf/nginx/conf.d:/etc/nginx/conf.d:ro
    command: [nginx, '-g', 'daemon off;']
    hostname: api.local

  portal-i4f:
    container_name: portal-i4f
    depends_on:
      - php-i4f
      - portal-traefik
    image: nginx:stable-alpine
    labels:
      - traefik.enable=true
      - traefik.http.routers.portal.rule=Host(`portal.local`)
    networks:
      api_driven:
    restart: unless-stopped
    volumes:
      - ../docker.sites/portal.local:/usr/share/nginx/portal.local
      - ./conf/nginx/conf.d:/etc/nginx/conf.d:ro
    command: [nginx, '-g', 'daemon off;']
    hostname: portal.local

  php-i4f:
    container_name: php-i4f
    depends_on:
      - portal-traefik
    image: isshub/core:php7.4.30-fpm-alpine3.16-intl-mysql
    networks:
      api_driven:
    restart: unless-stopped
    volumes:
      - ../docker.sites/api.local:/usr/share/nginx/api.local
      - ../docker.sites/portal.local:/usr/share/nginx/portal.local

networks:
  api_driven:
    name: "api_driven"

volumes:
  portal_acme:

I've tried to use multiple router rules to listen to both localhost and portal.local using regex/replacement middlewares as well but that stops the service at all and gives 404 error.

Can't you just add localhost as Host to the target service?

- traefik.http.routers.portal.rule=Host(`portal.local`) || Host(`localhost`)

Thanks. I was able to achieve it like below in portal-i4f service

      - traefik.http.routers.portal1.rule=Host(`portal.local`)
      - traefik.http.routers.portal1.entrypoints=web
      - traefik.http.routers.portal1.service=svc_portal1
      - traefik.http.services.svc_portal1.loadbalancer.server.port=80

      - traefik.http.routers.portal2.rule=Host(`localhost`)
      - traefik.http.routers.portal2.entrypoints=portal
      - traefik.http.routers.portal2.service=svc_portal2
      - traefik.http.services.svc_portal2.loadbalancer.server.port=83

and by creating another entrypoint in portal-traefik

      - --entrypoints.web.address=:80
      - --entrypoints.portal.address=:83

and by changing portal.local.conf to listen to both ports 80 and 83.

However, this didn't help my cause as during redirection, I was not able to access my session data since it was being registered with localhost:83 hence not accessible in portal.local. I've now decided to continue with only localhost:83 with following configuration in portal-i4f and changed portal.local.conf to listen to port 83 only.

      - traefik.http.routers.portal.rule=Host(`localhost`)
      - traefik.http.routers.portal.entrypoints=portal
      - traefik.http.routers.portal.service=svc_portal
      - traefik.http.services.svc_portal.loadbalancer.server.port=83

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.