Redirect to path

Hi,

I have a tomcat server and when I use the domain name it adds /admin/login ie: in a browser the link server.docker.localhost changes to *server.docker.localhost/admin/login.

I've expose the port 8080 in compose file for the web server and works fine ie: http://localhost:8080

Traefik docker compose:

reverse-proxy:
    image: traefik
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    ports:
      - 80:80
      - 443:443
    volumes:
      # Map the docker socket into the container
      - /var/run/docker.sock:/var/run/docker.sock:ro
      # Map the static configuration into the container
      - ./config/traefik.yaml:/etc/traefik/traefik.yaml:ro
      # Map the dynamic configuration into the container
      - ./dynamic:/etc/traefik/dynamic:ro
      # Map the certificates into the container
      - ./certs:/etc/certs:ro
    # networks:
    #   - traefik
    labels:
      traefik.enable: "true"
      traefik.http.routers.traefik_https.rule: Host(`traefik.docker.localhost`)
      traefik.http.routers.traefik_https.tls: "true"
      traefik.http.routers.traefik_https.service: api@internal
      traefik.http.routers.traefik_https.middlewares: dashboard-auth
      traefik.http.middlewares.dashboard-auth.basicauth.users: eu:$$apr1$$ag8QYLef$$FxAeaJ4OFaTVnPZv/u8RL/
 

Traefik conf:

# yaml-language-server: $schema=https://www.schemastore.org/traefik-v3.json
global:
  sendAnonymousUsage: false

################################################################
# API and dashboard configuration
################################################################
api:
  insecure: false
  dashboard: true

################################################################
# Entrypoint
################################################################
entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
          permanent: true

  websecure:
    address: ":443"

################################################################
# Providers
################################################################
providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
  file:
    directory: /etc/traefik/dynamic
    watch: true

################################################################
# Traefik Logging
################################################################
log:
  level: DEBUG

################################################################
# Access Logging
################################################################
accessLog: {}

 web_app:
    image: web_app
    container_name: web_app
    restart: unless-stopped
    ports:
      - 8080:8080

Dynamic configuration for the tomcat web server:

# yaml-language-server: $schema=https://www.schemastore.org/traefik-v3.json
http:
  routers:
    web_app:
      rule: Host(`server.docker.localhost`)
      tls: true
      service: web_app
     
  services:
    web_app:
      loadbalancer:
        servers:
          - url: http://server:8080

Can anyone guide me for the solution? don't give me the answer, just a direction.

Tanks

Check RedirectRegex middleware (doc).

1 Like

Hi

I',m trying to configure with RedirectRegex with no success. My url adds /domibus in path but it does't goes to /login:

If i access the expossed port http://localhost:8080/domibus it will add /login:

My new dynamic file:

http:
  middlewares:
    test-redirectregex:
      redirectRegex:
        regex: "^http://gateway.docker.localhost/(.*)"
        replacement: "http://gateway.docker.localhost/domibus/"

  routers:
    gateway_app: 
      rule: Host(`gateway.docker.localhost`)
      entrypoints: 
        - web
      service: gateway_app
      middlewares:
        - test-redirectregex
     
  services:
    gateway_app:
      loadbalancer:
        servers:
          - url: http://gateway_app:8080
        

You take a regex and replace every path with the same target path, that seems wrong. You probably want to add a variable to the replacement, that stands for the variable part of the regex.

1 Like

Tanks for the guiding, it's working.

Hi there,

The redirect to /admin/login is Tomcat's own behaviour - it's how Tomcat handles the default application context, not something Traefik is doing. What Traefik sees is: request comes in for server.docker.localhost/, Traefik passes it to Tomcat, Tomcat issues a 302 to /admin/login, Traefik transparently proxies that 302 back to the browser, browser follows it to server.docker.localhost/admin/login.

That second request (the /admin/login one) goes back through Traefik again. As long as your router rule is just Host() without any path-specific matcher, both paths will route through fine - you don't need to do anything special.

Check your Traefik labels on the Tomcat service to make sure you haven't got a PathPrefix rule that's too narrow:

  - "traefik.http.routers.tomcat.rule=Host(`server.docker.localhost`)"
  - "traefik.http.services.tomcat.loadbalancer.server.port=8080"

Just the Host() rule is enough - it matches all paths on that hostname, including /admin/login. If you've got a PathPrefix() in there as well, that could be blocking the second request.

One thing to double-check: is Tomcat's redirect going to the right hostname? Open browser devtools, Network tab, and look at the 302 response Location header. If it says http://localhost:8080/admin/login instead of http://server.docker.localhost/admin/login, then Tomcat's proxyName and proxyPort aren't set. In that case you'd need to add them to your Connector in server.xml or via the AJP/HTTP connector config so Tomcat knows it's behind a proxy.

1 Like

Hi,

I don't understand nothing about Tomcat and the server was given to me. What i realize is that there are no default web site in the server, if i just call the domain name it doesn't pop up login, onli if i type in the url the folder site. With the RedirectRegex middleware it works fine.

Apriciate for the explanation, now a know a litlle more of Tomcat :slight_smile:

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