Apache httpd 2.4反向代理不压缩

Gun*_*her 10 apache mod-deflate mod-proxy

使用Apache httpd 2.2,可以设置反向代理并使用mod_deflate压缩代理内容,以表达Accept-Encoding: gzip标题.

此配置足以使其工作:

    LoadModule deflate_module modules/mod_deflate.so
    LoadModule filter_module modules/mod_filter.so
    SetOutputFilter DEFLATE

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    ProxyRequests Off
    ProxyPass        /tomcat http://localhost:8880/
    ProxyPassReverse /tomcat http://localhost:8880/
    ProxyPass        /other  http://localhost:8001/
    ProxyPassReverse /other  http://localhost:8001/
Run Code Online (Sandbox Code Playgroud)

现在升级到2.4(Windows上为2.4.29)后,接受相同的配置,它确实压缩了DocumentRoot提供的静态内容.但是,当通过ProxyPass检索时,相同的内容将以未压缩的形式返回.

我知道我可以配置Tomcat来进行压缩,但是还有另一个服务器忽略了Accept-Encoding头.

如何设置反向代理,并压缩代理内容?

编辑:

以下是返回的标头,证明代理内容未被2.4服务器压缩:

----- Retrieving uncompressed from DocumentRoot ---------------------------------

C:\Temp>curl -I http://localhost/test.txt 
HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 17:11:59 GMT
Server: Apache/2.4.29 (Win64) OpenSSL/1.1.0g
Last-Modified: Fri, 05 Jan 2018 12:58:40 GMT
ETag: "75441-5620701eb471c"
Accept-Ranges: bytes
Content-Length: 480321
Vary: Accept-Encoding
Content-Type: text/plain

----- The same from Tomcat ------------------------------------------------------

C:\Temp>curl -I http://localhost:8880/rr/test.txt 
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"480321-1515157120042"
Last-Modified: Fri, 05 Jan 2018 12:58:40 GMT
Content-Type: text/plain
Content-Length: 480321
Date: Tue, 09 Jan 2018 17:11:59 GMT

----- 2.4.29: Retrieving compressed from DocumentRoot ---------------------------

C:\Temp>curl -I -H "Accept-Encoding: gzip" http://localhost/test.txt 
HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 17:11:59 GMT
Server: Apache/2.4.29 (Win64) OpenSSL/1.1.0g
Last-Modified: Fri, 05 Jan 2018 12:58:40 GMT
ETag: "75441-5620701eb471c-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48265
Content-Type: text/plain

----- 2.4.29: Not getting any compression for proxied Tomcat content ------------

C:\Temp>curl -I -H "Accept-Encoding: gzip" http://localhost/tomcat/rr/test.txt 
HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 17:11:59 GMT
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"480321-1515157120042"
Last-Modified: Fri, 05 Jan 2018 12:58:40 GMT
Content-Type: text/plain
Content-Length: 480321

----- 2.2.14: Retrieving compressed from DocumentRoot ---------------------------

C:\Temp>curl -I -H "Accept-Encoding: gzip" http://localhost:81/test.txt 
HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 17:11:59 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Fri, 05 Jan 2018 12:58:40 GMT
ETag: "90000000e7463-75441-5620701eb471c"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48265
Content-Type: text/plain

----- 2.2.14: Proxied Tomcat content comes compressed ---------------------------

C:\Temp>curl -I -H "Accept-Encoding: gzip" http://localhost:81/tomcat/rr/test.txt 
HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 17:11:59 GMT
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"480321-1515157120042"
Last-Modified: Fri, 05 Jan 2018 12:58:40 GMT
Content-Type: text/plain
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Run Code Online (Sandbox Code Playgroud)

所有这些都是在从ApacheHaus下载的普通2.4.29安装上测试的.上面的配置已添加到httpd.conf中,没有其他任何更改.这同样适用于2.2.14安装(2009年从Apache下载),但另一个更改为端口81.

ffe*_*ast 2

我\xe2\x80\x99已经成功地重现了curlapache/tomcat所描述的行为

\n\n

这就是我复制它的方式(OS X El Capitan):

\n\n

雄猫

\n\n
docker run -it --rm -p 8880:8080 tomcat:6.0\n
Run Code Online (Sandbox Code Playgroud)\n\n

阿帕奇

\n\n
httpd -v\n\nServer version: Apache/2.4.18 (Unix)\nServer built:   Feb 20 2016 20:03:19\n\nhttpd -l\nCompiled in modules:\n  core.c\n  mod_so.c\n  http_core.c\n  prefork.c\n
Run Code Online (Sandbox Code Playgroud)\n\n

Apache 配置(完整):

\n\n
Listen 80                                                                       \n\nLoadModule authz_user_module libexec/apache2/mod_authz_user.so                  \nLoadModule authz_core_module libexec/apache2/mod_authz_core.so                  \nLoadModule access_compat_module libexec/apache2/mod_access_compat.so            \nLoadModule filter_module libexec/apache2/mod_filter.so                          \nLoadModule deflate_module libexec/apache2/mod_deflate.so                        \nLoadModule mime_module libexec/apache2/mod_mime.so                              \nLoadModule log_config_module libexec/apache2/mod_log_config.so                  \nLoadModule headers_module libexec/apache2/mod_headers.so                        \nLoadModule version_module libexec/apache2/mod_version.so                        \nLoadModule proxy_module libexec/apache2/mod_proxy.so                            \nLoadModule proxy_http_module libexec/apache2/mod_proxy_http.so                  \nLoadModule unixd_module libexec/apache2/mod_unixd.so                            \n\n<IfModule unixd_module>                                                         \nUser _www                                                                       \nGroup _www                                                                      \n\n</IfModule>                                                                     \n\n<IfModule mime_module>                                                          \n    TypesConfig /private/etc/apache2/mime.types                                 \n</IfModule>                                                                     \n\nLogLevel debug                                                                  \n\n<IfModule log_config_module>                                                    \n    LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined\n    LogFormat "%h %l %u %t \\"%r\\" %>s %b" common                                \n\n    <IfModule logio_module>                                                     \n      # You need to enable mod_logio.c to use %I and %O                         \n      LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\" %I %O" combinedio\n    </IfModule>                                                                 \n    CustomLog "/private/var/log/apache2/access_log" common                      \n</IfModule>                                                                     \n\nErrorLog "/private/var/log/apache2/error_log"                                   \nTraceEnable off                                                                 \n\nSetOutputFilter  DEFLATE                                                        \n\nProxyRequests    Off                                                            \nProxyPass        /tomcat http://localhost:8880/                                 \nProxyPassReverse /tomcat http://localhost:8880/                                 \nProxyPass        /other  http://localhost:8001/                                 \nProxyPassReverse /other  http://localhost:8001/                                 \nDocumentRoot    /Library/WebServer/Documents \n
Run Code Online (Sandbox Code Playgroud)\n\n

检查

\n\n
curl -I -H \'Accept-Encoding: gzip\'  \'http://localhost/tomcat\' \nHTTP/1.1 200 OK\nDate: Sat, 13 Jan 2018 13:35:14 GMT\nServer: Apache-Coyote/1.1\nAccept-Ranges: bytes\nETag: W/"7454-1491118183000"\nLast-Modified: Sun, 02 Apr 2017 07:29:43 GMT\nContent-Type: text/html\nContent-Length: 7454\n\ncurl -I -H \'Accept-Encoding: gzip\'  \'http://localhost/index.html.en\' \nHTTP/1.1 200 OK\nDate: Sat, 13 Jan 2018 13:35:25 GMT\nServer: Apache/2.4.18 (Unix)\nLast-Modified: Tue, 09 Jan 2018 04:51:20 GMT\nETag: "45-56250aa712200-gzip"\nAccept-Ranges: bytes\nVary: Accept-Encoding\nContent-Encoding: gzip\nContent-Length: 65\nContent-Type: text/html\n
Run Code Online (Sandbox Code Playgroud)\n\n

如您所见,输出与您的示例非常匹配

\n\n

这里\xe2\x80\x99s是有趣的部分

\n\n

如果我使用常规GET请求而不是HEAD(通过浏览器或curl不使用-I) tomcat\xe2\x80\x99s 响应确实会被压缩

\n\n
curl -D - -H \'Accept-Encoding: gzip\'  \'http://localhost/tomcat\' 2>/dev/null | strings\nHTTP/1.1 200 OK\nDate: Sat, 13 Jan 2018 13:37:19 GMT\nServer: Apache-Coyote/1.1\nAccept-Ranges: bytes\nETag: W/"7454-1491118183000-gzip"\nLast-Modified: Sun, 02 Apr 2017 07:29:43 GMT\nContent-Type: text/html\nVary: Accept-Encoding\nContent-Encoding: gzip\nContent-Length: 2526\n(some junk)\n
Run Code Online (Sandbox Code Playgroud)\n\n

不知道为什么它\xe2\x80\x99s 发生,看起来像 Apache\xe2\x80\x99s + mod_proxy/defate HEAD请求上的错误行为。\n如果你说它在 Apache 2.2 中没问题,我猜可能是与此调整有一定关系

\n\n
mod_deflate will now skip compression if it knows that the size overhead added by the compression is larger than the data to be compressed.\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,我\xe2\x80\x99d 检查您的情况下GET请求的问题是否仍然存在。\n如果是 - 提供有关您的设置的更多详细信息,以便您的环境可以 100% 复制 - 用于 apache 和 tomcat 的有效 Dockerfile,以隔离可能的环境差异会很好

\n