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 [email protected]
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>
mod_proxy_fcgi adds response headers to r->err_headers_out which means you should use at least:
But there is no reason to not use both:
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.
From troubleshooting in the comments, I think this is a bug - the headers coming back from
mod_proxy_fcgi
seem to be unavailable tomod_headers
in any way, and are being combined with the data frommod_headers
after it processes.For now, if you need this behavior to work correct, maybe look at nginx or lighttpd, or slapping an HAProxy or Varnish instance in front of Apache to do the logging and header manipulation in the right way?
This question is old, but may help someone looking for its final solution:
As covener pointed out with
Header unset
you should also set the condition 'always' when setting the note:Use
%{userid}n
as placeholder for the variable (n from 'internal note' that is were mod_headers saves the value of the variable.From the docs: