Skip to content

nginx microservices with dynamic linked application

Taymindis Woon edited this page Dec 13, 2018 · 15 revisions

nginx-link-function has provided the following functions to achieve the communication with other application service. These functions are built in on top of nginx, it is purely based on nginx.

this feature is only support for nginx version more than 1.13.3
extern u_char* ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char*key);
extern void* ngx_link_func_get_query_param(ngx_link_func_ctx_t *ctx, const char *key);
extern int ngx_link_func_add_header_in(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen, const char *value, size_t val_len );

For every nginx-link-function linked application, you may apply the functions above to achieve the inter communication between the services. For example authenthication, you may get the userId,token, and etc from headers.

void my_heavy_processing(ngx_link_func_ctx_t *ctx) {
    char *userReceipt= (char*) ngx_link_func_get_header(ctx, "userReceipt");
    if(userReceipt) {
       processHeavy(.....);       
    }
    ngx_link_func_add_header_in(ctx, "userSuccessStatus", sizeof("userSuccessStatus") -1 ,  "true", sizeof("true") -1);
    // For every subrequest, you should have response code to let nginx understand it did returned.
    ngx_link_func_write_resp(
        ctx,
        202,
        NULL,
        ngx_link_func_content_type_plaintext,
        "Accepted",
        sizeof("Accepted") - 1
    );
}

void my_processing(ngx_link_func_ctx_t *ctx) {
    char *userRequestId= (char*) ngx_link_func_get_query_param(ctx, "userRequestId");
    if(userRequestId) {
       processSimple(.....);       
    }
    ngx_link_func_add_header_in(ctx, "userReceipt", sizeof("userReceipt") -1 ,  "ABC001", sizeof("ABC001") -1);

    // For every subrequest, you should have response code to let nginx understand it did returned.
    ngx_link_func_write_resp(
        ctx,
        202,
        NULL,
        ngx_link_func_content_type_plaintext,
        "Accepted",
        sizeof("Accepted") - 1
    );
}
void afterDoneVerification(ngx_link_func_ctx_t *ctx) {
    processVerify(....);
    if(error) {
      ngx_link_func_add_header_in(ctx, "verificationFailed", sizeof("verificationFailed") -1 ,  "true", sizeof("true") -1);
    }

}

From nginx config, you can setup many way to routing your service to another. For example based on the code login above

ngx_link_func_lib "/home/taymindis/github/nginx-link-function/t/libcfuntest.so";
location /my_heavy_processing{
    ngx_link_func_call "my_heavy_processing";
}
location = /my_processing {
    internal;
    ngx_link_func_call "my_processing";
}
location = /my_simple_authentication {
  ngx_link_func_subrequest /my_processing check_status incl_body incl_args; # ngx link function subrequest with sync/async way based on the config 

  ngx_link_func_call "afterDoneVerification"; # if proxy_pass is given, the response will be ignored, it will route to next phase which is proxy pass, if proxy pass not given, it should have response content

  proxy_pass http://127.0.0.1:${server_port}/backendProcess?userSuccessStatus=$http_userSuccessStatus;
}

There are a lot of directive which able to support routing service, such as:

ngx_link_func_subrequest

proxy directive

mirror directirve

auth_request

addition directive

to be continue