【问题标题】:Sending http requests with Openresty to Google Analytics使用 Openresty 向 Google Analytics 发送 http 请求
【发布时间】:2020-01-27 07:32:28
【问题描述】:

我一直在使用 openresty/nginx+lua 将服务器端点击发送到 Google Analytics Measurement Protocol。 但是,我正在使用的函数 (ngx.location.capture) 与 HTTP/2 不兼容,并且存在“无法修复”的问题。 显然,要走的路是使用“resty.http”模块。我一定是在迁移时做错了什么,因为它不再发送点击数。

这是有效的代码:

location /example {
    resolver 8.8.8.8 ipv6=off;
    access_by_lua_block  {
        local request = {
        v = 1,
        t = "pageview",
        tid = "UA-XXXXXXX-Y",
        cid = ngx.md5(ngx.var.remote_addr .. ngx.var.http_user_agent),
        uip = ngx.var.remote_addr,
        dp = ngx.var.request_uri,
        dr = ngx.var.http_referer,
        ua = ngx.var.http_user_agent,
        ul = ngx.var.http_accept_language
        }

        local res = ngx.location.capture(  "/gamp",  {
        method = ngx.HTTP_POST,
        body = ngx.encode_args(request)
        })
    }
}

    location = /gamp {
    internal;
    expires epoch;
    access_log off;
    proxy_pass_request_headers off;
    proxy_pass_request_body on;
    proxy_pass https://google-analytics.com/collect;
    }

以下是我尝试和失败的方式:

location /example {
        access_by_lua_block  {
                    local request = {
                    v = 1,
                    t = "pageview",
                    tid = "UA-XXXXXXX-Y",
                    cid = ngx.md5(ngx.var.remote_addr .. ngx.var.http_user_agent),
                    uip = ngx.var.remote_addr,
                    dp = ngx.var.request_uri,
                    dr = ngx.var.http_referer,
                    ua = ngx.var.http_user_agent,
                    ul = ngx.var.http_accept_language
                    }

            local http = require "resty.http"
            local httpc = http.new()
            local res, err = httpc:request_uri("https://google-analytics.com/collect", {
            method = "POST",
            body = ngx.encode_args(request)
            })
    }
}

【问题讨论】:

  • 您是否尝试从调用request_uri 时注销err 错误?
  • 是否应该将ngx.encode_args(request) 输出存储在paramsquery 属性中,而不是body?
  • 不确定我是否正确登录,但是当我尝试时,一切都失败了:2019/10/03 16:39:19 [notice] 13572#13572: signal 3 (SIGQUIT) received从 14346,关闭 2019/10/03 16:39:19 [通知] 13574#13574:优雅关闭 2019/10/03 16:39:19 [通知] 13574#13574:退出 2019/10/03 16: 39:19 [通知] 13574#13574:退出

标签: nginx lua http2 openresty measurement-protocol


【解决方案1】:

您需要再添加两个指令:resolverlua_ssl_trusted_certificate

例如,

server {
  ...
  resolver 8.8.8.8 ipv6=off;
  lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
  # optional
  # lua_ssl_verify_depth 5;
  ...
}

【讨论】:

  • 是的,谢谢,这行得通,我完全错过了证书的事情。我奖励赏金迟到了,我重新启动了它,并会在系统允许的情况下尽快给它。
猜你喜欢
  • 2012-04-02
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 2016-11-15
相关资源
最近更新 更多