gpt4 book ai didi

nginx - 使用 Openresty 向 Google Analytics 发送 http 请求

转载 作者:行者123 更新时间:2023-12-04 15:40:05 26 4
gpt4 key购买 nike

我一直在使用 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)
})
}
}

最佳答案

您需要再添加两个指令: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;
...
}

关于nginx - 使用 Openresty 向 Google Analytics 发送 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58126655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com