如何记录容器中的http请求和时间成本?

How to record http requests and time cost in containers?

情况是这样的,我正在使用docker在容器中构建一些项目,我想记录这些容器的请求url以优化这些作业。

所以我找到了一种方法 运行 一个 Nginx 容器作为转发代理,称为 proxy 和 运行 容器中的其他构建作业 http_proxy.

代理:

docker run -d -p 8090:8090 proxy

职位:

docker run --env http_proxy="http://127.0.0.1:8090" --network host jobs 

但我找不到正确的 Nginx 配置来执行此操作。

➜ cat nginx.conf                                                                                
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        listen       443;
        server_name _;
        # forward proxy for CONNECT request
        proxy_connect;
        proxy_connect_allow            443 563;
        proxy_connect_connect_timeout  10s;
        proxy_connect_read_timeout     10s;
        proxy_connect_send_timeout     10s;

        location / {
            resolver 8.8.8.8;
            proxy_pass $scheme://$host$request_uri;
        }
    }
}

我也尝试使用Envoy来代理容器,我看了文档Front Proxy好像不是正向代理,那么在容器中记录http请求和时间成本的推荐方法是什么?

任何帮助将不胜感激。

我用Nginx解决了这个问题,其实用Nginx作为透明转发代理很容易做到这一点,Nginx需要ngx_http_proxy_connect_module代理HTTPS请求,作者也把这个模块贡献给了Tengine .所以我尝试使用Tengine。

worker_processes  1;

events {
    worker_connections  65536;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       *:80; # fix 99 address not available
        listen       *:443;# fix 99 address not available
        server_name localhost;
        resolver 10.10.10.10 ipv6=off;
        resolver_timeout 30s;


        # forward proxy for CONNECT request
        proxy_connect;
        proxy_connect_allow            443 563;
        proxy_connect_connect_timeout  30s;
        proxy_connect_read_timeout     30s;
        proxy_connect_send_timeout     30s;

        location / {
            proxy_pass $scheme://$host$request_uri;
        }
        access_log /tmp/access.log;
        error_log /tmp/error.log;
    }
}

上面的conf是我的Nginx.conf。为了避免 connection error while connecting to upstream,我禁用了 ipv6 选项。有用。