nginx is an excellent representative of multi-process web server.
This article is to be used. mongols -1.2.3 implements a multi-process web server faster than nginx.
mongols is a C++ server infrastructure library. Its main features are as follows:
- tcp server
- http server
- websocket server
- web server
- leveldb server
- lua server
- sqlite server
- medis server
All the above servers are implemented by epoll mechanism, and support multi-threading and multi-process.
mongols does not depend on any event library, but its concurrency performance is better than the famous libevent, libev and libuv.
Moreover, it provides a very friendly development interface, which makes it easy for any developer who tries to develop a high-performance network server based on tcp, resp or http protocol.
Download: https://github.com/webcpp/mongols
First look at the comparison of pressure measurements:
Look at the code again:
1 #include <unistd.h> 2 #include <sys/wait.h> 3 #include <sys/signal.h> 4 #include <mongols/util.hpp> 5 #include <mongols/web_server.hpp> 6 #include <iostream> 7 8 9 static void signal_cb(int sig, siginfo_t *, void *); 10 static std::vector<pid_t> pids; 11 12 int main(int, char**) { 13 // daemon(1, 0); 14 auto f = [](const mongols::request & req) { 15 if (req.method == "GET" && req.uri.find("..") == std::string::npos) { 16 return true; 17 } 18 return false; 19 }; 20 int port = 9090; 21 const char* host = "127.0.0.1"; 22 mongols::web_server 23 server(host, port, 5000, 512000, 0/*must be 0*/); 24 server.set_root_path("html"); 25 server.set_mime_type_file("html/mime.conf"); 26 server.set_list_directory(true); 27 server.set_enable_mmap(false); 28 29 30 int child_process_len = std::thread::hardware_concurrency(); 31 mongols::forker(child_process_len 32 , [&]() { 33 server.run(f); 34 } 35 , pids); 36 for (int i = 0; i < child_process_len; ++i) { 37 mongols::process_bind_cpu(pids[i], i); 38 } 39 40 const int sig_len = 4; 41 int sigs[sig_len] = ; 42 struct sigaction act; 43 memset(&act, 0, sizeof (struct sigaction)); 44 sigemptyset(&act.sa_mask); 45 act.sa_sigaction = signal_cb; 46 47 for (int i = 0; i < sig_len; ++i) { 48 if (sigaction(sigs[i], &act, NULL) < 0) { 49 perror("sigaction error"); 50 return -1; 51 } 52 } 53 54 55 56 for (size_t i=0;i<pids.size();++i) { 57 pid_t pid; 58 if ((pid = wait(NULL)) > 0) { 59 60 } 61 } 62 63 } 64 65 static void signal_cb(int sig, siginfo_t *, void * ) { 66 switch (sig) { 67 case SIGTERM: 68 case SIGHUP: 69 case SIGQUIT: 70 case SIGINT: 71 for (auto & i : pids) { 72 kill(i, SIGTERM); 73 } 74 break; 75 default:break; 76 } 77 }
Conclusion: nginx is not the fastest web server.