Header unset seems not to work with apache 2.4.10 and php-fpm

apache-2.2apache-2.4php-fpm

I'm trying to pass headers from php code back to the apache accesslog by using HTTP headers, like so:

Header note X-Userid userid
Header unset X-Userid

LogFormat "%h %l %{userid}n %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined_with_php_userid
CustomLog /var/log/apache2/access_log combined_with_php_userid

With mod_php, the userid is inserted into the log as expected, and the header is unset before being sent to the client.

When running via php-fpm, using the following line, the userid is not inserted in the log and is not unset in the client HTTP headers.

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9001/var/html/$1

Originally I was using apache_note but this is only available with mod_php. I found the above as a solution for passing data from PHP to Apache/php-fpm or nginx, but it doesn't seem to work with php-fpm.

Is there something I need to enable or set to get Header unset working under php-fpm?

Virtual Host Config:

<VirtualHost *:80>
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9001/web/ee2/sites/site.com/$1
    ServerAdmin webmaster@site.dev
    DocumentRoot /web/ee2/sites/site.com
    ServerName site.dev

    Header note X-Userid userid
    Header unset X-Userid

    ErrorLog  /var/log/apache2/site.dev-error_log
    LogFormat "%h %l %{userid}n %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined_with_php_userid
    # also tried: # LogFormat "%h %l %{X-Userid}i %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined_with_php_userid
    CustomLog /var/log/apache2/searchenginenews.com-access_log combined_with_php_userid

    <Directory /web/ee2/sites/site.com>
        AllowOverride All
        Require all granted
    </Directory>

</VirtualHost>

Best Answer

mod_proxy_fcgi adds response headers to r->err_headers_out which means you should use at least:

Header unset X-Userid always

But there is no reason to not use both:

Header always unset X-Userid
Header unset X-Userid

This is an unfortunate part of the Apache API that bleeds into mod_headers -- headers can live in two places depending if they're meant to persist for non-success responses.