[英]Forwarding apache request to a c++ program
I am basically looking for tips and tricks on how to approach that problem. 我基本上在寻找有关如何处理该问题的提示和技巧。
I have a Server Software (Linux), which is written in C++. 我有一个服务器软件(Linux),它是用C ++编写的。 What I need to do is to provide some information, that is generated inside that software, via a http call to the apache webserver. 我需要做的是通过对apache webserver的http调用来提供在该软件内部生成的一些信息。
Of course it would be possible to store the data in the database and write a servlet for it, or use IPC to get the data, but I want to keep it simple and bundled in one file. 当然可以将数据存储在数据库中并为其编写servlet,或者使用IPC来获取数据,但我想保持简单并捆绑在一个文件中。 And also I want to know if the idea would work. 而且我想知道这个想法是否有效。
So the workflow would look like: Client -> Apache Webserver -> C++ Software 所以工作流程看起来像:Client - > Apache Webserver - > C ++ Software
Is there any existing library or something that could handle the Apache -> C++ connection? 是否有任何现有的库或某些东西可以处理Apache - > C ++连接? Or would it be something like just redirecting the request to a socket in c++ and do it manually? 或者它是否只是将请求重定向到c ++中的套接字并手动执行? Basically something like the tomcat connector. 基本上像tomcat连接器。
Please note that the server software is running permanently in the background, and should not be "started" by the call 请注意,服务器软件在后台永久运行,不应通过呼叫“启动”
There are a few options. 有几个选择。
CGI is simple and has been around forever. CGI很简单,一直存在。 Under CGI, the web server would spawn a separate process for every web request. 在CGI下,Web服务器将为每个Web请求生成一个单独的进程。 As you mentioned in your comment, you could write a CGI script that makes RPC calls to your C++ program. 正如您在评论中提到的,您可以编写一个CGI脚本,对您的C ++程序进行RPC调用。
FastCGI is an alternative to CGI; FastCGI是CGI的替代品; instead of spawning a separate process for every web request, it defines a protocol for letting the web server dispatch multiple web requests to a single long-running process. 它不是为每个Web请求生成一个单独的进程,而是定义了一个协议,用于让Web服务器将多个Web请求分派给一个长时间运行的进程。 It works quite well for web applications. 它适用于Web应用程序。 However, for your scenario, where you have a preexisting server process that needs to add a web interface, it may not work as well; 但是,对于您的场景,您有一个需要添加Web界面的预先存在的服务器进程,它可能无法正常工作; based on my limited understanding, web servers typically expect to start and stop the long-running FastCGI processes themselves (in response to incoming requests, server load, idle time, etc.) instead of connecting to preexisting FastCGI processes. 基于我有限的理解,Web服务器通常希望自己启动和停止长时间运行的FastCGI进程(响应传入的请求,服务器负载,空闲时间等),而不是连接到预先存在的FastCGI进程。 (Most servers would let you reconfigure this, I think, but it's not the default.) (我认为,大多数服务器都会让你重新配置它,但它不是默认设置。)
You can also always embed a web server like Mongoose or cpp-netlib in your C++ process and set up Apache to proxy requests to it. 您还可以在C ++进程中始终嵌入像Mongoose或cpp-netlib这样的Web服务器,并设置Apache来代理对它的请求。 This might be your best approach. 这可能是您最好的方法。 (Mongoose, for example, is extremely easy to embed.) (例如,猫鼬非常容易嵌入。)
Finally, you can use a full-fledged Apache module (either redesign your C++ server as an Apache module or have an Apache module to communicate with your C++ service). 最后,您可以使用完整的Apache模块(将C ++服务器重新设计为Apache模块,或者使用Apache模块与C ++服务进行通信)。 This is probably more complicated than you want to do, although there are existing projects like CPPSERV that take this approach. 这可能比您想要的更复杂,尽管现有的项目如CPPSERV采用这种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.