benjist
benjist

Reputation: 2881

Writing a REST Service in C++ with Nginx

I'm a bit underwhelmed by the Nginx module documentation. I've a lot of C++ code, and a REST Service already running using Boost Beast, and I'd like to compare performance between Beast and NGINX using the C++ module interface against a Benchmark I'll write accordingly to my needs.

I've seen this tutorial here: https://www.evanmiller.org/nginx-modules-guide.html

But I've thus far not seen a concise, short example to just get started.

Is there a hidden documentation? Alternatively, do you have an example showing how to use Nginx as a REST service in C(++)?

Upvotes: 3

Views: 3111

Answers (1)

Maxim Sagaydachny
Maxim Sagaydachny

Reputation: 2218

Short answer: Do not embed any application code into nginx.

Long answer: You can make new nginx module to help nginx to do its work better, for example:

  • add some new method of authentication
  • or some new transport to back-end, like shared memory.

Nginx was designed to serve static content, proxy requests and do some filtering like modifying headers. Main objective of nginx - do these things as fast as possible and spend as less resources as possible. It allows your application server to scale dynamically without affecting currently connected users.

Nginx is good web server but was never designed to become application server.

It does not makes much sense to embed application logic into nginx just because it is built with C language.

If you need to have the best of both worlds (proxy, static files and rest server) then just use them both (nginx and Beast) with each having its own responsibility. Nginx will take care of balancing, encryption and any other non-application specific function and app server will do its work.

Nginx's architecture is based on non-blocking network/file calls and all connections are served in a single thread and Nginx do it well because it just shuffles data back and forth.

If the code of your application can generate content fast and without blocking calls to external services then you could embed your app into nginx with consequences of loosing scalability. And if some part of your app requires CPU bound work or blocking calls then you need to move such things off main networking loop and it complicates things "a bit".

By embedding your logic into nginx you could probably save some microseconds and file handles on communications. For multi-user websocket app like chat or stock feed (i.e. app with long-term open connections) it could liberate extra resources but for the REST app with fast responses it would not make any gain.

Your REST app most likely uses SSL encryption. This encryption adds much more microseconds(milliseconds) to your response time compared to what you could gain by such implementation.

My advice: Leave nginx to do its things and do not interfere with it

Upvotes: 2

Related Questions