2

I built a reverse proxy with Apache 2.4 on a cento's 7 server. It works with standard html pages but i need to substitute some url stored in .js files too. The directive:

ProxyHTMLExtended On

should enable the parsing inside external .css and .js files but it doesn't work. In the log file I can see:

[proxy_html:trace1] [pid 3263] mod_proxy_html.c(823): [client xxx] Non-HTML content; not inserting proxy-html filter

I tried to use mod_substitute, this is the interesting part in my 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>

But in the log file there aren't any mod_substitute trace. It seems mod_substitute is never called.

The proxyHTMLURLMap rules works fine, but only on regular html files.

Depending on the .js file I'm asking to the server, I can see in the log file:

[xml2enc:debug] [pid 3259] mod_xml2enc.c(254): [client xxx] AH01434: Charset ISO-8859-1 not supported by libxml2; trying apr_xlate

or

[proxy_html:trace1] [pid 3263] mod_proxy_html.c(823): [client xxx] Non-HTML content; not inserting proxy-html filter

then the process stop, I receive the file but nothing has been replaced on it.

1) Wy the "ProxyHTMLExtended On" rule don't parse external .js files as described in Apache documentation?

2) Wy the mod_substitute don't work?

S.Succi
  • 21
  • 1
  • 3
  • The simplest explanation would be the underlying server is not sending data with either of the two content types in your config. Do a network dump and confirm the content type header from the remove server. Also it looks like there is a libxml2 incompatibility, you may need to update it. – Unbeliever Oct 06 '16 at 21:38

3 Answers3

2

I'll try to answer to your questions

1) Wy the "ProxyHTMLExtended On" rule don't parse external .js files as described in Apache documentation?

You say that ProxyHTMLExtended Directive:

should enable the parsing inside external .css and .js files but it doesn't work.

That's seems to be incorrect, the current doc says:

Set to Off, HTML links are rewritten according to the ProxyHTMLURLMap directives, but links appearing in Javascript and CSS are ignored.

Set to On, all scripting events (as determined by ProxyHTMLEvents) and embedded scripts or stylesheets are also processed by the ProxyHTMLURLMap rules, according to the flags set for each rule. Since this requires more parsing, performance will be best if you only enable it when strictly necessary.

That means that embedded scripts, the ones in <script></script> are checked. It doesn't mention .js files.

2) Wy the mod_substitute don't work?

About this i don't know for sure why it doesn't work, but assuming the mod_substitute is enabled since apache started without error, the only thing that I can guess is that apache is sending application/javascript as Mime-Type instead of text/javascript that you wrote

Some bonus suggestions:

  • I wouldn't use ProxyHTMLURLMap ^\/(.*) /mylocation/$1 R with ProxyHTMLExtended On because will translate every / in your scripts, if you have <script> var a = 12/2; </script> will be translated into <script> var a = 12/mylocation/2; </script>. I would consider using ProxyHTMLURLMap / /mylocation/ c (the c flag means: "Pass embedded script and style sections through untouched.")
  • I don't really think that you need ProxyHTMLURLMap /mylocation /mylocation
  • ProxyHTMLURLMap http://remoteserver /mylocation/ will add an extra / to your urls, it still works but, imho, it is not a good translation.
    Ex. <a href="http://remoteserver/about"> becomes <a href="/mylocation//about">
    I suggest to rewrite it like this ProxyHTMLURLMap http://remoteserver /mylocation
Community
  • 1
  • 1
Luca Ricci
  • 95
  • 9
2

I have a similar problem where mod substitute was not working for me.

Then I read somewhere that normally, mod_subsittue is actually by default only working if the HTTP response you get from the server has mime type txt/html.

This was not my scenario. My scenario was that I want to re-write the content of an XML, namely a JEE web service that was being reverse proxied by apache httpd.

In order to manage to make the mod substitute alter the reply content it was necessary to do:

<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>

The magic step was enabling the mod_filter and adding the directive: AddOutputFilterByType .

When this was added in, the substitue altered the body of the xml. replacing the endpoint address.

99Sono
  • 3,554
  • 27
  • 39
0

About this:

But in the log file there aren't any mod_substitute trace

I managed to get a mod_substitute trace in error.log by using:

LogLevel alert substitute:trace8
sdbbs
  • 4,270
  • 5
  • 32
  • 87