C/C++ 中的高性能应用程序 Web 服务器

发布于 2024-11-16 07:58:14 字数 326 浏览 2 评论 0原文

有没有用 C 或 C++ 编写的高性能(理想情况下事件化且开源)Web 服务器?

我希望能够使用它,因为它使用填写的 HTTP 请求类/结构调用我的应用程序中的方法/函数,然后我可以向它返回填写的 HTTP 响应类/结构。

如果它不是开源的,我需要内置对长轮询连接、保持活动等的支持,否则,我认为我可以自己添加这些东西。

如果您不知道有任何此类服务器可用,您会建议编写我自己的 Web 服务器来完成任务吗?它不能是基于文件的,并且必须用高性能C/C++编写。


编辑:我正在考虑类似 C 的 Ruby Mongrel 之类的东西,如果有帮助的话。

Is there any high performance (ideally evented and open source) web server in C or C++?

I'd like to be able to use it in that it calls a method/function in my application with a filled out HTTP Request class/struct, and then I can return a filled out HTTP Response class/struct to it.

If it isn't open source, I'd need built in support for long-polling connections, keep-alive, etc—otherwise, I think that I can add these things myself.

If you don't know of any such servers available, would you recommend writing my own web server to fit the task? It cannot be file-based, and must be written in high-performance C/C++.


Edit: I'm thinking something like the Ruby Mongrel for C, if that helps.

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

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

发布评论

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

评论(5

叶落知秋 2024-11-23 07:58:15

我对我的工作有完全相同的要求,因此我评估了许多解决方案:mongoose、libmicrohttpd、libevent。我也在考虑编写 nginx 模块。以下是我的发现摘要:

nginx

nginx 项目页面

我喜欢这个服务器并且经常使用它。它的性能和资源使用率比 Apache 好得多,我仍然在使用 Apache,但计划迁移到 nginx。

  • 非常好的可调性能。功能丰富。可移植性。
  • 模块 API 没有文档记录,而且似乎非常冗长。请参阅此 nginx hello world 模块 作为示例。
  • Nginx不使用线程,而是使用多进程。这使得编写模块变得更加困难,需要学习共享内存的nginx API等。

mongoose

mongoose 项目页面

  • 所有服务器的代码是在单个 mongoose.c 文件中(大约 130K),没有依赖项。这很好。
  • 每个连接一个线程,因此如果您需要并发性,则必须配置大量线程,即。高内存使用率。不太好。
  • 性能很好,但并不特别。
  • API很简单,但你必须自己编写所有响应HTTP标头,即。详细学习HTTP协议。

libmicrohttpd

libmicrohttpd 项目页面

  • 官方 GNU 项目。
  • 冗长的 API,对我来说似乎很尴尬,尽管比编写 nginx 模块简单得多。
  • 在 keep-alive 模式下性能良好(链接到下面的基准测试),但在没有 keep-alive 的情况下性能不太好。

libevent

libevent 项目页面

Libevent 库有内置的 Web 服务器,称为 evhttp。

  • 它是基于事件的,为此使用 libevent。
  • 简单的API。自动构造 HTTP 标头。
  • 正式单线程。这是主要缺点。我发现一个 hack,它使 evhttp 的多个实例同时运行,接受来自同一个实例的连接插座。不确定它是否安全可靠。
  • 单线程evhttp的性能出奇的差。多线程 hack 效果更好,但仍然不好。

G-WAN

G-WAN 项目不是开源的,但我想简单说一下。

  • 性能非常好,内存使用率低,可执行文件只有 150 KB。
  • 非常方便的“servlet”部署:只需将.c文件复制到csp目录中,运行服务器就会自动编译它。代码修改也是即时编译的。
  • 简单的API。虽然在某些方面受到限制。丰富的功能(json、键值存储等)。
  • 不稳定。我的静态文件出现段错误。挂在一些示例脚本上。 (有干净安装经验。切勿混合不同版本的文件)。
  • 仅 32 位二进制(不再是)。

正如您所看到的,现有的替代方案都没有让我完全满意。于是我开发了自己的服务器,那就是...

NXWEB

NXWEB 项目页面

功能亮点:

  • 性能非常好;请参阅项目页面上的基准测试
  • 可以服务数以万计的并发请求
  • 小内存占用
  • 多线程模型旨在扩展
  • 异常轻的代码库
  • 简单的 API
  • 体面的 HTTP 协议处理
  • 保持活动连接
  • SSL 支持(通过 GNUTLS)
  • HTTP 代理(具有保持活动连接)池)
  • 非阻塞发送文件支持(具有可配置的小文件内存缓存;gzip 预编码文件服务)
  • 为开发人员提供的模块化设计
  • 可以作为守护进程运行;出错时重新启动
  • 开源

限制:

  • 依赖于 libev 库(不再是)
  • 仅在 Linux 上测试

I had the very same requirements for my job, so I evaluated a number of solutions: mongoose, libmicrohttpd, libevent. And I also was thinking about writing nginx modules. Here is the summary of my findings:

nginx

nginx project page

I love this server and use it a lot. Its performance and resource usage is much better than that of Apache, which I also still use but plan migrating to nginx.

  • Very good tunable performance. Rich functionality. Portability.
  • Module API is not documented and seems to be very verbose. See this nginx hello world module as example.
  • Nginx does not use threads but uses multiple processes. This makes writing modules harder, need to learn nginx API for shared memory, etc.

mongoose

mongoose project page

  • All server's code is in single mongoose.c file (about 130K), no dependencies. This is good.
  • One thread per connection, so if you need concurrency you've got to configure lots of threads, ie. high RAM usage. Not too good.
  • Performance is good, although not exceptional.
  • API is simple but you have to compose all response HTTP headers yourself, ie. learn HTTP protocol in detail.

libmicrohttpd

libmicrohttpd project page

  • Official GNU project.
  • Verbose API, seems awkward to me, although much more simple than writing nginx modules.
  • Good performance in keep-alive mode (link to my benchmarks below), not so good without keep-alive.

libevent

libevent project page

Libevent library has built-in web server called evhttp.

  • It is event based, uses libevent for that.
  • Easy API. Constructs HTTP headers automatically.
  • Officially single-threaded. This is major disadvantage. I've found a hack, which makes several instances of evhttp run simultaneously accepting connections from the same socket. Not sure if it is all safe and robust.
  • Performance of single-threaded evhttp is surprisingly poor. Multi-threaded hack works better, but still not good.

G-WAN

G-WAN project is not open source, but I'd like to say a few words about it.

  • Very good performance, low memory usage, 150 KB executable.
  • Very convenient 'servlet' deployment: just copy .c file into csp directory, and running server automatically compiles it. Code modifications also compiled on the fly.
  • Simple API. Although constrained in some ways. Rich functionality (json, key-value store, etc.).
  • Unstable. I had segfaults on static files. Hangs on some sample scripts. (Experienced on clean install. Never mixed files of different versions).
  • Only 32-bit binary (not anymore).

So as you can see, none of existing alternatives have fully satisfied me. So I have developed my own server, which is ...

NXWEB

NXWEB project page

Feature highlights:

  • Very good performance; see benchmarks on project page
  • Can serve tens of thousands concurrent requests
  • Small memory footprint
  • Multi-threaded model designed to scale
  • Exceptionally light code base
  • Simple API
  • Decent HTTP protocol handling
  • Keep-alive connections
  • SSL support (via GNUTLS)
  • HTTP proxy (with keep-alive connection pooling)
  • Non-blocking sendfile support (with configurable small file memory cache; gzip pre-encoded file serving)
  • Modular design for developers
  • Can be run as daemon; relaunches itself on error
  • Open source

Limitations:

  • Depends on libev library (not anymore)
  • Only tested on Linux
默嘫て 2024-11-23 07:58:15

我建议编写一个可以与许多高性能 Web 服务器(甚至闭源服务器)一起使用的 FastCGI 可执行文件。

I would suggest to write a FastCGI executable that can be used with many high performance web servers (even closed source ones).

小女人ら 2024-11-23 07:58:15

猫鼬:一个文件。简单易用。不是 asycn io,但非常适合嵌入式和特殊用途。

关。出色的。没有崩溃。超精心规划的配置。非常智能且易于 C/C++ 开发,换句话说,与 nginx 相比,它的 api 非常干净、合理。每个核心提供一个线程。或您指定的任何内容。一个很好的选择。最大的缺点(也许我在这方面缺乏):无法单步执行代码。

libevent:单线程在单核机器上并不是劣势。毕竟它的重点是异步 I/O。其他核心确实有多线程。

nginx:没有个人经验。在不完整的服务器上取得了重大进展。 (非常令人困惑的 api)

boost asio:asynchio (asio) 的 C++ 库。惊人的。对于像我这样的傻瓜来说,需要一个友好的高级 API。以及来自 php、java、javascript、node.js 和其他 Web 语言的其他人。

python Bottle:很棒的 1 文件库(框架/系统),可以轻松构建 python Web 应用程序。有/是一个内置的 httpd 服务器,如 libevent 和 node.js

node.js:javascript 异步服务器。一个很好的选择。不幸的是,必须使用 javascript 进行编程,这确实变得乏味。虽然为了完成工作还有一些话要说;还有一些话要说,让自己在这个过程中享受乐趣。希望没有人提出node.php

mongoose: one file. simple and easy to use. not an asycn io but perfect for embedded and particular purposes.

gwan. excellent. no crashes. ultra well planned configuration. very smart and easy for c/c++ development in other words, very clean sensible api compared to nginx. provides a thread per core. or whatever you specify. a great choice. largest disadvantage (maybe im lacking in this area): cannot step thru code.

libevent: single thread is not a disadvantage on a single core machine. afterall its point is an async i/o. does have multithreads for other cores.

nginx: no personal experience. gaining serious ground on a-patchy server. (terribly confusing api)

boost asio: a c++ library for asynchio (asio). awesome. needs a friendly higher-level api for simpletons like myself. and others who come from php, java, javascript, node.js and other web languages.

python bottle: awesome 1 file lib (framework/system) that makes it easy to build python web apps. has/is a built in httpd server, like libevent and node.js

node.js: javascript asyncio server. an excellent selection. unfortunately, have to program in javascript that does become tedious. while there is something to be said for getting the job done; there is also something to be said for enjoying yourself during the process. hopefully no ones comes up with node.php

扭转时空 2024-11-23 07:58:15

我将提出与 Axel Gneiting 相同的建议 - 但已经提供了我采用这种方法的原因的答案:

1)HTTP 作为协议并不简单 - 编写自己的服务器或修改现成的解决方案是一项非常复杂的任务 - 比使用可用的 API 来实现单独的处理引擎复杂得多

2)使用(未经修改的)主流网络服务器应该为您提供比您需要的更多的功能(因此您有成长的空间)。

3)使用(未经修改的)主流网络服务器通常意味着它比自制系统经过了更广泛的测试和记录。

4) ..并且它更有可能是安全和稳定的。

5) 使用 fastCGI,您可以使用各种语言来开发后端处理 - 包括 C++ 和 C。有 标准工具包可用于促进这一点。

6) 另外,许多网络服务器提供对进程内运行解释器引擎的支持(例如 mod_php、mod_perl)。不过,我建议不要将自己的本机代码作为模块运行。

它不能是基于文件的。

呃?这意味着什么?

I'm going to suggest the same thing as Axel Gneiting - but have provided an answer with my reasons for taking this approach:

1) HTTP is not trivial as a protocol - writing your own server or amending an off-the-shelf solution is a very complex task - a lot more complex than using the available APIs for implementing a separate processing engine

2) Using (an unmodified) mainstream webserver should provide you with more functionality than you require (so you've got growing room).

3) Using (an unmodified) mainstream webserver will usually mean that it has been far more extensively tested and documented than a homebrew system.

4) .. and its more likely to be secure and stable.

5) Using fastCGI you can use all sorts of languages to develop your back-end processing in - including C++ and C. There are standard toolkits available to facilitate this.

6) alternatively many webservers provide support for running interpreter engines in-process (e.g. mod_php, mod_perl). I'd advise against running your own native code as a module though.

It cannot be file-based.

Eh? What does that mean?

初心未许 2024-11-23 07:58:15

我是一个狂热的 nginx 用户; nginx是用C语言编写的; nginx 似乎可以为你工作。如果你想要 nginx 的最佳速度,我会制作一个 nginx 模块。以下是 第 3 方模块,您可以检查它们以了解它需要什么。

至于长轮询要求,您可能想看看 http 推送模块。

I'm an avid nginx user; nginx is written in C; nginx seems like it could work for you. If you want the very best speed out of nginx, I would make a nginx module. Here are 3rd party modules which you can examine to get an idea of what it requires.

As for the long polling requirement, you might want to have a look at the http push modules.

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