Giter VIP home page Giter VIP logo

taymindis / nginx-link-function Goto Github PK

View Code? Open in Web Editor NEW
228.0 24.0 40.0 439 KB

It is a NGINX module that provides dynamic linking to your application in server context and call the function of your application in location directive

Home Page: https://nginx-link-function.github.io

License: BSD 2-Clause "Simplified" License

C 94.79% Perl 5.21%
nginx nginx-cfunction ngx-http embedded golang zero-downtime dynamic-link-library dynamic-linking nginx-server test-nginx

nginx-link-function's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nginx-link-function's Issues

Logging?

Your example works great and I think that I'll be able to build off of that. One question is that you give some logging examples:

ngx_http_c_func_log(info, ctx, "%s", "Starting The Application");
but it does not seem to log anywhere. Is there something special that must be done?

`ngx_link_func_init_cycle()` not found on FreeBSD

The following basic example is not accepted by the nginx-link-function module on FreeBSD 12 with nginx 1.18 :

#include <stdio.h>
#include <ngx_link_func_module.h>

int is_service_on = 0;

void ngx_link_func_init_cycle(ngx_link_func_cycle_t* cycle) {
    ngx_link_func_cyc_log(info, cycle, "%s", "starting application");

    is_service_on=1;
}

void greeting(ngx_link_func_ctx_t *ctx) {
    ngx_link_func_log_info(ctx, "Calling back and log from my_app_simple_get");

    ngx_link_func_write_resp(
        ctx,
        200,
        "200 OK",
        "text/plain",
        "greeting from ngx_link_func testing",
        sizeof("greeting from ngx_link_func testing")-1
    );
}

void ngx_link_func_exit_cycle(ngx_link_func_cycle_t* cyc) {
    ngx_link_func_cyc_log(info, cyc, "%s\n", "Shutting down/reloading the Application");

    is_service_on = 0;
}

Compilation:

$ gcc -shared -fPIC -o hello.so hello.c

The shared library is then loaded with:

    server {
        listen       80;
        server_name  localhost;
        ngx_link_func_lib "/usr/local/etc/nginx/hello.so";

        location = /greeting {
            ngx_link_func_call "greeting";
        } 
    }

Starting nginx fails with:

# service nginx start
nginx: [error] function ngx_link_func_init_cycle(ngx_link_func_cycle_t *cycle) not found in "/usr/local/etc/nginx/hello.so", at least create an empty init function block
 Undefined symbol "_nss_cache_cycle_prevention_function" in /usr/local/etc/nginx/nginx.conf:126

But the shared library contains the wanted function:

# nm -D hello.so
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w __cxa_finalize
00000000000007f4 T _fini
0000000000000510 T _init
00000000000006f7 T greeting
0000000000200bb8 B is_service_on
                 U ngx_link_func_cyc_log_info
0000000000000745 T ngx_link_func_exit_cycle
0000000000000695 T ngx_link_func_init_cycle
                 U ngx_link_func_log_info
                 U ngx_link_func_write_resp
                 U snprintf

Facing client request blocking issue?

Some users are having issue when proceed some heavy request in their apps layer. Due to old nginx-c-function has no threads feature, it only accept 1 request per time, it may causing slowness.

unknown directive "ngx_link_func_lib"

The following error occurs starting the FreeBSD port of nginx 1.18.0_15,2:

# service nginx onestart
Performing sanity check on nginx configuration:
nginx: [emerg] unknown directive "ngx_link_func_lib" in /usr/local/etc/nginx/nginx.conf:42
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
Starting nginx.
nginx: [emerg] unknown directive "ngx_link_func_lib" in /usr/local/etc/nginx/nginx.conf:42
/usr/local/etc/rc.d/nginx: WARNING: failed to start nginx

nginx has been compiled with the LINK option enabled to include the nginx-link-function module:

# pkg info www/nginx
nginx-1.18.0_15,2            
Name           : nginx      
Version        : 1.18.0_15,2
Installed on   : Wed Jun 17 14:40:05 2020 CEST
Origin         : www/nginx  
Architecture   : FreeBSD:12:amd64
Prefix         : /usr/local     
Categories     : www                                                                                                                           
Licenses       : BSD2CLAUSE      
Maintainer     : [email protected] 
WWW            : https://nginx.org/ 
Comment        : Robust and small WWW server
Options        :
[...]
        LINK           : on
[...]

The build log shows that the nginx-link-function module is indeed enabled.

To reproduce the error on FreeBSD, install nginx from ports and enable LINK:

# cd /usr/local/ports/www/nginx/
# make config
# make
# make install

nginx: [emerg] unknown directive "ngx_http_c_func_link_lib" in /home/aimso/nginx-c-function/t/servroot/conf/nginx.conf

t/sanity.t .. nginx: [emerg] unknown directive "ngx_http_c_func_link_lib" in /home/aimso/nginx-c-function/t/servroot/conf/nginx.conf:39
Bailout called. Further testing stopped: TEST 6: Set C_FUNC_TEST_GET_CALLOC_FROM_POOL - Cannot start nginx using command "nginx -p /home/aimso/nginx-c-function/t/servroot/ -c /home/aimso/nginx-c-function/t/servroot/conf/nginx.conf > /dev/null" (status code 256).
FAILED--Further testing stopped: TEST 6: Set C_FUNC_TEST_GET_CALLOC_FROM_POOL - Cannot start nginx using command "nginx -p /home/aimso/nginx-c-function/t/servroot/ -c /home/aimso/nginx-c-function/t/servroot/conf/nginx.conf > /dev/null" (status code 256).

How to get the value of a config file param during cycle init?

Hello,
I looked through the wiki, and had a quick look in the nginx source, but can't quite understand how to get the value of a config file parameter?

location /foo {
  root /custom/path/to/assets;
  ngx_link_func_call "foo";
}

How would I get the value of root during ngx_link_func_init_cycle?

Nginx gzip filter compatibility issue

Thank you very much for making this project. I am definitely going to use it for my C++ project to integrate it with Nginx since I found it to be an excellent solution compared to all other things I considered. I found your API functions and documentation excellent and everything worked very quickly and easily.

I did however find a bug in the current version of Nginx 1.21 and this project's master branch which you may want to review.

Basically gzip doesn't work for the urls using this module and I isolated how to fix this.

In ngx_link_func_module.c, there are these 2 lines.
r->headers_out.content_type.len = resp_content_type->len;
r->headers_out.content_type.data = resp_content_type->data;

For whatever reason, ngx_http_code_module.c is checking an addition length variable, which also exists in the nginx documentation, so there is some kind of reason they have this length variable needing to be set twice. This causes this logic to evaluate as true in the gzip module which disables it: ngx_http_test_content_type(r, &conf->types) == NULL

When I add this line next to those, gzip is able to work!

r->headers_out.content_type_len = resp_content_type->len;

You can see this variable is documented here and they say it usually needs to be the same as content_type.len:
https://www.nginx.com/resources/wiki/extending/api/http/

In the process of debugging this, I learned a little bit more about how Nginx works and am less scared to try to work on it now especially since i was able to output debug messages and isolate things reasonably easy, though it did take some hunting to understand this one. It feels more like a bug in nginx to have 2 similar variable names.

Thanks again for creating a simple project that allowed me to dive into Nginx source and be productive on the first day.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.