
我正在嘗試使用 HTTP 標頭將 php 程式碼中的標頭傳回 apache accesslog,如下所示:
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 unset
在 php-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
這是 Apache API 中一個不幸的部分,它會滲透到 mod_headers 中——標頭可以存在於兩個位置,這取決於它們是否要針對不成功的回應而持續存在。
答案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.