首页 > 系统相关 >Apache Nginx中记录自定义Header

Apache Nginx中记录自定义Header

时间:2023-08-11 15:38:25浏览次数:43  
标签:body http name 自定义 header request Nginx 1.2 Apache


从Apache切到Nginx需要保持日志格式统一,以便兼容之前的数据统计脚本

现在Apache的日志格式为:



LogFormat"%h %t %m %U %q %>s %{HEAD}i %D"


说明:

%h:客户端IP地址

%t:时间(标准英语格式)

%m:请求的方法(GET,POST)

%U:请求的URL路径,不包含查询字符串

%q:查询字符串

%>s:请求的最终状态

%{HEAD}i:请求头信息

%D:服务器处理请求所用时间,微秒为单位

对应的Nginx日志格式为:





log_format  main  '$remote_addr [$time_local] $request_method $uri '


                  '$query_string $status '


                  '$http_head $request_time ';



其中Apache里的%{HEAD}i这个参数里的HEAD是在程序里自定义了一个header,在Nginx里面取这个HEAD用到的是$http_head参数,head就是自定义的header名称HEAD, Nginx里面需要小写,在这里走了不少弯路。

用firebug获取http header,如下图

有两部分内容,一个是响应头,一个是请求头,请求头是客户端发送给服务器的,包括Requst Line及HTTP Header,响应头是服务器返回给客户端的,包括Status Line及HTTP Header。关于http header的说明,这里有一篇很好的文章可以参考。

在Nginx里面可以用$upstream_http_’header名称’来获取响应头信息,比 如$upstream_http_content_type将会获取到类似text/html; charset=utf-8的内容,说明返回的文件类型及编码格式。在我的需求中是要获取自定义的请求头的内容,需要使用$sent_http_’自定义 header名称’来获取,但我测试过程中没有取到,用$http_’自定义header名称’才取到,也许和Nginx版本有关,我的版本是 1.4.2。

另外,测试过程中用的是firefox的poster插件,可以自定义请求头,截图如下:

自定义一个header,然后输入URL,点GET即可发送带有自定义header的请求,在服务器端查看日志可以看到取到了‘AAAAAAAAAAAAAAAAAAAAAA’.

nginx.org里关于log_format参数的说明,英文不好,看着费劲。

The ngx_http_core_module module supports embedded variables with names matching the Apache Server variables. First of all, these are variables representing client request header fields, such as $http_user_agent, $http_cookie, and so on. Also there are other variables:

$arg_name
argument name in the request line

$args
arguments in the request line

$binary_remote_addr
client address in a binary form, value’s length is always 4 bytes

$body_bytes_sent
number of bytes sent to a client, not counting the response header; this variable is compatible with the “%B” parameter of the mod_log_config Apache module

$bytes_sent
number of bytes sent to a client (1.3.8, 1.2.5)

$connection
connection serial number (1.3.8, 1.2.5)

$connection_requests
current number of requests made through a connection (1.3.8, 1.2.5)

$content_length
“Content-Length” request header field

$content_type
“Content-Type” request header field

$cookie_name
the name cookie

$document_root
root or alias directive’s value for the current request

$document_uri
same as $uri

$host
in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name matching a request

$hostname
host name

$http_name
arbitrary request header field; the last part of a variable name is the field name converted to lower case with dashes replaced by underscores

$https
“on” if connection operates in SSL mode, or an empty string otherwise

$is_args
“?” if a request line has arguments, or an empty string otherwise

$limit_rate
setting this variable enables response rate limiting; see limit_rate

$msec
current time in seconds with the milliseconds resolution (1.3.9, 1.2.6)

$nginx_version
nginx version

$pid
PID of the worker process

$pipe
“p” if request was pipelined, “.” otherwise (1.3.12, 1.2.7)

$proxy_protocol_addr
client address from the PROXY protocol header, or an empty string otherwise (1.5.12)
The PROXY protocol must be previously enabled by setting the proxy_protocol parameter in the listen directive.

$query_string
same as $args

$realpath_root
an absolute pathname corresponding to the root or alias directive’s value for the current request, with all symbolic links resolved to real paths

$remote_addr
client address

$remote_port
client port

$remote_user
user name supplied with the Basic authentication

$request
full original request line

$request_body
request body
The variable’s value is made available in locations processed by the proxy_pass, fastcgi_pass, uwsgi_pass, and scgi_pass directives.

$request_body_file
name of a temporary file with the request body
At the end of processing, the file needs to be removed. To always write the request body to a file, client_body_in_file_only needs to be enabled. When the name of a temporary file is passed in a proxied request or in a request to a FastCGI/uwsgi/SCGI server, passing the request body should be disabled by the proxy_pass_request_body off, fastcgi_pass_request_body off, uwsgi_pass_request_body off, or scgi_pass_request_body off directives, respectively.

$request_completion
“OK” if a request has completed, or an empty string otherwise

$request_filename
file path for the current request, based on the root or alias directives, and the request URI

$request_length
request length (including request line, header, and request body) (1.3.12, 1.2.7)

$request_method
request method, usually “GET” or “POST”

$request_time
request processing time in seconds with a milliseconds resolution (1.3.9, 1.2.6); time elapsed since the first bytes were read from the client

$request_uri
full original request URI (with arguments)

$scheme
request scheme, “http” or “https”

$sent_http_name
arbitrary response header field; the last part of a variable name is the field name converted to lower case with dashes replaced by underscores

$server_addr
an address of the server which accepted a request
Computing a value of this variable usually requires one system call. To avoid a system call, the listen directives must specify addresses and use the bind parameter.

$server_name
name of the server which accepted a request

$server_port
port of the server which accepted a request

$server_protocol
request protocol, usually “HTTP/1.0” or “HTTP/1.1”

$status
response status (1.3.2, 1.2.2)

$tcpinfo_rtt, $tcpinfo_rttvar, $tcpinfo_snd_cwnd, $tcpinfo_rcv_space
information about the client TCP connection; available on systems that support the TCP_INFO socket option

$time_iso8601local
time in the ISO 8601 standard format (1.3.12, 1.2.7)

$time_local

local time in the Common Log Format (1.3.12, 1.2.7)

$uri
current URI in request, normalized
The value of $uri may change during request processing, e.g. when doing internal redirects, or when using index files.

参考:http://blog.51yip.com/apachenginx/1277.html


标签:body,http,name,自定义,header,request,Nginx,1.2,Apache
From: https://blog.51cto.com/u_6186189/7048279

相关文章

  • nginx优化加强战斗力及遇到的坑解决
    先说遇到个坑,第一个是负载问题,这个问题与架构有关,由于我设计架构多了两层,结果导致会话负载只转向一个。解决这样的问题思路有两个:一是改变负载策略,二是更改架构设计。由于采用动静分离部署,而nginx又设计了静态,结果客户端去读nginx静态,访问量上来,页面加载很慢。解决:二者......
  • 第一章 安装Nginx+Lua开发环境
    首先我们选择使用OpenResty,其是由Nginx核心加很多第三方模块组成,其最大的亮点是默认集成了Lua开发环境,使得Nginx可以作为一个WebServer使用。借助于Nginx的事件驱动模型和非阻塞IO,可以实现高性能的Web应用程序。而且OpenResty提供了大量组件如Mysql、Redis、Memcached等等,使在Ng......
  • Nginx+Lua开发入门
    Nginx入门本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章:nginx启动、关闭、重启agentzh的Nginx教程http://openresty.org/download/agentzh-nginx-tutorials-zhcn.htmlNginx+Lua入门http://17173ops.com/2013/11/01/17173-ngx-lua-manual.shtmlnginx配置指令的......
  • nginx三种获取用户真实ip的方法
    随着nginx的迅速崛起,越来越多公司将apache更换成nginx.同时也越来越多人使用nginx作为负载均衡,并且代理前面可能还加上了CDN加速,但是随之也遇到一个问题:nginx如何获取用户的真实IP地址,如果后端是apache,请跳转到<apache获取用户真实IP地址>,如果是后端真实服务器是nginx,那么继续......
  • Nginx 安全的配置项
    1 漏扫出现的问题1.1 检测到目标X-Content-Type-Options响应头缺失修复方法:nginx 增加响应头配置:add_header X-Content-Type-Options "nosniff"  always; 详细解释:X-Content-Type-Options头信息是一种安全策略,用于防止浏览器在解析响应内容类型时执行MIME类型嗅探......
  • 借助PageSpeed,为Nginx网站服务器提速
    网站加载速度越快,访客互动性、留住率和转换率就越高,这早已不是什么秘密。网站每延迟100毫秒,亚马逊的销售额就会减少1%;延迟增加500毫秒,这意味着谷歌的流量和收入就会减少20%。要是有一个办法可以为你的网站服务器提速,又不必升级到功能更强大的服务器,就没有理由不试一试这个办法。......
  • openresty(nginx)、lua、drizzle测试
    一、概述:1.研究目标:nginx中使用lua脚本,及nginx直接访问mysql,redis2.需要安装的内容:openresty,mysql,redis3.OpenResty(也称为ngx_openresty)是一个全功能的Web应用服务器。它打包了标准的Nginx核心,很多的常用的第三方模块,以及它们的大多数依赖项。http://openresty.org/cn/ind......
  • nginx or apache前端禁收录,爬虫,抓取
    一、Nginx规则直接在server 中新增如下规则即可:##################################################禁止蜘蛛抓取动态或指定页面规则By##################################################server{listen80;server_namezhangge.net;indexindex.htmlindex.......
  • #yyds干货盘点#nginx中fastcgi_params文件及相应配置
    在ubuntu服务器安装完php7.4-fdm和nginx后,发现fastcgi_params没有生成,也可能是二次安装的关系。所以临时去网上找了个手工建上。特意在这里记录下,避免下次再遇到同样的问题。#脚本文件请求的路径,也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如......
  • 在生产环境中使用Apache Mesos和Docker
    本文翻译自IVOVERBERK博客,Docker容器软件已受到了从科技巨头到企业的广泛注意。但是,随着容器概念转变成为现实世界中的成熟技术,那么问题就变成了:怎么样才能快速把Docker应用于生产环境中呢?介绍在生产环境中安全有效地的运行Docker容器会有很多复杂的挑战。许多复杂性挑战都是在......