通过 NGINX 异步发送标头和 POST 数据到辅助服务器?

发布于 2025-01-02 20:34:41 字数 252 浏览 2 评论 0原文

我想将 NGINX 收到的数据异步发送到另一台服务器,而不会对 NGINX 的请求造成任何延迟。

以下是详细场景:

用户向我的 NGINX 服务器发送了一个请求,该服务器执行一个脚本并显示响应。

在这之间,我想将请求数据发送到另一台服务器,以便我可以操作并获得适当的日志记录和分析。

NGINX 不应该等待来自该服务器的响应,并且在提供用户想要的初始响应时不应该有延迟。

如果需要任何进一步的说明,请告诉我。

I want to send the data received by NGINX to another server asynchronously without causing any delay in serving the request by NGINX.

Here is the detailed scenario:

User sent a request to my NGINX server which executes a script and shows the response.

In between this, I want to send the request data to another server so that I can manipulate and get decent logging and analytics.

NGINX should not wait for the response from this server and there should be no lag in serving the initial response user wanted.

Let me know if any further clarifications are required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笛声青案梦长安 2025-01-09 20:34:41

要将数据异步发送到另一台服务器,您可以使用 Beanstalkd

然而,我们最近设置了一个使用 Beanstalkd 的系统。事实证明,它对处理传入请求的速度有一些负面影响。因此我们决定直接将请求发送到其他服务器。这更加一致。

此致,

To asynchronously send data to another server you can use Beanstalkd.

However, We have recently setup a system in which we used Beanstalkd. It proved to have some negative effect on the speed of working with incoming requests. Therefore we have made the decision to directly send the request to the other server. This worked more consistently.

Best regards,

彻夜缠绵 2025-01-09 20:34:41

由于您确实想要将 POST 数据转发到其他服务器,因此我会考虑使用 PHP、Lua、Ruby... 中的脚本,并执行类似以下操作(类似 PHP):

$data = json_encode($_POST);
file_put_contents("/tmp/data.json", $data);
exec("log-data.sh&");

& 在 exec() 命令的命令行中“在后台”运行该命令,与当前 PHP 实例分离。这里我展示了一个 shell 脚本,它可以是另一个 PHP 脚本,一个 C 程序,Lua,任何你想要的。

事实上,脚本执行了严格的最低限度的操作,即共享 $_POST 数据,然后运行脚本。 (不过,有一点很重要,名称 "/tmp/data.json" 需要动态生成,否则当两个同时发生的请求时,第一个请求的数据会粉碎另一个请求的数据,甚至他们都一致地写入文件...)

如果您打算编写自己的 nginx 模块,您会做同样的事情。您的处理程序会将 POST 数据保存在文件中,然后您将使用 fork() + exec() 启动一个进程来运行另一个脚本、C 程序等背景。不过,这需要您重新编译 nginx。

Since you certainly want to forward the POST data, to the other server, I would look into using a script in PHP, Lua, Ruby... and do something like this (PHP-like):

$data = json_encode($_POST);
file_put_contents("/tmp/data.json", $data);
exec("log-data.sh&");

The & in the command line to the exec() command runs that command "in the background", detached from the current PHP instance. Here I show a shell script, it could be another PHP script, a C program, Lua, whatever you'd like.

The fact is that script does the strict minimum which is share the $_POST data and then run the script. (one important thing, though, the name "/tmp/data.json" needs to be dynamically generated otherwise when two simultaneous requests happen, the data of the first smash the data of the other, or even they both write to the file in concert...)

If you were planning to write your own nginx module, you would do the same thing. Your handler would save the POST data in a file and then you'd start a process with fork() + exec() to run another script, C program, whatnot in the background. This requires you to recompile nginx, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文