【问题标题】:Combining a proxy rewrite in apache http server with header manipulation将 apache http 服务器中的代理重写与标头操作相结合
【发布时间】:2016-10-19 09:59:11
【问题描述】:

我必须修改通过我在 apache http 服务器中设置的代理重写指令传递的响应标头。

这个简单的例子一切正常,它无条件地从后端服务器为所有请求传递目标:

<VirtualHost ...>
  ...
  # unconditionally modify headers
  Header set Content-Type "text/html"
  Header unset Content-Disposition
  Header unset Content-Transfer-Encoding
  # fetch goal from backend
  RewriteEngine on
  SSLProxyEngine on
  RewriteRule ^ https://back.example.org/goal [P]
  ...
  # prevent all access to the file system
  DocumentRoot /var/www/html
  <Directory /var/www/html>
    Options none
    Order deny,allow
    Deny from All
  </Directory>
</VirtualHost>

但是问题是主机还必须从另一个后端服务器传送一些静态文件,其中标头必须被更改!所以我尝试在第一步重写虚拟目标,应用标题修改规则,然后在第二步进行代理:

<VirtualHost ...>
  ...
  # modify headers only for /goal
  <Location /goal>
    Header set Content-Type "text/html"
    Header unset Content-Disposition
    Header unset Content-Transfer-Encoding
  </Location>
  # fetch static exceptions from static backend
  RewriteEngine on
  SSLProxyEngine on
  RewriteRule static-1$ https://static.example.org/static-1 [P]
  RewriteRule static-2$ https://static.example.org/static-2 [P]
  RewriteRule static-3$ https://static.example.org/static-3 [P]
  # two step rewrite and proxy to fetch goal from backend and get the headers modified
  RewriteRule ^/goal$  https://back.xample.org/goal [P]
  RewriteRule ^ /goal [PT,N]
  ...
  # prevent all access to the file system
  DocumentRoot /var/www/html
  <Directory /var/www/html>
    Options none
    Order deny,allow
    Deny from All
  </Directory>
</VirtualHost>

然而,这给我留下了 http 状态 403。

我使用重写日志来进一步了解,实际上根据第一步,请求被重写为/goal,再次调用 URI -to-filehandler API,然后请求被简单地映射到文件系统,这解释了403.为什么下一轮不应用第二个重写步骤,代理步骤?

或者,我的实际问题是:如何将标头修改应用于包罗万象的重写代理规则结果,但从标头修改中定义一些明确的例外?

【问题讨论】:

    标签: apache http-headers mod-proxy


    【解决方案1】:

    好吧,戳了一下mir,因为我既没有收到答案也没有收到评论……(为什么我的大部分问题都只是一堵沉默的墙?)。

    一个可行的解决方案是对静态内容异常使用否定的LocationMatch 指令。不性感,但工作:

    <VirtualHost ...>
      ...
      RewriteEngine on
      SSLProxyEngine on
      # fetch static exceptions from static backend
      RewriteRule static-1$ https://static.example.org/static-1 [P]
      RewriteRule static-2$ https://static.example.org/static-2 [P]
      RewriteRule static-3$ https://static.example.org/static-3 [P]
      # proxy goal from backend
      RewriteRule ^  https://back.xample.org/goal [P]
      ...
      # modify headers, but _not_ for static exceptions
      <LocationMatch "^/(?!static)">
        Header set Content-Type "text/html"
        Header unset Content-Disposition
        Header unset Content-Transfer-Encoding
      </LocationMatch>
      ...
      # prevent all access to the file system
      DocumentRoot /var/www/html
      <Directory /var/www/html>
        Options none
        Order deny,allow
        Deny from All
      </Directory>
    </VirtualHost>
    

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 2016-10-12
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 2010-11-27
      相关资源
      最近更新 更多