nginx的access日志中400 bad request错误的解决方法

发布时间:2020-10-03编辑:脚本学堂
在nginx的access.log中存在大量的400错误,并以每天几百M的速度增加,占用大量空间。

nginx的access.log中存在大量的400错误,并以每天几百M的速度增加,占用大量空间。
 

复制代码 代码示例:
# tail -f /opt/nginx/logs/access.log
116.236.228.180 - - [15/Dec/2010:11:00:15 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:15 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:15 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:15 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:15 +0800] "-" 400 0 "-" "-"
119.97.196.7 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
119.97.196.7 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
219.243.95.197 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"
116.236.228.180 - - [15/Dec/2010:11:00:16 +0800] "-" 400 0 "-" "-"

网上很多资料说是HTTP头/Cookie过大引起的,可以修改nginx.conf中两参数来修正.
 

client_header_buffer_size 16k;
large_client_header_buffers 4 32k;

修改后
 

client_header_buffer_size 64k;
large_client_header_buffers 4 64k;

没有效果,即便把nginx0.7.62升到最新的0.8.54也是无果。
在官方论坛中,nginx作者提到空主机头不会返回自定义的状态码,而是返回400错误。参见:http://forum.nginx.org/read.php?2,9695,11560

最后修正如下,改为原先的值:
 

client_header_buffer_size 16k;
large_client_header_buffers 4 32k;

关闭默认主机的日志记录,即可解决问题:
 

复制代码 代码如下:
server {
listen *:80 default;
server_name _;
return 444;
access_log   off;
}