X-Forwarded-Prefix not added unless request path ends with slash
goeh opened this issue · 7 comments
I was happy to see that X-Forwarded-Prefix was added in 2.0.2.RELEASE and I tried to remove my workaround
- id: contact-service
uri: lb://contact-service
predicates:
- Path=/v1/contact/**
filters:
- StripPrefix=2
# - AddRequestHeader=X-Forwarded-Prefix, /v1/contact
But the X-Forwarded-Prefix is not added unless I addd a trailing slash to the request.
This works, prefix is set to "/v1/contact":
curl http://localhost:8080/v1/contact/
This does not work. It adds other X-Forwarded-... headers but not X-Forwarded-Prefix.
curl http://localhost:8080/v1/contact
I traced it to https://github.com/spring-cloud/spring-cloud-gateway/blob/master/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilter.java#L246 where both prefix
and originalUri.getPath()
is "/v1/contact". My guess is that the condition should be <= instead of <
Shouldn't your path then be /v1/contact**
?
But... requests with or without trailing slash are forwarded to contact-service and lists contacts just fine. I've never had any problem with this configuration before.
That is the only route you have defined?
No I have 10+ routes. Most of the paths ends with something/** and I have not noticed any problem with routing and trailing slash before. Just this X-Forwarded-Prefix problem i discovered today.
I just changed the config to Path=/v1/contact** and it did not make any difference. The prefix is added if I have trailing slash in my curl request. It's not added without it.
OK we will have to look into it. When I looked at the unit tests it looked like the use case you described it covered so that is why I was asking.
Thanks. This system has been in production for weeks so I know that the routes works ok. I was just hoping I could remove the AddRequestHeader
filter from all routes now that the gateway adds X-Forwarded-Prefix
by default.
Closed via 2bbdda9