4

I have a working Reverse Proxy that is active only on a specific Location in my site, in this case for example: www.example.com/reverseproxy/site1 I am reverse proxying site1. I want to replace some part of the body of every page that is proxied so I tried to use mod_substitute, the problem is that I can't get it to work under <Location /reverseproxy>

This is my httpd (SSL is enabled):

<VirtualHost _default_:443> 

    ProxyPassInterpolateEnv On
    RewriteEngine On

    ... Some rewrite rules
    RewriteRule /?reverseproxy/http(s*):/([^\/]*)/*(.*) "http$1://$2/$3" [P]
    SSLProxyEngine On
    ProxyPassReverse "/reverseproxy/http${ssl}://${page}" http${ssl}://${page} interpolate

    SetOutputFilter INFLATE;proxy-html;SUBSTITUTE;DEFLATE;
    ProxyHTMLInterp On
    ProxyHTMLExtended Off

    ...Some ProxyURLMap
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute "s|<body>|<body1>|"
</VirtualHost>

This substitute does work but it applies to all the pages and not only to /reverseproxy. When I try to put it in <Location /reverseproxy> it does not work, it just ignores it:

<Location /reverseproxy>
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|<body>|<body1>|"
</Location>

What I tried:

I tried adding

 RequestHeader unset Accept-Encoding

outside the <Location> tag but it didn't work

I tried then adding this

SetOutputFilter SUBSTITUTE;DEFLATE

in the <Location /reverseproxy> tag but no luck

And as someone suggested tried with this filterchain

FilterDeclare filter
FilterProvider filter SUBSTITUTE "%{CONTENT_TYPE} =~ m|^text/html|"
FilterDeclare unpackGZIP
FilterProvider unpackGZIP INFLATE "resp('Content-Encoding') == 'gzip'"

<Location /reverseproxy>
FilterChain unpackGZIP filter DEFLATE

Everything else inside the Location works, it just seems to ignore the substitution.

Paul_Rent
  • 41
  • 1
  • 3
  • You're not trying this in a .htaccess, are you? – Gerard H. Pille May 26 '20 at 11:17
  • @GerardH.Pille i have it in httpd conf – Paul_Rent May 26 '20 at 11:42
  • I believe there is a problem with your ProxyPassReverse, and as a consequence, your "" doesn't capture the requests . "The optional interpolate keyword, used together with ProxyPassInterpolateEnv, enables interpolation of environment variables specified using the format ${VARNAME}. Note that interpolation is not supported within the scheme portion of a URL." => "http${ssl}" – Gerard H. Pille May 26 '20 at 11:44
  • @GerardH.Pille first of all thanks for your time, i believe you are right and my doesn't capture requests. I tried removing the ${ssl} variable from the ProxyPassReverse (and on the ProxyPass above it), the page is still proxied but the Location tag still doesn't seem to work. – Paul_Rent May 26 '20 at 12:42
  • Check out "RewriteRule /?reverseproxy/http(s*):/([^\/]*)/*(.*) "http$1://$2/$3" [P]" too, which removes "reverseproxy" completely. I must say this rule looks more suited for a forward proxy than a reverse proxy. Anyway: your location should be based on what is in "$3". – Gerard H. Pille May 26 '20 at 15:40

2 Answers2

3

The only solution that was working for me, is:

AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text/html

Source:

Valerio Bozzolan
  • 314
  • 2
  • 15
2

I'm glad to say that there is a way to get around the interpolation issue, at least with the substitution issue.

Lots of google results for interpolation with mod_substitute come here, so I figured I'd help. It is indeed possible to do something like this:

## These modules are required
LoadModule filter_module modules/mod_filter.so
LoadModule substitute_module modules/mod_substitute.so
LoadModule env_module modules/mod_env.so

## Must be done in vhost directive
<VirtualHost:*:80>
PassEnv HOSTNAME_ENV_VARIABLE_NAME
Define SUBSTITUTE_STRING "s|http://hardcoded-url|http://${HOSTNAME_ENV_VARIABLE_NAME}|i"

<Location /my-path-here-wut-wut/>
        AddOutputFilterByType SUBSTITUTE my/mime-type-here
        Substitute "${SUBSTITUTE_STRING}"
</Location>


</VirtualHost>

When visiting /my-path-here-wut-wut/ which returns HTML containing http://hardcoded-url it will be substituted to http://<HOSTNAME_ENV_VARIABLE_NAME>

OioiGuvnor
  • 21
  • 2