运营反馈 Nginx 报 400 错误,具体点说:Request Header Or Cookie Too Large。其实随便搜搜就知道可以通过加大 client_header_buffer_size
和 large_client_header_buffers
来解决问题,不过这里面有一些细节值得讨论,正所谓:知其然,知其所以然。
首先,让我们想想为何 Nginx 不能用一个指令来搞定问题,而要用两个指令?为了搞清楚这个问题,我们不妨先看看官方文档的描述:
client_header_buffer_size: Sets buffer size for reading client request header. For most requests, a buffer of 1K bytes is enough. However, if a request includes long cookies, or comes from a WAP client, it may not fit into 1K. If a request line or a request header field does not fit into this buffer then larger buffers, configured by the large_client_header_buffers directive, are allocated.
large_client_header_buffers: Sets the maximum number and size of buffers used for reading large client request header. A request line cannot exceed the size of one buffer, or the 414 (Request-URI Too Large) error is returned to the client. A request header field cannot exceed the size of one buffer as well, or the 400 (Bad Request) error is returned to the client. Buffers are allocated only on demand. By default, the buffer size is equal to 8K bytes. If after the end of request processing a connection is transitioned into the keep-alive state, these buffers are released.
大概意思是说 Nginx 使用 client_header_buffer_size 缓存客户端的请求头,一旦空间不够了,就通过 large_client_header_buffers 按需扩容。这样做可以平衡资源和性能。
比如说大部分请求头的大小在 2K 以内,小部分的请求头在 10K 左右,此时理论上我们可以通过调整 client_header_buffer_size 到 10K 来解决问题,不过这样的话无疑浪费了宝贵的内存,更好的方法是调整 client_header_buffer_size 到 2K 满足大部分请求头的需求,至于小部分 10K 的请求头,则通过调整 large_client_header_buffers 来满足。
当http 的URI太长或者request header过大时会报414 Request URI too large或400 bad request错误。
可能原因
场景1.cookie中写入的值太大造成的,因为header中的其他参数的size一般比较固定,只有cookie可能被写入较大的数据
场景2.请求参数太长,比如发布一个文章正文,用urlencode后,使用get方式传到后台。
当请求头过大时,超过large_client_header_buffer时,
nginx可能返回"Request URI too large" (414)或者"Bad-request"(400)错误,
GET http://blog.test.ct99.cn/ HTTP/1.1
Host: blog.test.ct99.cn
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
Cookie: bdshare_firstime=1363517175366;
If-Modified-Since: Mon, 13 May 2013 13:40:02 GMT
如上例HTTP请求头由多行构成,
其中"GET http://blog.test.ct99.cn/ HTTP/1.1"表示Request line
当Request line的长度大于large_client_header_buffer的一个buffer(128k)时,nginx会返回"Request URI too large" (414)错误,对应上面的场景2。
请求投中最长的一行也要小于large_client_header_buffer,当不是Request line的最长行大于一个buffer(128k)时,会返回"Bad-request"(400)错误,对应上面的场景1。
此时调节这两个参数即可
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;