Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass original URI, with arguments, to Traefik ErrorPage handler specified in `query`?

Tags:

traefik

I'm trying to use nginx to serve a custom error page using the Error Page middleware so that 404 requests to a lambda service (which I don't control) can be handled with a custom error page. I want to be able to get the context of this original request on that error page, either in Nginx for further forwarding, or else as a header for further handling e.g. in PHP or whatnot so I can provide contextual links on the 404 page.

However, right now after the redirection to Nginx in Traefik's ErrorPage middleware it seems the request has lost all the headers and data from the original service query.

The relevant part of my dockerfile:

traefik.port=8080
traefik.protocol=http
traefik.docker.network=proxy
traefik.frontend.rule=PathPrefix:/myservice;ReplacePathRegex:^/myservice/(.*) /newprefix/$$1
traefik.frontend.errors.myservice.status=404
traefik.frontend.errors.myservice.service=nginx
traefik.frontend.errors.myservice.query=/myservice-{status}

Nginx receives the forwarded 404 request, but the request URI comes through as nothing more than the path /myservice-404 specified in query (or /, if I omit traefik.frontend.errors.myservice.query). After the ReplacePathRegex I have the path of the original request available in the HTTP_X_REPLACED_PATH header, but any query arguments are no longer accessible in any header, and nginx can't see anything else about the original URI. For example, if I requested mysite.com/myservice/some/subpath?with=parameters, the HTTP_X_REPLACED_PATH header will show /myservice/some/subpath but not include the parameters.

Is it possible in Traefik to pass another service the complete context about the original request?

What I'm really looking for is something like try_files, where I could say "if this traefik request fails, try this other path instead", but I'd settle for being able to access the original, full request arguments within the handling backend server. If there was a way to send Nginx a request with the full path and query received by Traefik, that would be ideal.

tl;dr:

  • I am routing a request to a specific service in Traefik
  • If that request 404s, I want to be able to pass that request to Nginx for further processing / a contextual error page
  • I want Nginx and/or the page which receives the ErrorPage redirect to be able to know about the request that 404'd in the service
like image 251
K. Adam Avatar asked Nov 07 '22 02:11

K. Adam


1 Answers

Unfortunately this is not possible with Traefik. I tried to achieve something similar but I realized that the only information that we are able to pass to the error page is the HTTP code, that's it.

The only options available are mentioned in their docs: https://doc.traefik.io/traefik/middlewares/errorpages/

like image 117
Julien Salinas Avatar answered Dec 16 '22 21:12

Julien Salinas