加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

gzip_ratio不在nginx日志中打印

发布时间:2020-12-13 21:16:06 所属栏目:Nginx 来源:网络整理
导读:我正在配置nginx access_log以JSON格式输出以与其他工具一起使用. 我想要的数据包括有关压缩的信息.我有gzip,但我唯一得到的gzip_ratio是 . 要确认,嵌入的var是$gzip_ratio. http://nginx.org/en/docs/http/ngx_http_gzip_module.html 这是我的log_format的

我正在配置nginx access_log以JSON格式输出以与其他工具一起使用.

我想要的数据包括有关压缩的信息.我有gzip,但我唯一得到的gzip_ratio是 – .

要确认,嵌入的var是$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来测试压缩,结果如下:

[~]$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

因此,从实际测量响应的大小来看,它似乎正在被压缩.但是,日志仍然缺少gzip_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","body_bytes_sent": "871","request": "0.676","request_time": "0.676","upstream_response_time": "0.676"}

有谁知道我怎么能得到实际的gzip_ratio?

最佳答案
您使用的是nginx的错误版本,或者您的测试方法存在一些缺陷.我刚刚确认log_format定义中的$gzip_ratio在nginx 1.9.1中运行得非常好.

我看到$gzip_ratio只相等 – 当没有执行压缩时.在所有其他情况下,它保持数值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读