ZBLOG

你这个配置错了,日志文件记录不到相关信息

非常抱歉给出的示例配置有误。确实在nginx的log_format中并没有提供$connection_time这个内置变量来记录连接时间信息。

如果你想统计上传文件过程中的连接时间,可以考虑使用nginx模块ngx_http_realip_module和ngx_http_geo_module结合,自定义一个变量来记录连接时间。

以下是一个修改后的示例配置:

http {
    geo $conn_start {
        default 0;
        include /etc/nginx/geo/ip_ranges.conf; # 根据实际情况设置IP段
    }

    map $conn_start $connection_time {
        0 "";
        1 "$request_time";
    }

    log_format custom_log '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent" '
                        'upstream_response_time $upstream_response_time '
                        'request_time $request_time '
                        'connection_time $connection_time';

    server {
        listen 80;
        server_name example.com;

        access_log /var/log/nginx/access.log custom_log; # 指定自定义日志格式

        location / {
            set_real_ip_from your_trusted_proxies;
            real_ip_header X-Forwarded-For;

            if ($conn_start = 0) {
                set $conn_start 1;
                rewrite ^(.*)$ /$1 break;
            }

            proxy_pass https://localhost:8080/upload;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        }
    }
}

在上述示例中,我们使用了ngx_http_realip_module来设置真实的客户端IP地址,并且结合ngx_http_geo_module和一个自定义变量$conn_start来判断连接是否为初始连接。同时,使用map指令将$request_time赋值给$connection_time。然后,在log_format中可以直接使用$connection_time来记录连接时间。

请注意根据实际需求修改配置中的IP段和trusted_proxies,以确保准确获取到客户端真实IP。

对于这种高级日志定制需求,你可能需要根据具体情况进行更多的配置调整。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=12615

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?