向 ProxyPass 请求添加自定义标头

Sim*_*mon 11 http-headers apache-2.2 apache-2.4

我有一个简单的 apache 虚拟主机:

<VirtualHost *:80>
  ServerName hello.local

  ProxyPass / http://localhost:8810/
  ProxyPassReverse / http://localhost:8810/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

所有对 hello.local 的请求都被代理到http://localhost:8810/. 我想要做的是向 http 请求添加一个标头,该请求将http://localhost:8810/使用外部命令返回的值。就像是

Header set MyHeader ${/usr/bin/an_external_program}

有什么办法可以做到这一点吗?

Sim*_*mon 11

好,我知道了。

首先,执行的脚本用于获取要插入到标题中的值。我将其创建为/opt/apache/debug.sh

#!/bin/bash

#this script just loops forever and outputs a random string
#every time it receives something on stdin

while read
do
        cat /dev/urandom|head -c12|base64
done
Run Code Online (Sandbox Code Playgroud)

阿帕奇配置:

<VirtualHost *:80>
        ServerName light.nik

        RewriteEngine On

        RewriteMap doheader prg:/opt/apache/debug.sh
        RewriteRule (.*) - [E=customheader:${doheader:},P]

        RequestHeader set customheader %{customheader}e

        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

运行的后端服务http://localhost:8080/接收customheader来自脚本的值。

关于使用外部程序的 Apache 文档在这里