
Nginx C function - ScottWRobinson
https://nginx-c-function.github.io
======
drenvuk
There is something that should be said about nginx's third party modules.
There are many and they add quite a bit of functionality, especially when
they're upstream modules.

[https://www.nginx.com/resources/wiki/modules/](https://www.nginx.com/resources/wiki/modules/)

It's great because you don't even need a web framework, really. If you're
comfortable and confident in nginx you can get quite a bit of performance out
of the server while adding your application logic to it. I'm not sure why more
people don't and just use it as a reverse proxy.

~~~
cagenut
because you could do all that and more with apache modules, the reason people
switched was because nginx _didnt_ get bogged down in all that.

imho its keyword engineering, but you can see given the history why nginx
adopters aren't thinking that way.

~~~
eddieroger
I don't know - I switched from Apache to NGINX because I preferred it's config
formatting over writing XML when I stopped using things like CPanel. Many of
the NGINX servers under my current role run the NGINX Lua module performing
logic on over 1B requests per day. I do try to reduce dependency on modules,
but that's not because it makes it too Apache-like, it's for simplicity's
sake. If you don't want NGINX to have these features, don't compile it in,
then boom. no longer bogged down.

------
fcambus
For a more mature implementation of this concept, I would highly recommend
having a look at Kore: [https://kore.io](https://kore.io)

It has a sane architecture, and is fully privilege separated by default, with
private keys isolated in a separate process. I've been using it lately to
write REST APIs in C, and the experience has been awesome, it has great APIs
to parse requests and easily construct responses.

~~~
setquk
Genuine question here but why would you want to write REST APIs in C?

I’ve been writing C for 25 years and I wouldn’t trust myself not to do
something stupid.

------
chx
Could someone explain to me why would I choose this over
[https://github.com/openresty/lua-nginx-
module](https://github.com/openresty/lua-nginx-module) ? It supports LuaJIT
and while I suppose it is possible to write faster C code by hand than JIT
translated Lua it'd be hard and I'd fear the maintainability of hand optimized
C code.

~~~
comex
For one thing, you don't have to use this module with C. Any language that
supports exporting C-compatible functions will do – there's C++ of course, but
also Rust, Go, D, Nim, and many many others.

~~~
shakna
LuaJIT also supports C-functions. It's FFI [0] will allow you to declare any
C-compatible function or data structure, and then use it in normal Lua code.

[0] [https://luajit.org/ext_ffi.html](https://luajit.org/ext_ffi.html)

~~~
guipsp
Why would you ever opt to go thru Lua in this case?

~~~
shakna
It's well supported, well maintained, and around for the foreseeable future.
(OpenResty)

The language implementation is fast, and allows you to tap into any
C-compatible library.

The question isn't why you would go through Lua, but why you would choose
something with less support?

------
writepub
We're huge fans of C interfaces on the sever side.
[https://kore.io](https://kore.io) is our preferred stack.

It's primary interface is C. In our opinion, it's lighter weight, and simpler
to integrate with your app.

------
noway421
Is the function blocking or asynchronous? Cant quite find it in the README.

~~~
drenvuk
It works with nginx threads using aio. It's in one of the issues there if you
look deep enough.

[https://github.com/Taymindis/nginx-c-
function/issues/2](https://github.com/Taymindis/nginx-c-function/issues/2)

I haven't figured out how to get rid of the drop in performance though. Lets
say you just return a 200 from the nginx location like so:

    
    
      location /a {
            return 200 "{\"result\": \"Hello World!\"}";
        }
    

vs

    
    
        location / {
            ngx_http_c_func_call 'getRoute';
        }
    
    

The requests per second will drop by about 40% of what you'd otherwise get.
This is with 4 workers, the worker_rlimit_nofile set to 262144 etc, etc.

I'm not sure what I'm doing wrong. It is pretty nice given that you also get
all of the other options like upstream handling and reverse proxying and
pretty damned good speed. The flexibility this adds is beyond awesome. But if
I only need max speed for an api that only talks to postgres, redis, and a few
other things, I'd just use H2o.

~~~
int0x80
what is the src of the C function?

~~~
drenvuk
Check this file out, it's in the example that's provided:
[https://github.com/Taymindis/sample-ngx-cfunc-
project/blob/m...](https://github.com/Taymindis/sample-ngx-cfunc-
project/blob/master/ngx-cfunc-ggl-direction/src/main/CfuncGglDirection.cpp)
I've stripped out everything of 'getRoute' and only provide a response of a
global variable 'thing' that never changes.

    
    
      std::string thing = "{\"result\": \"Hello World!\"}";
      void getRoute(ngx_http_c_func_ctx_t* ctx) {
        ngx_http_c_func_write_resp(
                    ctx,
                    200,
                    NULL,
                    ngx_http_c_func_content_type_json,
                    ::thing.c_str(),
                    ::thing.length()
        );
      }

------
annchin_peh
Can anyone explain to me what is the different between

ngx_http_c_func_call "my_app_simple_get_greeting";

vs

ngx_http_c_func_call "my_app_simple_get_greeting" respTo=myResponseVariable;

I didn't see any different, just create a variable.

~~~
annchin_peh
I've got the answer.
[https://www.reddit.com/r/programming/comments/9nsbt8/create_...](https://www.reddit.com/r/programming/comments/9nsbt8/create_your_desired_cc_application_on_top_of/e7sbj7n)

------
kyberias
cgi-bin all over again?

~~~
viraptor
Not really. Cgi-bin processes were executed from scratch. This is a single
function call from an dynamic lib.

------
andyhoang
Is this like lambda(aws) for C?

~~~
smadge
I suppose a procedure that responds to an http request is now called a lambda.

------
another-cuppa
Why does it keep mentioning .so? Nobody refers to it like that. Just say
dynamically link.

------
rntksi
Any idea why sharing this link to Pocket on Android 9 does not work? ("this
webpage does not contain a valid link")

