大网数据广告活动
服务器运维问答

Apache2 mod_substitute not working in <Location>

· 52CE 我爱测

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…

问题原文

原帖链接 · Paul_Rent · CC BY-SA 4.0

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.

社区高分解答

原帖链接 · OioiGuvnor · CC BY-SA 4.0

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>

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。