创建与 nginx 通信的 C++ 应用程序的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9083869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:16:24  来源:igfitidea点击:

Best method to create a c++ app to communicate with nginx

c++httpnode.jsnginxfastcgi

提问by Vivek Goel

I need to write a C++ interface that can read our data structure and provide the o/p based on query using http protocol.

我需要编写一个 C++ 接口,它可以读取我们的数据结构并使用 http 协议提供基于查询的 o/p。

Server Need
It should be able to serve 100 clients at the same time.

服务器需求
它应该能够同时为 100 个客户端提供服务。

Why C++
All code is already written in C++. So we need to just write a http layer in C++. That's why I am choosing C++ instead of a more conventional web-programming language.

为什么是 C++
所有代码都已经用 C++ 编写了。所以我们只需要在 C++ 中编写一个 http 层即可。这就是为什么我选择 C++ 而不是更传统的网络编程语言。

I am thinking to use nginx to serve static files and use its proxy pass to communicate with C++.

我正在考虑使用 nginx 来提供静态文件并使用其代理传递与 C++ 进行通信。

There are two approaches I have found:

我发现有两种方法:

  • Write a FastCGI c++ module.

  • Write a node.js c++ module.

  • Please just any other suggestion if you have

  • 编写一个 FastCGI c++ 模块。

  • 编写一个 node.js c++ 模块。

  • 如果您有任何其他建议,请

Can you please list the pros and cons for each method based on prior experience?

您能否根据先前的经验列出每种方法的优缺点?

采纳答案by Vivek Goel

I think I will go forward with Nginx module devlopment http://www.evanmiller.org/nginx-modules-guide.html

我想我会继续 Nginx 模块开发http://www.evanmiller.org/nginx-modules-guide.html

Why ?

为什么 ?

  1. It don't require any other library dependency like fastcgi and other.
  2. I can use all feature of nginx inside my module.
  1. 它不需要任何其他库依赖项,如 fastcgi 和其他。
  2. 我可以在我的模块中使用 nginx 的所有功能。

回答by Christopher Smith

No one here seems to have addressed the actual question, though some nice work arounds have been offered. I've been able to build C++ modules for nginx with a couple of minor changes.

尽管已经提供了一些不错的解决方法,但似乎没有人解决实际问题。我已经能够通过一些小的更改为 nginx 构建 C++ 模块。

  1. Change the module source file name to end with .cpp so gcc realizes it is dealing with C++.
  2. Make sure all your nginx includes (e.g. ngx_config.h, ngx_core.h, etc.) are wrapped with an extern "C" { } structure. Similarly make sure any functions called through nginx function pointers are declared with a wrapper.
  3. Add --with-ld-opt="-lstdc++" to your "configure" invocation when setting up nginx.
  1. 将模块源文件名更改为以 .cpp 结尾,以便 gcc 意识到它正在处理 C++。
  2. 确保所有 nginx 包含(例如 ngx_config.h、ngx_core.h 等)都用 extern "C" { } 结构包裹。同样,确保通过 nginx 函数指针调用的任何函数都使用包装器声明。
  3. 设置 nginx 时,将 --with-ld-opt="-lstdc++" 添加到“配置”调用中。

With those three steps your module should compile, build, link, and actually work.

通过这三个步骤,您的模块应该可以编译、构建、链接并实际工作。

回答by Kimmeh

What you are asking is basically how to turn the c++ process that holds your data strutures into a webserver. That might not be the best way to go about it. (Then again, maybe it is in your situation. It depends on the complexity of the c++ process's interfaces you are trying to expose i guess.)

您要问的基本上是如何将保存数据结构的 C++ 进程转换为网络服务器。这可能不是最好的方法。(话又说回来,也许这在你的情况下。这取决于你试图公开的 C++ 进程接口的复杂性,我猜。)

Anyways, I would try to stick a small http frontend in between the c++ process and the clients that could do the http work and communicate with the c++ backend process using some simple messaging protocol like ZeroMQ/zmq.

无论如何,我会尝试在 c++ 进程和可以执行 http 工作并使用一些简单的消息传递协议(如ZeroMQ/zmq )与 c++ 后端进程通信的客户端之间插入一个小的 http 前端。

zmqin c/c++ is fairly straight forward, and its very efficient and very fast. Using zmq you could very quickly setup a simple webserver frontend in python, or whatever language you prefer that has zmq bindings, and have that frontend communicate asyncronously or syncronously with the backend c++ process using zmq.

c/c++ 中的zmq相当简单,而且非常高效和快速。使用 zmq,您可以非常快速地在 python 中设置一个简单的网络服务器前端,或者您喜欢的任何具有zmq 绑定的语言,并让该前端使用 zmq 与后端 c++ 进程异步或同步通信。

The c++ examplesand the guideare nice starting points if you are looking into using zmq.

如果您正在考虑使用 zmq ,c++ 示例指南是不错的起点。

For Node.js there are also a few examples.

对于 Node.js 也有一些例子

回答by Peter

Try G-WAN, it allows you to use your c++ application directly.

试试 G-WAN,它允许你直接使用你的 C++ 应用程序。

回答by Oktaheta

You may try nginx c function

你可以试试nginx c 函数

It is simple to use and built in nginx cache memory on apps layer, wiki for nginx c function

使用简单,应用层内置nginx缓存,nginx c函数wiki

Example project with cpp

使用 cpp 的示例项目

Sample code:

示例代码:

#include <stdio.h>
#include <ngx_http_c_func_module.h>

/*** build the program as .so library and copy to the preferred place for nginx to link this library ***/
/*** gcc -shared -o libcfuntest.so -fPIC cfuntest.c ***/
/*** cp libcfuntest.so /etc/nginx/ ***/

int is_service_on = 0;

void ngx_http_c_func_init(ngx_http_c_func_ctx_t* ctx) {
    ngx_http_c_func_log(info, ctx, "%s", "Starting The Application");


    is_service_on=1;
}



void my_app_simple_get_greeting(ngx_http_c_func_ctx_t *ctx) {
    ngx_http_c_func_log_info(ctx, "Calling back and log from my_app_simple_get");

    ngx_http_c_func_write_resp(
        ctx,
        200,
        "200 OK",
        "text/plain",
        "greeting from ngx_http_c_func testing"
    );
}

void my_app_simple_get_args(ngx_http_c_func_ctx_t *ctx) {
    ngx_http_c_func_log_info(ctx, "Calling back and log from my_app_simple_get_args");

    ngx_http_c_func_write_resp(
        ctx,
        200,
        "200 OK",
        "text/plain",
        ctx->req_args
    );
}

void my_app_simple_get_token_args(ngx_http_c_func_ctx_t *ctx) {
    ngx_http_c_func_log_info(ctx, "Calling back and log from my_app_simple_get_token_args");

    char * tokenArgs = ngx_http_c_func_get_query_param(ctx, "token");
    if (! tokenArgs) {
        ngx_http_c_func_write_resp(
            ctx,
            401,
            "401 unauthorized",
            "text/plain",
            "Token Not Found"
        );
    } else {
        ngx_http_c_func_write_resp(
            ctx,
            401,
            "401 unauthorized",
            "text/plain",
            tokenArgs
        );
    }
}

void my_app_simple_post(ngx_http_c_func_ctx_t *ctx) {
    ngx_http_c_func_log_info(ctx, "Calling back and log from my_app_simple_post");

    ngx_http_c_func_write_resp(
        ctx,
        202,
        "202 Accepted and Processing",
        "text/plain",
        ctx->req_body
    );
}



void my_app_simple_get_no_resp(ngx_http_c_func_ctx_t *ctx) {
    ngx_http_c_func_log_info(ctx, "Calling back and log from my_app_simple_get_no_resp");


}

void ngx_http_c_func_exit(ngx_http_c_func_ctx_t* ctx) {
    ngx_http_c_func_log(info, ctx, "%s\n", "Shutting down The Application");

    is_service_on = 0;
}