使用 Openresty 发送 http 请求到 Google Analytics
Sending http requests with Openresty to Google Analytics
我一直在使用 openresty/nginx+lua 将服务器端点击发送到 Google Analytics Measurement Protocol。
但是,我使用的函数 (ngx.location.capture
) 与 HTTP/2 不兼容,并且存在 "won't fix" 问题。
显然,要走的路是使用 '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)
})
}
}
您需要再添加两个指令:resolver
和 lua_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;
...
}
我一直在使用 openresty/nginx+lua 将服务器端点击发送到 Google Analytics Measurement Protocol。
但是,我使用的函数 (ngx.location.capture
) 与 HTTP/2 不兼容,并且存在 "won't fix" 问题。
显然,要走的路是使用 '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)
})
}
}
您需要再添加两个指令:resolver
和 lua_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;
...
}