Reverse proxy to api with subpath postgrest

Hi, I tried o run postgrest api behind traefik
It's currect run only if I use a subdomain.

- "traefik.enable=true"
- "traefik.http.routers.livenet-api.rule=Host(`api.ln-server.loc`)"
 - "traefik.http.routers.livenet-api.service=livenet-api"
 - "traefik.http.services.livenet-api.loadbalancer.server.port=3000"

But if i try to run behind subpath it's not work

I tried with PathPrefix, Path rule and with pathregex rules like

- "traefik.enable=true"
- "traefik.http.routers.livenet-api.rule=Path(`/livenet-api/{path:.+}`)"
- "traefik.http.middlewares.livenet-api-replacepathregex.replacepathregex.regex=/livenet-api(.*)"
- "traefik.http.middlewares.livenet-api-replacepathregex.replacepathregex.replacement=$$1"
- "traefik.http.routers.livenet-api.middlewares=livenet-api-replacepathregex@docker"

I read this post:

https://docs.traefik.io/middlewares/replacepathregex/

But still NOT WORKS!

I need to do this

https://ln-server.loc/api/backups/other_details
to
:3000/backups

Hi @nicola92

I think you can achieve what you are after with a PathPrefix rule and a StripPrefix middleware


    - "traefik.enable=true"
    - "traefik.http.routers.livenet-api.rule=PathPrefix(`/livenet-api`)"
    - "traefik.http.services.livenet-api.loadbalancer.server.port=3000"
    - "traefik.http.routers.livenet-api.middlewares=livenet-strip"
    - "traefik.http.middlewares.lienet-strip.stripprefix.prefixes=/livenet-api

thanks, it's works!
But I have a problem with https, it's work only with http. But I change the routers rule with a subdomain it's work with https. I need both https and http. All other services (pgadmin, ngix etc..) in docker-compose works under https with patprefix rule.
do you know it there are some other requirements?

 - "traefik.enable=true"
      - "traefik.http.routers.livenet-api.rule=Host(`ln-server.loc`) && PathPrefix(`/api/v1`)"
      - "traefik.http.routers.livenet-api.middlewares=livenet-api-stripprefix"
      - "traefik.http.middlewares.livenet-api-stripprefix.stripprefix.prefixes=/api/v1"
      - "traefik.http.routers.livenet-api.service=livenet-api"
      - "traefik.http.routers.livenet-api.tls=true"
      - "traefik.http.services.livenet-api.loadbalancer.server.port=3000"
      - "traefik.http.routers.livenet-api.entrypoints=web"
      - "traefik.http.routers.livenet-api.entrypoints=websecure"
      #- "traefik.http.middlewares.livenet-api-https.redirectscheme.scheme=https"
      - "traefik.docker.network=livenet-network"

Hi @nicola92

You have to define separate routers, one for http and one for https.

More common is to redirect http to https and just define the one router for your service.

- "traefik.enable=true"
     - "traefik.docker.network=livenet-network"
# https router
      - "traefik.http.routers.livenet-api.rule=Host(`ln-server.loc`) && PathPrefix(`/api/v1`)"
      - "traefik.http.routers.livenet-api.tls=true"
      - "traefik.http.routers.livenet-api.entrypoints=websecure"
      - "traefik.http.routers.livenet-api.middlewares=livenet-api-stripprefix"
#http router (different name for the router)
      - "traefik.http.routers.livenet-api-http.rule=Host(`ln-server.loc`) && PathPrefix(`/api/v1`)"
      - "traefik.http.routers.livenet-api-http.entrypoints=web"
      - "traefik.http.routers.livenet-api-http.middlewares=livenet-api-stripprefix"
# middleware
      - "traefik.http.routers.livenet-api.middlewares=livenet-api-stripprefix"
      - "traefik.http.middlewares.livenet-api-stripprefix.stripprefix.prefixes=/api/v1"
# not needed, this is implicit as it the label on the servive
#      - "traefik.http.routers.livenet-api.service=livenet-api"
# this is only needed if your service exposes more than one port (or none)
# i.e. if the container exposes 3000 this line is not needed
      - "traefik.http.services.livenet-api.loadbalancer.server.port=3000"
      #- "traefik.http.middlewares.livenet-api-https.redirectscheme.scheme=https"
 

Thanks,
but on https still get 404. Traefik tells me that it works properly (there are no errors in dashboard).
it's very frustrating because all the other services work properly and I don't understand what the problem is on reverse proxy

@cakiwi this is my new configuration.
I try to add and remove the definition of service and server port but nothing change.

labels:
      - "traefik.enable=true"
      - "traefik.docker.network=livenet-network"
  # https router
      - "traefik.http.routers.livenet-api.rule=Host(`ln-server.loc`) && PathPrefix(`/api/v1`)"
      - "traefik.http.routers.livenet-api.middlewares=livenet-api-stripprefix"
      - "traefik.http.middlewares.livenet-api-stripprefix.stripprefix.prefixes=/api/v1"
      - "traefik.http.routers.livenet-api.entrypoints=websecure"
      - "traefik.http.routers.livenet-api.tls=true"
      - "traefik.http.routers.livenet-api.service=livenet-api"
      - "traefik.http.services.livenet-api.loadbalancer.server.port=3000"
  # http router (different name for the router)
      - "traefik.http.routers.livenet-api-http.rule=Host(`ln-server.loc`) && PathPrefix(`/api/v1`)"
      - "traefik.http.routers.livenet-api-http.middlewares=livenet-api-http-stripprefix"
      - "traefik.http.middlewares.livenet-api-http-stripprefix.stripprefix.prefixes=/api/v1"
      - "traefik.http.routers.livenet-api-http.entrypoints=web"
      - "traefik.http.routers.livenet-api-http.tls=false"
      - "traefik.http.routers.livenet-api-http.service=livenet-api"
      - "traefik.http.services.livenet-api-http.loadbalancer.server.port=3000"