ヘッダーの設定解除は apache 2.4.10 および php-fpm では機能しないようです

ヘッダーの設定解除は apache 2.4.10 および php-fpm では機能しないようです

次のように、HTTP ヘッダーを使用して、PHP コードから Apache アクセス ログにヘッダーを戻そうとしています。

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

を使用するとmod_php、ユーザー ID が期待どおりにログに挿入され、クライアントに送信される前にヘッダーが設定解除されます。

次の行を使用して php-fpm 経由で実行する場合、ユーザー ID はログに挿入されず、クライアントの HTTP ヘッダーで設定解除されません。

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

もともと を使っていましたapache_noteが、これは でのみ利用可能ですmod_php。 PHP から Apache/php-fpm または nginx にデータを渡すための解決策として上記を見つけましたが、php-fpm では動作しないようです。

Header unsetphp-fpm で動作させるために、何かを有効にしたり設定したりする必要があるのでしょうか?

仮想ホスト設定:

<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>

答え1

mod_proxy_fcgi は r->err_headers_out に応答ヘッダーを追加します。つまり、少なくとも以下を使用する必要があります。

Header unset X-Userid always

しかし、両方を使用しない理由はありません。

Header always unset X-Userid
Header unset X-Userid

これは、mod_headers に影響を及ぼす Apache API の残念な部分です。ヘッダーは、非成功応答に対して保持されるかどうかに応じて、2 つの場所に存在する可能性があります。

答え2

コメントのトラブルシューティングから、これはバグだと思います。 から返されるヘッダーは、いかなる方法でもmod_proxy_fcgi利用できないようで、処理後にmod_headersからのデータと結合されています。mod_headers

今のところ、この動作を正しく動作させる必要がある場合は、nginx または lighttpd を検討するか、Apache の前に HAProxy または Varnish インスタンスを配置して、ログ記録とヘッダー操作を正しく実行してみてはいかがでしょうか。

答え3

この質問は古いものですが、最終的な解決策を探している人の役に立つかもしれません。

covener が指摘したように、Header unsetメモを設定するときに条件を「常に」設定する必要もあります。

Header always note X-Userid userid
Header always unset X-Userid

%{userid}n変数 (「内部メモ」の n は、mod_headers が変数の値を保存する場所) のプレースホルダーとして使用します。

からドキュメント:

Header [condition] note header value

The optional condition argument determines which internal table 
of responses headers this directive will operate against. Despite the 
name, the default value of onsuccess does not limit an action to 
responses with a 2xx status code. Headers set under this condition are 
still used when, for example, a request is successfully proxied or 
generated by CGI, even when they have generated a failing status code.

関連情報