反向代理后访问Solr Web界面出现「内容编码错误」
Solr反向代理出现Content Encoding Error的解决方案
问题背景
服务器配置为Ubuntu 22、Apache 2.4.52、Solr 8.11.2,直接通过端口8983访问Solr正常,但通过Apache反向代理访问/solr路径时,浏览器提示Content Encoding Error,其他服务(Tomcat、PmWiki等)的反向代理均正常。
核心排查方向
从curl响应和代理日志来看,代理连接已成功建立,但内容解析失败,最可能的原因是双重压缩冲突(Apache和Solr同时对响应进行gzip压缩),其次是路径匹配或HTTP协议兼容性问题。
具体解决步骤
1. 关闭Apache代理路径的内容压缩
在Solr的反向代理配置中添加SetEnv no-gzip 1,避免Apache与Solr重复压缩内容:
<Location "/solr" > ProxyPass http://localhost:8983/solr/ ProxyPassReverse http://localhost:8983/solr/ # 禁用该路径的gzip压缩,防止与Solr的压缩逻辑冲突 SetEnv no-gzip 1 Order allow,deny Allow from all </Location>
2. 修正代理路径的斜杠一致性
确保Location路径与ProxyPass目标路径的斜杠规则统一,避免资源路径跳转错误:
# 方案A:Location带斜杠,与ProxyPass目标路径匹配 <Location "/solr/" > ProxyPass http://localhost:8983/solr/ ProxyPassReverse http://localhost:8983/solr/ SetEnv no-gzip 1 Order allow,deny Allow from all </Location> # 方案B:去掉ProxyPass末尾的斜杠 <Location "/solr" > ProxyPass http://localhost:8983/solr ProxyPassReverse http://localhost:8983/solr SetEnv no-gzip 1 Order allow,deny Allow from all </Location>
3. 禁用HTTP/2协议(针对HTTP/2环境)
如果服务器启用了HTTP/2,可能与代理的压缩机制存在兼容性问题,可在Solr的Location配置中强制使用HTTP/1.1:
<Location "/solr" > ProxyPass http://localhost:8983/solr/ ProxyPassReverse http://localhost:8983/solr/ SetEnv no-gzip 1 # 禁用HTTP/2,切换为HTTP/1.1 Protocols http/1.1 Order allow,deny Allow from all </Location>
4. 检查Solr自身的压缩配置
打开Solr核心的solrconfig.xml,查找是否开启了gzip压缩,若开启且不想关闭Apache的压缩,可关闭Solr的压缩:
<!-- 找到对应响应写入器配置,将enableGzip设为false --> <queryResponseWriter name="xml" class="solr.XMLResponseWriter"> <str name="content-type">text/xml;charset=UTF-8</str> <bool name="enableGzip">false</bool> </queryResponseWriter>
验证方法
修改配置后重启Apache:
sudo systemctl restart apache2
然后通过浏览器访问https://你的域名/solr,或用curl测试响应:
curl -H 'Accept-Encoding: gzip,deflate' https://你的域名/solr
若响应正常且能正确解压,说明问题已解决。
内容的提问来源于stack exchange,提问作者ScottD
相关产品推荐
相关产品推荐

