我正在configurationnginx access_log以JSON格式输出,以便与其他工具一起使用。
我想要的数据包括有关压缩的信息。 我有gzip,但我得到的唯一的gzip_ratio是- 。
为了确认,embedded的$gzip_ratio是$gzip_ratio 。
http://nginx.org/en/docs/http/ngx_http_gzip_module.html
这里是我的log_format的定义:
log_format main_json '{"time": "$time_iso8601", ' '"remote_addr": "$remote_addr", ' '"body_bytes_sent": "$body_bytes_sent", ' '"gzip_ratio": "$gzip_ratio", ' '"status": "$status", ' '"request": "$request_time", ' '"request_method": "$request_method", ' '"http_referrer": "$http_referer", ' '"http_user_agent": "$http_user_agent", ' '"http_x_forwarded_for": "$http_x_forwarded_for", ' '"request_time": "$request_time", ' '"upstream_response_time": "$upstream_response_time"}';
这里是nginx.conf中的gzip设置:
gzip on; gzip_proxied any; gzip_types text/plain text/xml text/css application/x-javascript text/javascript application/xml+rss text/json application/json;
这里是access_log的输出:
{ "time":"2015-02-03T14:26:26+00:00", "remote_addr":"[IP]", "body_bytes_sent":"574", "gzip_ratio":"-", "status":"200", "request":"0.064", "request_method":"GET", "http_referrer":"-", "http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2293.0 Safari/537.36", "http_x_forwarded_for":"-", "request_time":"0.064", "upstream_response_time":"0.064" }
所以,事情似乎没有被压缩。 但是,我运行cURL来testing压缩,这里是结果:
[~]$ curl https://[URL] --silent --write-out "size_download=%{size_download}\n" --output /dev/null size_download=3297 [~]$ curl https://[URL] --silent -H "Accept-Encoding: gzip,deflate" --write-out "size_download=%{size_download}\n" --output /dev/null size_download=859
所以,从实际测量响应的大小来看,似乎正在被压缩。 但是,日志仍然缺lessgzip_ratio 。 每个请求日志中的body_bytes_sent与由cURL报告的字节(压缩响应略有变化)匹配。
{"time": "2015-02-03T14:57:11+00:00", "remote_addr": "[IP]", "body_bytes_sent": "3297", "gzip_ratio": "-", "status": "200", "request": "0.477", "request_method": "GET", "http_referrer": "-", "http_user_agent": "curl/7.37.0", "http_x_forwarded_for": "-", "request_time": "0.477", "upstream_response_time": "0.477"} {"time": "2015-02-03T14:57:20+00:00", "remote_addr": "[IP]", "body_bytes_sent": "871", "gzip_ratio": "-", "status": "200", "request": "0.676", "request_method": "GET", "http_referrer": "-", "http_user_agent": "curl/7.37.0", "http_x_forwarded_for": "-", "request_time": "0.676", "upstream_response_time": "0.676"}
任何人都知道我可以得到实际的gzip_ratio ?
要么你正在使用一个bug的版本的nginx或者你的testing方法有一些缺陷。 我刚刚确认$gzip_ratio在log_format定义在nginx 1.9.1中工作得很好。
我已经看到$gzip_ratio只有相等-当没有压缩执行。 在所有其他情况下,它保存一个数字值。