Apache2.4如何设置使能Transfer-Encoding: chunked

问题:
http接口,输出Json

代码迁移服务器,原来的服务器接口消息头输出如下:

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Content-Type: text/html
Date: Sun, 02 Jun 2019 17:04:02 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=100
Pragma: no-cache
Server: Apache
**Transfer-Encoding: chunked**
X-Powered-By: PHP/5.5.38

新服务器自己装的LAMP环境,配置apache2,调用同一个接口,消息头返回如下:

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: close
**Content-Length: 5**
Content-Type: text/html
Date: Mon, 03 Jun 2019 15:47:39 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Proxy-Connection: keep-alive
Server: Apache

客户端写了chunked输出的解析不好升级,所以希望新服务器配置后,保持与原服务器一样的chunked输出.

Apache版本:

iZm5ecq1dcb6zw7pexhdp5Z:/etc/apache2 # apache2ctl -v
Server version: Apache/2.4.23 (Linux/SUSE)
Server built:   2017-12-28 13:20:25.000000000 +0000

设置加载模块:

iZm5ecq1dcb6zw7pexhdp5Z:/etc/apache2 # /usr/sbin/apachectl -t -D DUMP_MODULES
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_prefork_module (static)
 unixd_module (static)
 systemd_module (static)
 authz_host_module (shared)
 actions_module (shared)
 alias_module (shared)
 authn_file_module (shared)
 auth_basic_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cgi_module (shared)
 dir_module (shared)
 include_module (shared)
 log_config_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 setenvif_module (shared)
 status_module (shared)
 userdir_module (shared)
 asis_module (shared)
 imagemap_module (shared)
 php5_module (shared)
 version_module (shared)
 authn_core_module (shared)
 reqtimeout_module (shared)
 authz_core_module (shared)
 socache_shmcb_module (shared)
 deflate_module (shared)
 filter_module (shared)
 headers_module (shared)

客户端请求:
GET /device_auth.php?deviceid=xxxxxxxxx HTTP/1.1
Host:www.xxxxx.tech

1、尝试注释掉server-tuning.conf配置文件中的涉及的downgrade-1.0行,重启apace服务,接口没有改变
2、尝试用curl在客户端添加header指定Accept-Encoding: gzip之后,可以得到Server返回Transfer-Encoding: chunked

请问

是否遗漏了什么设置,如何修改配置才能使接口默认返回chunked消息头?

阅读 5.2k
2 个回答

我猜测是因为没开启apache中的KeepAlive,参考一下apache社区中的这篇回答,链接描述

https://httpd.apache.org/docs...
The DeflateBufferSize directive specifies the size in bytes of the fragments that zlib should compress at one time. If the compressed response size is bigger than the one specified by this directive then httpd will switch to chunked encoding (HTTP header Transfer-Encoding set to Chunked), with the side effect of not setting any Content-Length HTTP header. This is particularly important when httpd works behind reverse caching proxies or when httpd is configured with mod_cache and mod_cache_disk because HTTP responses without any Content-Length header might not be cached.
按官方的描述,只有压缩后的大小超出了设定的这个DeflateBufferSize值才会切换成chunked encoding。

不知道Apache的哪个历史版本开始加入这个条件的?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题