高性能应用程序webserver在C/ c++。

时间:2022-09-01 19:21:10

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

在C或c++中,是否有高性能(理想情况下是开放源码的)web服务器?

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.

我希望能够使用它,它在我的应用程序中调用一个方法/函数,并填写HTTP请求类/struct,然后返回一个填充的HTTP响应类/结构。

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++.

如果您不知道是否有这样的服务器可用,您是否建议编写我自己的web服务器来适应这个任务?它不能以文件为基础,必须用高性能的C/ c++编写。


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

编辑:如果这有帮助的话,我想要像Ruby Mongrel这样的东西。

5 个解决方案

#1


61  

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:

我对我的工作有同样的要求,所以我评估了一些解决方案:猫鼬、libmicrohttpd、libevent。我也在考虑写nginx模块。以下是我的调查结果摘要:

nginx

nginx project page

nginx项目页面

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.

我很喜欢这个服务器,并且经常使用它。它的性能和资源使用比Apache要好得多,我还在使用它,但是计划迁移到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.
  • 模块API没有文档化,看起来非常冗长。以nginx hello world模块为例。
  • Nginx does not use threads but uses multiple processes. This makes writing modules harder, need to learn nginx API for shared memory, etc.
  • Nginx不使用线程,而是使用多个进程。这使得编写模块更加困难,需要学习nginx API共享内存等等。

mongoose

mongoose project page

猫鼬项目页面

  • All server's code is in single mongoose.c file (about 130K), no dependencies. This is good.
  • 所有服务器的代码都在单个mongoose中。c文件(大约130K),没有依赖关系。这是很好的。
  • 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.
  • API很简单,但是您必须自己编写所有的响应HTTP头部。详细学习HTTP协议。

libmicrohttpd

libmicrohttpd project page

libmicrohttpd项目页面

  • Official GNU project.
  • 官方的GNU项目。
  • Verbose API, seems awkward to me, although much more simple than writing nginx modules.
  • 冗长的API对我来说似乎很笨拙,尽管比编写nginx模块要简单得多。
  • Good performance in keep-alive mode (link to my benchmarks below), not so good without keep-alive.
  • 在保持模式下的良好表现(链接到我的基准测试),如果没有keep-alive就不太好了。

libevent

libevent project page

libevent项目页面

Libevent library has built-in web server called evhttp.

Libevent库有一个名为evhttp的内置web服务器。

  • It is event based, uses libevent for that.
  • 它是基于事件的,使用libevent。
  • Easy API. Constructs HTTP headers automatically.
  • 简单的API。自动构建HTTP头。
  • 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.
  • 正式单线程。这是主要缺点。我发现了一个hack,它使evhttp运行的几个实例同时接受来自同一个套接字的连接。不确定它是否安全可靠。
  • Performance of single-threaded evhttp is surprisingly poor. Multi-threaded hack works better, but still not good.
  • 单线程evhttp的性能非常差。多线程破解效果更好,但还是不好。

G-WAN

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

G-WAN项目不是开源的,但我想说几句。

  • Very good performance, low memory usage, 150 KB executable.
  • 性能非常好,内存使用率低,可执行150 KB。
  • Very convenient 'servlet' deployment: just copy .c file into csp directory, and running server automatically compiles it. Code modifications also compiled on the fly.
  • 非常方便的“servlet”部署:将.c文件复制到csp目录中,运行服务器自动编译它。代码修改也在飞行中进行编译。
  • Simple API. Although constrained in some ways. Rich functionality (json, key-value store, etc.).
  • 简单的API。尽管在某些方面受到限制。丰富的功能(json、键值存储等)。
  • 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).
  • 只有32位二进制(不再)。

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

NXWEB项目页面

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
  • 简单的API
  • Decent HTTP protocol handling
  • 体面的HTTP协议处理
  • Keep-alive connections
  • 点火电极连接
  • SSL support (via GNUTLS)
  • SSL支持(通过GNUTLS)
  • HTTP proxy (with keep-alive connection pooling)
  • HTTP代理(带有keepalive连接池)
  • Non-blocking sendfile support (with configurable small file memory cache; gzip pre-encoded file serving)
  • 非阻塞sendfile支持(具有可配置的小文件内存缓存;gzip pre-encoded文件服务)
  • Modular design for developers
  • 模块化设计为开发人员
  • Can be run as daemon; relaunches itself on error
  • 可以作为守护进程运行;改版自己错误
  • Open source
  • 开源

Limitations:

限制:

  • Depends on libev library (not anymore)
  • 取决于libev库(不再是)
  • Only tested on Linux
  • 只测试了在Linux上

#2


7  

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

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

#3


4  

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

猫鼬:一个文件。简单易用。不是asycn io,而是嵌入式和特定用途的完美。

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.

gwan。太好了。没有崩溃。超好计划配置。与nginx相比,c/c++开发非常智能,也很容易开发。提供每个核心的线程。或任何你指定。一个伟大的选择。最大的缺点(可能我在这方面缺乏):不能通过代码。

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.

libevent:单线程并不是单一核心机器上的缺点。毕竟它的重点是一个异步i/o。对其他核心有多线程。

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

nginx:没有个人经验。在不完善的服务器上取得了很好的进展。(极度困惑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.

boost asio:一个用于asynchio (asio)的c++库。太棒了。需要一个类似于我这样的simpletons的高级api。还有来自php, java, javascript, node的其他人。js和其他网络语言。

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

python瓶:awesome 1文件lib(框架/系统),这使得构建python web应用程序变得很容易。是否在httpd服务器中构建,比如libevent和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

节点。js:javascript asyncio服务器。一个很好的选择。不幸的是,必须在javascript中编程,这会变得单调乏味。虽然完成任务有一些需要说的;在这个过程中,你也可以享受一些乐趣。但愿没人想到node.php。

#4


4  

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

我将提出和Axel Gneiting一样的建议——但我给出了一个答案,我的理由是:

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

HTTP不是简单的协议——编写自己的服务器或修改现成的解决方案是一项非常复杂的任务——比使用可用的api实现单独的处理引擎要复杂得多。

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

2)使用(未修改的)主流webserver应该为你提供比你所需要的更多的功能(所以你有了成长的空间)。

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

3)使用(未修改的)主流webserver通常意味着它比自制系统更广泛地测试和记录。

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

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

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.

5)使用fastCGI可以使用各种语言来开发后端处理,包括c++和C。

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.

6)或者许多web服务器支持运行解释器引擎(例如mod_php、mod_perl)。不过,我建议不要将自己的本机代码作为模块运行。

It cannot be file-based.

它不能是基于文件的。

Eh? What does that mean?

是吗?这是什么意思?

#5


2  

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.

我是一个*的nginx用户;nginx是用C写的;nginx似乎可以为你服务。如果你想让nginx最好的速度,我会做一个nginx模块。这里是你可以检查的第三方模块,以了解它需要什么。

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

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

#1


61  

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:

我对我的工作有同样的要求,所以我评估了一些解决方案:猫鼬、libmicrohttpd、libevent。我也在考虑写nginx模块。以下是我的调查结果摘要:

nginx

nginx project page

nginx项目页面

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.

我很喜欢这个服务器,并且经常使用它。它的性能和资源使用比Apache要好得多,我还在使用它,但是计划迁移到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.
  • 模块API没有文档化,看起来非常冗长。以nginx hello world模块为例。
  • Nginx does not use threads but uses multiple processes. This makes writing modules harder, need to learn nginx API for shared memory, etc.
  • Nginx不使用线程,而是使用多个进程。这使得编写模块更加困难,需要学习nginx API共享内存等等。

mongoose

mongoose project page

猫鼬项目页面

  • All server's code is in single mongoose.c file (about 130K), no dependencies. This is good.
  • 所有服务器的代码都在单个mongoose中。c文件(大约130K),没有依赖关系。这是很好的。
  • 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.
  • API很简单,但是您必须自己编写所有的响应HTTP头部。详细学习HTTP协议。

libmicrohttpd

libmicrohttpd project page

libmicrohttpd项目页面

  • Official GNU project.
  • 官方的GNU项目。
  • Verbose API, seems awkward to me, although much more simple than writing nginx modules.
  • 冗长的API对我来说似乎很笨拙,尽管比编写nginx模块要简单得多。
  • Good performance in keep-alive mode (link to my benchmarks below), not so good without keep-alive.
  • 在保持模式下的良好表现(链接到我的基准测试),如果没有keep-alive就不太好了。

libevent

libevent project page

libevent项目页面

Libevent library has built-in web server called evhttp.

Libevent库有一个名为evhttp的内置web服务器。

  • It is event based, uses libevent for that.
  • 它是基于事件的,使用libevent。
  • Easy API. Constructs HTTP headers automatically.
  • 简单的API。自动构建HTTP头。
  • 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.
  • 正式单线程。这是主要缺点。我发现了一个hack,它使evhttp运行的几个实例同时接受来自同一个套接字的连接。不确定它是否安全可靠。
  • Performance of single-threaded evhttp is surprisingly poor. Multi-threaded hack works better, but still not good.
  • 单线程evhttp的性能非常差。多线程破解效果更好,但还是不好。

G-WAN

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

G-WAN项目不是开源的,但我想说几句。

  • Very good performance, low memory usage, 150 KB executable.
  • 性能非常好,内存使用率低,可执行150 KB。
  • Very convenient 'servlet' deployment: just copy .c file into csp directory, and running server automatically compiles it. Code modifications also compiled on the fly.
  • 非常方便的“servlet”部署:将.c文件复制到csp目录中,运行服务器自动编译它。代码修改也在飞行中进行编译。
  • Simple API. Although constrained in some ways. Rich functionality (json, key-value store, etc.).
  • 简单的API。尽管在某些方面受到限制。丰富的功能(json、键值存储等)。
  • 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).
  • 只有32位二进制(不再)。

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

NXWEB项目页面

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
  • 简单的API
  • Decent HTTP protocol handling
  • 体面的HTTP协议处理
  • Keep-alive connections
  • 点火电极连接
  • SSL support (via GNUTLS)
  • SSL支持(通过GNUTLS)
  • HTTP proxy (with keep-alive connection pooling)
  • HTTP代理(带有keepalive连接池)
  • Non-blocking sendfile support (with configurable small file memory cache; gzip pre-encoded file serving)
  • 非阻塞sendfile支持(具有可配置的小文件内存缓存;gzip pre-encoded文件服务)
  • Modular design for developers
  • 模块化设计为开发人员
  • Can be run as daemon; relaunches itself on error
  • 可以作为守护进程运行;改版自己错误
  • Open source
  • 开源

Limitations:

限制:

  • Depends on libev library (not anymore)
  • 取决于libev库(不再是)
  • Only tested on Linux
  • 只测试了在Linux上

#2


7  

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

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

#3


4  

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

猫鼬:一个文件。简单易用。不是asycn io,而是嵌入式和特定用途的完美。

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.

gwan。太好了。没有崩溃。超好计划配置。与nginx相比,c/c++开发非常智能,也很容易开发。提供每个核心的线程。或任何你指定。一个伟大的选择。最大的缺点(可能我在这方面缺乏):不能通过代码。

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.

libevent:单线程并不是单一核心机器上的缺点。毕竟它的重点是一个异步i/o。对其他核心有多线程。

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

nginx:没有个人经验。在不完善的服务器上取得了很好的进展。(极度困惑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.

boost asio:一个用于asynchio (asio)的c++库。太棒了。需要一个类似于我这样的simpletons的高级api。还有来自php, java, javascript, node的其他人。js和其他网络语言。

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

python瓶:awesome 1文件lib(框架/系统),这使得构建python web应用程序变得很容易。是否在httpd服务器中构建,比如libevent和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

节点。js:javascript asyncio服务器。一个很好的选择。不幸的是,必须在javascript中编程,这会变得单调乏味。虽然完成任务有一些需要说的;在这个过程中,你也可以享受一些乐趣。但愿没人想到node.php。

#4


4  

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

我将提出和Axel Gneiting一样的建议——但我给出了一个答案,我的理由是:

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

HTTP不是简单的协议——编写自己的服务器或修改现成的解决方案是一项非常复杂的任务——比使用可用的api实现单独的处理引擎要复杂得多。

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

2)使用(未修改的)主流webserver应该为你提供比你所需要的更多的功能(所以你有了成长的空间)。

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

3)使用(未修改的)主流webserver通常意味着它比自制系统更广泛地测试和记录。

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

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

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.

5)使用fastCGI可以使用各种语言来开发后端处理,包括c++和C。

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.

6)或者许多web服务器支持运行解释器引擎(例如mod_php、mod_perl)。不过,我建议不要将自己的本机代码作为模块运行。

It cannot be file-based.

它不能是基于文件的。

Eh? What does that mean?

是吗?这是什么意思?

#5


2  

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.

我是一个*的nginx用户;nginx是用C写的;nginx似乎可以为你服务。如果你想让nginx最好的速度,我会做一个nginx模块。这里是你可以检查的第三方模块,以了解它需要什么。

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

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