【发布时间】: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)输出存储在params的query属性中,而不是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