简体   繁体   English

是否可以使用 Apache 记录所有 HTTP 请求标头?

[英]Is it possible to log all HTTP request headers with Apache?

How to make a record into the logfile the contents of the HTTP request header (all) as received by apache?如何将apache接收到的HTTP请求头(全部)的内容记录到日志文件中?

Currently my apache combined log format configuration is:目前我的apache组合日志格式配置是:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\"" combined

I understand that it is possible to do it so:我知道可以这样做:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\" \"%{heading name}i\" \"%{heading name}i\" \"%{heading name}i\"" combined

but it is not logical and it is not possible to know which headers will be.但这不合逻辑,也不可能知道哪些标题是。

mod_log_forensic is what you want, but it may not be included/available with your Apache install by default. mod_log_forensic是您想要的,但默认情况下,您的 Apache 安装可能不包含/不提供它。

Here is how to use it.这是如何使用它。

LoadModule log_forensic_module /usr/lib64/httpd/modules/mod_log_forensic.so 
<IfModule log_forensic_module> 
ForensicLog /var/log/httpd/forensic_log 
</IfModule> 

Here is a list of all http-headers: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields这是所有 http 标头的列表:http: //en.wikipedia.org/wiki/List_of_HTTP_header_fields

And here is a list of all apache-logformats: http://httpd.apache.org/docs/2.0/mod/mod_log_config.html#formats这是所有 apache-logformats 的列表: http : //httpd.apache.org/docs/2.0/mod/mod_log_config.html#formats

As you did write correctly, the code for logging a specific header is %{foobar}i where foobar is the name of the header.正如您写得正确,记录特定标题的代码是 %{foobar}i ,其中 foobar 是标题的名称。 So, the only solution is to create a specific format string.因此,唯一的解决方案是创建特定的格式字符串。 When you expect a non-standard header like x-my-nonstandard-header, then use %{x-my-nonstandard-header}i .当您期望像 x-my-nonstandard-header 这样的非标准标头时,请使用%{x-my-nonstandard-header}i If your server is going to ignore this non-standard-header, why should you want to write it to your logfile?如果你的服务器要忽略这个非标准头,你为什么要把它写到你的日志文件中? An unknown header has absolutely no effect to your system.未知标头对您的系统绝对没有影响。

If you're interested in seeing which specific headers a remote client is sending to your server, and you can cause the request to run a CGI script, then the simplest solution is to have your server script dump the environment variables into a file somewhere.如果您有兴趣查看远程客户端向您的服务器发送哪些特定标头,并且您可以使请求运行 CGI 脚本,那么最简单的解决方案是让您的服务器脚本将环境变量转储到某个文件中。

eg run the shell command "env > /tmp/headers" from within your script例如,从脚本中运行 shell 命令“env > /tmp/headers”

Then, look for the environment variables that start with HTTP_...然后,查找以 HTTP_ 开头的环境变量...

You will see lines like:你会看到像这样的行:

HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_ENCODING=gzip, deflate
HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.5
HTTP_CACHE_CONTROL=max-age=0

Each of those represents a request header.每一个都代表一个请求头。

Note that the header names are modified from the actual request.请注意,标头名称是根据实际请求修改的。 For example, "Accept-Language" becomes "HTTP_ACCEPT_LANGUAGE", and so on.例如,“Accept-Language”变为“HTTP_ACCEPT_LANGUAGE”,依此类推。

In my case easiest way to get browser headers was to use php.在我的情况下,获取浏览器标头的最简单方法是使用 php。 It appends headers to file and prints them to test page.它将标题附加到文件并将它们打印到测试页。

<?php
$fp = fopen('m:/temp/requests.txt', 'a');
$time = $_SERVER['REQUEST_TIME'];
fwrite($fp, $time + "\n");
echo "$time.<br>";
foreach (getallheaders() as $name => $value) {
    $cur_hd = "$name: $value\n";
    fwrite($fp, $cur_hd);
    echo "$cur_hd.<br>";
}
fwrite($fp, "***\n");
fclose($fp);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM