Rewriting the response header in traefik - traefik

I have a kubernetes cluster and I am using traefik ingress controller to route traffic to deployments inside the kubenetes cluster.
I am able to use ingress.kubernetes.io/rewrite-target annotation to change the incoming request path to the one expected by the backend.
For example : /star is transformed to /trek by the rewrite target annotation and the request gets routed to /trek and backend processing is successful.
What I want to know is if there is a way to change response header so that /trek gets changed back to /star?

Did you get an answer to this? It looks like similar functionality is available in Apache Traffic Server: https://docs.trafficserver.apache.org/en/latest/admin-guide/plugins/header_rewrite.en.html, but would be good to have it in traefik

The functionality that does this is modifiers and specifically ReplacePath. Here is a similar answer with some examples.

Related

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

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
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/

Ambassador Mapping Rewrite Needs Original URL In Header

I'm using Ambassador Mappings created through a Helm chart in order to rewrite and forward a web request. The client has requested that we include the originally requested URL in a header when we forward the request.
Based on documentation, it looks like there are only two dynamic values I can set: %DOWNSTREAM_REMOTE_ADDRESS_WITHOUT_PORT% and %PROTOCOL%, neither of which seems to solve my issue.
Are there any ways for me to set this header properly through an Ambassador mapping?
Thanks!
So I think we've found the solution. If you use Envoy's syntax and make sure that is forwarded through, you can use their REQ() function, to get the authority and path, to construct the URL. For example:
http://%REQ(:authority)%%REQ(:path)%
Here is the documentation for all of the possibilities you can use with Envoy through Ambassador's mapping: https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_conn_man/headers#custom-request-response-headers

Download from a http location using apache camel

I need to download a file from an http location to my local system using apache camel. When I gave the below code
from("http://url/filename.xml")
.to("file://C:location")
it has worked for ftp but not working when the url is "http". That is, it is not downloading the file from the http location to the local address provided in the "to()".
This should work.
from("direct:abc")
.setHeader("Accept", simple("application/xml"))//Change it according to the file content
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.to("http://url/filename.xml")
.to("file:///tmp/?fileName=yourFileName.xml");
You cannot use from("Some url"). Above route is triggered whenever there is a message on direct:abc endpoint. You can change the yourFileName.xml to whatever filename you want it to be stored as.
Instead of a trigger from route, you can as well use a timer or any other means of self triggering.
The reason you cannot consume from a rest enpoint like this
from("http://url/filename.xml")
is you cannot consume from http endpoint. So there needs to be a trigger. Infact the exception message is pretty clear when you do like this. It says
org.apache.camel.spring.boot.CamelSpringBootInitializationException: org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route(route1)[[From[http://url/filename.xml]] -> [To[... because of Cannot consume from http endpoint
the http component cannot be used as a consumer ie. you cannot have a route as from("http://...")
you need to use a consumer component that will start the route.
You could try something like this
from("timer:foo?fixedRate=true&period=5000")
.to("http://url/filename.xml")
.to("file://C:location")

What headers should be used when combining URI-rewrites and HATEAOS?

Let's say I have a Foo service that accepts requests like:
http://foo-service/bar/baz
...and returns HATEOAS-style responses:
{
"self": "http://foo-service/bar/baz"
}
(Yes it should be links, href, etc - I'm simplifying for this question).
Now suppose I want to put that behind a reverse-proxy that also rewrites URI paths:
http://router/foo/bar/baz
(Here, I'm detecting the path starts with /foo/..., and so the reverse-proxy knows what service to route to. I would expect the self link to be "http://router/foo/bar/baz", even though the reverse-proxy actually made a request to http://foo-service/bar/baz).
I know about the Host: and X-Forwarded-Host: headers for specifying what the original request host was.
What is the correct header (or more generally, what is the correct way) for specifying the original path?
Finally found a possible answer: it seems to be X-Forwarded-Prefix.
eg: HATEOAS paths are invalid when using an API Gateway in a Spring Boot app

Is there a way to use WCF to redirect all HTTP requests on a certain port

I need to redirect all requests on port 80 of an application server to a web server. I'm trying to avoid the need to install IIS and instead use WCF to do the job.
It looks like an operation such as the one below is suitable but one problem I've got is if a URL of the form http://mydomain.com/ is used then WCF will present a page about metadata.
[OperationContract, WebGet(UriTemplate = "*")]
RedirectToWebServer();
Does anybody know of a way to get WCF behaving the same as IIS in redirect mode?
This just seems like the wrong tool for the job. If you really don't want to use one of the many web servers that could do this with a couple minutes of setup time (IIS, Apache, Lighttpd), you could just make a simple HTTP socket server.
Listen on port 80. As soon as you get two newlines in a row, send back the response:
HTTP/1.1 301 Moved Permanently
Location: http://myothersite.com/whatever
(I'm almost certain that's the minimum you need). If you want to be really fancy and follow HTTP specs, match HTTP/1.1 or HTTP/1.0 based on what the request has.. but for a quick and dirty redirect, that's all you need.
That said, again, I'd say go grab another web server and set up a redirect using it. There are many lightweight HTTP servers that will work.