spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

AddRequestHeader/SetRequestHostHeader is adding multiple Forwarded: <Filtered> which is giving 431-request headers field too large

Open lookingforrishi opened this issue 9 months ago • 1 comments

Hi Folks, I am using Spring boot starter -3.4.4 and Spring cloud dependencies version 2024.0.1.

I have created two routes in application.yaml:

  1. route 1 is where I want to add request headers to the incoming requests which match the path predicate. For this I am using AddRequestHeaderIfNotPresent=X-Custom_header:custom_value.
  2. route 2 is where I want to replace the host from http://localhost:8080 to https//some-domian.com for which I am using SetRequestHostHeader.

In both the cases I am getting Http headers is larger than 16384 bytes and getting 431 http status code, where Forwarded:<Filtered> is coming multiple times in the error.

Please let me know if it is a bug in existing version or I am missing some configuration.

lookingforrishi avatar Apr 08 '25 23:04 lookingforrishi

I have the same problem, and the console log is like this:

2025-09-24T17:15:34.523+08:00 WARN 19380 --- [gateway-service] [ctor-http-nio-8] r.n.http.server.HttpServerOperations : [3fbf3ef5, L:/172.20.100.20:8000 - R:/172.20.100.20:52861] Decoding failed: REQUEST(decodeResult: failure(io.netty.handler.codec.http.TooLongHttpHeaderException: HTTP header is larger than 8192 bytes.), version: HTTP/1.1) GET /test1 HTTP/1.1 User-Agent: Accept: Accept-Encoding: Accept-Language: Cache-Control: Upgrade-Insecure-Requests: X-Forwarded-For: X-Forwarded-Proto: X-Forwarded-Port: X-Forwarded-Host: Forwarded: Forwarded: Forwarded: Forwarded: Forwarded: Forwarded: Forwarded: Forwarded: Forwarded: ...

The code is simple:

@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator routes(RouteLocatorBuilder routeBuilder) {
        return routeBuilder
                .routes()
                .route("test1", spec -> spec
                        .order(1)
                        .path("/test1")
                        .uri("http://localhost:8000/gateway/test/user/1")
                )
                .build();
    }
}

Visit http://localhost:8000/gateway/test/user/1" then got the correct response, but failed with the url request: http://localhost:8000/teset1".

How to solve this?

Owen8686 avatar Sep 24 '25 09:09 Owen8686