我在cento的7服务器上使用Apache 2.4构建了一个反向代理。它适用于标准 html 页面,但我也需要替换 .js 文件中存储的一些 url。指令:
ProxyHTMLExtended On
应该启用外部 .css 和 .js 文件内部的解析,但它不起作用。在日志文件中我可以看到:
[proxy_html:trace1] [pid 3263] mod_proxy_html.c(823): [client xxx] Non-HTML content; not inserting proxy-html filter
我尝试使用 mod_substitute,这是我的 httpd.conf 中有趣的部分:
ProxyPass /mylocation/ http://remoteserver/
<Location /mylocation/>
ProxyHTMLEnable On
ProxyHTMLExtended On
LogLevel debug proxy_html:trace3 substitute_module:debug
RequestHeader unset Accept-Encoding
AddOutputFilterByType SUBSTITUTE text/javascript text/html
Substitute "s|/css/|/mylocation/css/|ni"
Substitute "s|/js/|/mylocation/js/|ni"
Substitute "s|/custom_logo/|/mylocation/custom_logo/|ni"
Substitute "s|/html/|/mylocation/html/|ni"
Substitute "s|/current_config/|/mylocation/current_config/|ni"
Substitute "s|/web_lang/|/mylocation/web_lang/|ni"
Substitute "s|/custom_lang/|/mylocation/custom_lang/|ni"
ProxyPassReverse /
ProxyHTMLURLMap //remoteserver /mylocation/
ProxyHTMLURLMap http://remoteserver /mylocation/
ProxyHTMLURLMap /mylocation /mylocation
ProxyHTMLURLMap ^\/(.*) /mylocation/$1 R
</Location>
但是在日志文件中没有任何 mod_substitute 跟踪。似乎 mod_substitute 从未被调用过。
proxyHTMLURLMap 规则工作正常,但仅限于常规 html 文件。
根据我向服务器请求的 .js 文件,我可以在日志文件中看到:
[xml2enc:debug] [pid 3259] mod_xml2enc.c(254): [client xxx] AH01434: Charset ISO-8859-1 not supported by libxml2; trying apr_xlate
或
[proxy_html:trace1] [pid 3263] mod_proxy_html.c(823): [client xxx] Non-HTML content; not inserting proxy-html filter
然后进程停止,我收到文件,但上面没有任何内容被替换。
1) 为什么“ProxyHTMLExtended On”规则不解析外部 .js 文件,如 Apache 文档中所述?
2)为什么 mod_substitute 不起作用?
我会尽力回答你的问题
1) 为什么“ProxyHTMLExtended On”规则不解析外部 .js 文件,如 Apache 文档中所述?
你说
ProxyHTMLExtended
指令:
应该启用外部 .css 和 .js 文件内部的解析,但它不起作用。
这似乎是不正确的,当前文档说:
设置为 Off,HTML 链接将根据 ProxyHTMLURLMap 指令重写,但出现在 Javascript 和 CSS 中的链接将被忽略。
设置为“开”,所有脚本事件(由 ProxyHTMLEvents 确定)和嵌入式脚本或样式表也会由 ProxyHTMLURLMap 规则根据为每个规则设置的标志进行处理。由于这需要更多的解析,因此如果仅在绝对必要时启用它,性能将是最好的。
这意味着嵌入式脚本,
<script></script>
中的脚本被检查。它没有提到 .js 文件。
2)为什么 mod_substitute 不起作用?
关于这一点,我不确定为什么它不起作用,但假设自从 apache 启动没有错误以来 mod_substitute 已启用,我唯一能猜测的是 apache 正在发送
application/javascript
作为 Mime-Type 而不是text/javascript
你写的
一些奖励建议:
ProxyHTMLURLMap ^\/(.*) /mylocation/$1 R
与 ProxyHTMLExtended On
一起使用,因为会翻译脚本中的每个 /
,如果您有 <script> var a = 12/2; </script>
将被翻译为 <script> var a = 12/mylocation/2; </script>
。我会考虑使用 ProxyHTMLURLMap / /mylocation/ c
(c
标志意味着:“通过不受影响的方式传递嵌入的脚本和样式部分。”)ProxyHTMLURLMap /mylocation /mylocation
ProxyHTMLURLMap http://remoteserver /mylocation/
会在您的网址中添加额外的 /
,它仍然有效,但是,恕我直言,这不是一个好的翻译。<a href="http://remoteserver/about">
变成 <a href="/mylocation//about">
ProxyHTMLURLMap http://remoteserver /mylocation
我有一个类似的问题,mod替代品对我不起作用。
然后我在某处读到,通常情况下,mod_subsittue 实际上默认情况下仅在从服务器获得的 HTTP 响应具有 mime 类型 txt/html 时才起作用。
这不是我的场景。 我的场景是,我想重写 XML 的内容,即由 apache httpd 反向代理的 JEE Web 服务。
为了设法让 mod 替代品改变回复内容,有必要这样做:
<Location /mockOutgoingWebServicePortBinding>
# core authentication and mod_auth_basic configuration
# for mod_authn_dbd
AuthType Basic
Authname "Password Required"
# core authorization configuration
AuthUserFile C:\Apache24\conf\htpasswd
Require valid-user
# mod_filter to be able to subsitute text xml
AddOutputFilterByType SUBSTITUTE text/xml text/html
Substitute "s|http://someHostName:8088/|http://localhost:80/|i"
</Location>
神奇的一步是启用 mod_filter 并添加指令:AddOutputFilterByType 。
添加此内容后,替换会更改 xml 的正文。替换端点地址。
关于这个:
但是在日志文件中没有任何 mod_substitute 跟踪
我设法通过使用以下方法在 error.log 中获取 mod_substitute 跟踪:
LogLevel alert substitute:trace8