When the request header is too long and is directly rejected by the server, should you receive the Cookie first or the custom header first?
When the request header is too long and is directly rejected by the server, should we receive the Cookie first or the custom header? First attribute, then receive the amount: The first step is to determine whether the server rejected the total amount of the request header exceeding the limit or the individual field exceeding the limit, both of which correspond to the same client error status code 431; after attributing, the order of receiving the amount is generally to receive the Cookie field, which is the easiest to swell, and then look at the custom header and the gateway append items, because in most scenarios, Cookie is the fastest growing in volume and also the easiest to safely reduce.
What does the server return when it rejects?
According to MDN's definition, the status code 431 indicates that the server refuses to process the request due to the request's HTTP header being too long. After reducing the volume of the request header, it can be resent. This status code has two usages: the total amount of the request header is too large, or a single header field is too large. MDN suggests that the server should specify which one in the response body, and ideally, indicate which headers are too large, so that the client knows whether to clear the Cookie or have the caller change the code. If the response body only says
Why is the request header getting bigger and bigger?
There are three common sources. The first is tracking cookies: advertising and statistics components continuously write identifiers to cookies, accumulating them with multiple visits, and a single domain cookie may swell to a considerable volume.The second is the addition of custom headers by third-party scripts: fields for埋点, experimental platform, and link tracking need to be added separately, and the total amount increases accordingly. The third is the addition of proxy and gateway: each layer of the forwarding chain may add a forwarding header.Three types of sources are not felt daily, but only when a user collides with the limit carrying a large Cookie, and it often manifests as 'the same interface, some users are normal and some report errors'.
How should the troubleshooting order be arranged?
First, attribute the cause: let the server write the error response body clearly whether it is total or single field over-standard. This step determines who to find to make changes next. Then, reproduce: export the request header from the user environment with the problem, compare it with the header of a normal user request for the same path, and the extra part is the source of expansion. Then, measure the volume: if the total exceeds the standard, clear the Cookie first, especially the tracking fields; if a single field exceeds the standard, directly handle that field, split or shorten it. After collecting, observe for a period of time, confirm that it no longer recurs, and then solidify the changes into the configuration or code.
Why collect cookies first?
Cookies have three characteristics that put them at the forefront of the collection order: they are automatically carried by the browser in each request, and users are not aware of them and cannot clean them up; they accumulate throughout the year, and the fields left behind when the writing component has already been decommissioned are still there; the reduction action is completely controllable on the server side, and the effect can be achieved by issuing shorter cookies, splitting domains, and setting reasonable expiration periods. Custom headers are different; changing them requires coordination with each entry, and the cycle is long. Therefore, collecting cookies first is the cheapest path, not because custom headers are unimportant, but because they can be changed the fastest and have the clearest impact.
Common questions
What is the difference between 431 and 413?413 is for large request body, 431 is for long request headers; refer to the former for file upload errors, and the latter for interfaces with a large number of Cookies.
How can users temporarily自救?Just clear the Cookies of the corresponding site and retry to restore access; but the root cause is usually too many Cookies written by the server side, and cleaning up is only a temporary relief.
Can the header size limit of the gateway layer be increased directly?Answer: Can be adjusted, but only recommended as a buffer. The continuous growth of the head volume indicates that there are components pushing things into it, first attribute the amount received, then discuss adjusting the limit.