Get the most out of your Centmin Mod LEMP stack
Become a Member

Nginx Introduction to nginScript

Discussion in 'Nginx and PHP-FPM news & discussions' started by eva2000, Mar 25, 2017.

Thread Status:
Not open for further replies.
  1. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,110
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,645
    Local Time:
    7:56 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Nginx folks posted a new blog article on introduction to their own nginScript nginx module at nginscript: A custom JavaScript Implementation for NGINX & NGINX Plus.

    Centmin Mod nginScript Support



    Centmin Mod 123.09beta01 beta and newer releases, already have optional support for nginScript nginx module via 2 variables which are disabled by default. Centmin Mod 123.09beta01 added nginScript support back in September, 2015. To enable nginScript support, you set in persistent config file /etc/centminmod/custom_config.inc the following variables prior to recompiling Nginx via centmin.sh menu option 4.
    Code (Text):
    NGINX_NJS='y'
    NGXDYNAMIC_NJS='y'
    

    End result is nginScript dynamic nginx module integration into Centmin Mod Nginx server.
    dynamic modules include file /usr/local/nginx/conf/dynamic-modules.conf
    Code (Text):
    load_module "modules/ngx_http_image_filter_module.so";
    load_module "modules/ngx_http_fancyindex_module.so";
    load_module "modules/ngx_http_brotli_filter_module.so";
    load_module "modules/ngx_http_brotli_static_module.so";
    load_module "modules/ngx_stream_module.so";
    load_module "modules/ngx_http_js_module.so";
    load_module "modules/ngx_stream_js_module.so";
    

    dynamic module directory at /usr/local/nginx/modules
    Code (Text):
    -rwxr-xr-x   1 root root 1.6M Mar 25 08:54 ngx_http_brotli_filter_module.so
    -rwxr-xr-x   1 root root 1.6M Mar 24 16:18 ngx_http_brotli_filter_module.so.old
    -rwxr-xr-x   1 root root  32K Mar 25 08:54 ngx_http_brotli_static_module.so
    -rwxr-xr-x   1 root root  32K Mar 24 16:18 ngx_http_brotli_static_module.so.old
    -rwxr-xr-x   1 root root  55K Mar 25 08:54 ngx_http_fancyindex_module.so
    -rwxr-xr-x   1 root root  55K Mar 24 16:18 ngx_http_fancyindex_module.so.old
    -rwxr-xr-x   1 root root  59K Mar 25 08:54 ngx_http_image_filter_module.so
    -rwxr-xr-x   1 root root  59K Mar 24 16:18 ngx_http_image_filter_module.so.old
    -rwxr-xr-x   1 root root 625K Mar 25 08:54 ngx_http_js_module.so
    -rwxr-xr-x   1 root root 617K Mar 25 08:54 ngx_stream_js_module.so
    -rwxr-xr-x   1 root root 476K Mar 25 08:54 ngx_stream_module.so
    -rwxr-xr-x   1 root root 476K Mar 24 16:18 ngx_stream_module.so.old
    

    In above exampe, I also enabled Nginx Brotli dynamic module support via persistent config file set to
    Code (Text):
    NGINX_NJS='y'
    NGXDYNAMIC_NJS='y'
    NGXDYNAMIC_BROTLI=y
    NGINX_LIBBROTLI=y
    


     
    Last edited: Mar 25, 2017
  2. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,110
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,645
    Local Time:
    7:56 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Using Nginx blog provided example for tetsing https://community.centminmod.com/threads/introduction-to-nginscript.10926/#post-46865. I set in Nginx vhost with altered js_include path to header_logging.js at /usr/local/nginx/conf/nginscript/header_logging.js
    Code (Text):
    js_include /usr/local/nginx/conf/nginscript/header_logging.js;             # Load JavaScript code from here
    js_set     $access_log_with_headers kvAccessLog; # Fill variable from JS function
    log_format kvpairs $access_log_with_headers;     # Define special log format
    

    Create /usr/local/nginx/conf/nginscript directory
    Code (Text):
    mkdir -p /usr/local/nginx/conf/nginscript
    

    contents of /usr/local/nginx/conf/nginscript/header_logging.js
    Code (Text):
    function kvHeaders(headers, parent) {
        var kvpairs = "";
        for (var h in headers) {
            kvpairs += " " + parent + "." + h + "=";
            if ( headers[h].indexOf(" ") == -1 ) {
            kvpairs += headers[h];
            } else {
                kvpairs += "'" + headers[h] + "'";
            }
        }
        return kvpairs;
    }
    
    function kvAccessLog(req, res) {
        var log = req.variables.time_iso8601;  // nginScript can access all variables
        log += " client=" + req.remoteAddress; // Property of request object
        log += " method=" + req.method;        // "
        log += " uri=" + req.uri;              // "
        log += " status=" + res.status;        // Property of response object
        log += kvHeaders(req.headers, "req");  // Send request headers object to function
        log += kvHeaders(res.headers, "res");  // Send response headers object to function
        return log;
    }

    then altered the blog posted example nginx vhost to test from subdirectory at /nginscript instead where proxied host.example.com
    Code (Text):
    
    location /nginxscript {
      access_log /var/log/nginx/access_headers.log kvpairs;
      proxy_pass http://host.example.com;
    }
    

    Unfortunately, testing Nginx config fails without any clues as to why ?
    Code (Text):
    nginx -t
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
    

    loaded nginScript module as a dynamic module via include file /usr/local/nginx/conf/dynamic-modules.conf in nginx.conf
    Code (Text):
    cat /usr/local/nginx/conf/dynamic-modules.conf
    load_module "modules/ngx_http_image_filter_module.so";
    load_module "modules/ngx_http_fancyindex_module.so";
    load_module "modules/ngx_http_brotli_filter_module.so";
    load_module "modules/ngx_http_brotli_static_module.so";
    load_module "modules/ngx_stream_module.so";
    load_module "modules/ngx_http_js_module.so";
    load_module "modules/ngx_stream_js_module.so";
    

    nginx.conf excerpt
    Code (Text):
    user              nginx nginx;
    worker_processes 4;
    worker_priority -10;
    
    worker_rlimit_nofile 260000;
    timer_resolution 100ms;
    
    pcre_jit on;
    include /usr/local/nginx/conf/dynamic-modules.conf;
    
    
    pid         logs/nginx.pid;
    
    events {
        worker_connections  10000;
        accept_mutex off;
        accept_mutex_delay 200ms;
        use epoll;
        #multi_accept on;
    }
    
    
    http {
    
     
  3. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,110
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,645
    Local Time:
    7:56 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Use example from Module ngx_http_js_module

    create directory at /usr/local/nginx/conf/nginscript for js_include file /usr/local/nginx/conf/nginscript/http.js
    Code (Text):
    js_include /usr/local/nginx/conf/nginscript/http.js;
    js_set $foo     foo;
    js_set $summary summary;
    
        location /nginscript {
            add_header X-Foo $foo;
            js_content baz;
        }
    
        location /summary {
            return 200 $summary;
        }
    

    contents of /usr/local/nginx/conf/nginscript/http.js
    Code (Text):
    function foo(req, res) {
        req.log("hello from foo() handler");
        return "foo";
    }
    
    function summary(req, res) {
        var a, s, h;
    
        s = "JS summary\n\n";
    
        s += "Method: " + req.method + "\n";
        s += "HTTP version: " + req.httpVersion + "\n";
        s += "Host: " + req.headers.host + "\n";
        s += "Remote Address: " + req.remoteAddress + "\n";
        s += "URI: " + req.uri + "\n";
    
        s += "Headers:\n";
        for (h in req.headers) {
            s += "  header '" + h + "' is '" + req.headers[h] + "'\n";
        }
    
        s += "Args:\n";
        for (a in req.args) {
            s += "  arg '" + a + "' is '" + req.args[a] + "'\n";
        }
    
        return s;
    }
    
    function baz(req, res) {
        res.headers.foo = 1234;
        res.status = 200;
        res.contentType = "text/plain; charset=utf-8";
        res.contentLength = 15;
        res.sendHeader();
        res.send("nginx");
        res.send("java");
        res.send("script");
    
        res.finish();
    }

    Unfortunately, testing Nginx config fails without any clues as to why ?
    Code (Text):
    nginx -t
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
    

    loaded nginScript module as a dynamic module via include file /usr/local/nginx/conf/dynamic-modules.conf in nginx.conf
    Code (Text):
    cat /usr/local/nginx/conf/dynamic-modules.conf
    load_module "modules/ngx_http_image_filter_module.so";
    load_module "modules/ngx_http_fancyindex_module.so";
    load_module "modules/ngx_http_brotli_filter_module.so";
    load_module "modules/ngx_http_brotli_static_module.so";
    load_module "modules/ngx_stream_module.so";
    load_module "modules/ngx_http_js_module.so";
    load_module "modules/ngx_stream_js_module.so";
    

    nginx.conf excerpt
    Code (Text):
    user              nginx nginx;
    worker_processes 4;
    worker_priority -10;
    
    worker_rlimit_nofile 260000;
    timer_resolution 100ms;
    
    pcre_jit on;
    include /usr/local/nginx/conf/dynamic-modules.conf;
    
    
    pid         logs/nginx.pid;
    
    events {
        worker_connections  10000;
        accept_mutex off;
        accept_mutex_delay 200ms;
        use epoll;
        #multi_accept on;
    }
    
    
    http {
    
     
  4. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,110
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,645
    Local Time:
    7:56 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    So examples in post #2 and #3 above seem to fail and prevent nginx from starting ? Seems to have compiled file for nginScript though
    Code (Text):
    checking for OS
     + Linux 4.10.1-1.el7.elrepo.x86_64 x86_64
    checking for C compiler ... found
     + using Clang C compiler
     + clang version: 3.4.2 (tags/RELEASE_34/dot2-final)
    checking for --with-ld-opt="-lrt -ljemalloc -Wl,-z,relro -Wl,-rpath,/usr/local/lib" ... found
    checking for -Wl,-E switch ... found
    checking for gcc builtin atomic operations ... found
    checking for C99 variadic macros ... found
    checking for gcc variadic macros ... found
    checking for gcc builtin 64 bit byteswap ... found
    checking for unistd.h ... found
    checking for inttypes.h ... found
    checking for limits.h ... found
    checking for sys/filio.h ... not found
    checking for sys/param.h ... found
    checking for sys/mount.h ... found
    checking for sys/statvfs.h ... found
    checking for crypt.h ... found
    checking for Linux specific features
    checking for epoll ... found
    checking for EPOLLRDHUP ... found
    checking for EPOLLEXCLUSIVE ... not found
    checking for O_PATH ... found
    checking for sendfile() ... found
    checking for sendfile64() ... found
    checking for sys/prctl.h ... found
    checking for prctl(PR_SET_DUMPABLE) ... found
    checking for sched_setaffinity() ... found
    checking for crypt_r() ... found
    checking for sys/vfs.h ... found
    checking for nobody group ... found
    checking for poll() ... found
    checking for /dev/poll ... not found
    checking for kqueue ... not found
    checking for crypt() ... not found
    checking for crypt() in libcrypt ... found
    checking for F_READAHEAD ... not found
    checking for posix_fadvise() ... found
    checking for O_DIRECT ... found
    checking for F_NOCACHE ... not found
    checking for directio() ... not found
    checking for statfs() ... found
    checking for statvfs() ... found
    checking for dlopen() ... not found
    checking for dlopen() in libdl ... found
    checking for sched_yield() ... found
    checking for SO_SETFIB ... not found
    checking for SO_REUSEPORT ... found
    checking for SO_ACCEPTFILTER ... not found
    checking for SO_BINDANY ... not found
    checking for IP_BIND_ADDRESS_NO_PORT ... not found
    checking for IP_TRANSPARENT ... found
    checking for IP_BINDANY ... not found
    checking for IP_RECVDSTADDR ... not found
    checking for IP_PKTINFO ... found
    checking for IPV6_RECVPKTINFO ... found
    checking for TCP_DEFER_ACCEPT ... found
    checking for TCP_KEEPIDLE ... found
    checking for TCP_FASTOPEN ... found
    checking for TCP_INFO ... found
    checking for accept4() ... found
    checking for eventfd() ... found
    checking for int size ... 4 bytes
    checking for long size ... 8 bytes
    checking for long long size ... 8 bytes
    checking for void * size ... 8 bytes
    checking for uint32_t ... found
    checking for uint64_t ... found
    checking for sig_atomic_t ... found
    checking for sig_atomic_t size ... 4 bytes
    checking for socklen_t ... found
    checking for in_addr_t ... found
    checking for in_port_t ... found
    checking for rlim_t ... found
    checking for uintptr_t ... uintptr_t found
    checking for system byte ordering ... little endian
    checking for size_t size ... 8 bytes
    checking for off_t size ... 8 bytes
    checking for time_t size ... 8 bytes
    checking for AF_INET6 ... found
    checking for setproctitle() ... not found
    checking for pread() ... found
    checking for pwrite() ... found
    checking for pwritev() ... found
    checking for sys_nerr ... found
    checking for localtime_r() ... found
    checking for posix_memalign() ... found
    checking for memalign() ... found
    checking for mmap(MAP_ANON|MAP_SHARED) ... found
    checking for mmap("/dev/zero", MAP_SHARED) ... found
    checking for System V shared memory ... found
    checking for POSIX semaphores ... not found
    checking for POSIX semaphores in libpthread ... found
    checking for struct msghdr.msg_control ... found
    checking for ioctl(FIONBIO) ... found
    checking for struct tm.tm_gmtoff ... found
    checking for struct dirent.d_namlen ... not found
    checking for struct dirent.d_type ... found
    checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
    checking for openat(), fstatat() ... found
    checking for getaddrinfo() ... found
    configuring additional modules
    adding module in ../ngx_cache_purge-2.3
     + ngx_http_cache_purge_module was configured
    adding module in ../ngx_devel_kit-0.3.0
     + ngx_devel_kit was configured
    adding module in ../set-misc-nginx-module-0.31
    found ngx_devel_kit for ngx_set_misc; looks good.
     + ngx_http_set_misc_module was configured
    adding module in ../echo-nginx-module-0.60
     + ngx_http_echo_module was configured
    adding module in ../redis2-nginx-module-0.13
     + ngx_http_redis2_module was configured
    adding module in ../ngx_http_redis-0.3.7
     + ngx_http_redis_module was configured
    adding module in ../memc-nginx-module-0.17
     + ngx_http_memc_module was configured
    adding module in ../srcache-nginx-module-0.31
     + ngx_http_srcache_filter_module was configured
    adding module in ../headers-more-nginx-module-0.32
     + ngx_http_headers_more_filter_module was configured
    configuring additional dynamic modules
    adding module in ../ngx_brotli
     + ngx_brotli was configured
    adding module in ../njs/nginx
     + ngx_js_module was configured
    adding module in ../ngx-fancyindex-0.4.0
     + ngx_http_fancyindex_module was configured
    checking for GD library ... found
    checking for GD WebP support ... not found
    checking for GeoIP library ... found
    checking for GeoIP IPv6 support ... found
    checking for atomic_ops library ... found
    creating objs/Makefile
    
    Configuration summary
      + using threads
      + using PCRE library: ../pcre-8.40
      + using OpenSSL library: ../libressl-2.4.5
      + using zlib library: ../zlib-1.2.11
      + using system libatomic_ops library
    
      nginx path prefix: "/usr/local/nginx"
      nginx binary file: "/usr/local/sbin/nginx"
      nginx modules path: "/usr/local/nginx/modules"
      nginx configuration prefix: "/usr/local/nginx/conf"
      nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
      nginx pid file: "/usr/local/nginx/logs/nginx.pid"
      nginx error log file: "/usr/local/nginx/logs/error.log"
      nginx http access log file: "/usr/local/nginx/logs/access.log"
      nginx http client request body temporary files: "client_body_temp"
      nginx http proxy temporary files: "proxy_temp"
      nginx http fastcgi temporary files: "fastcgi_temp"
      nginx http uwsgi temporary files: "uwsgi_temp"
      nginx http scgi temporary files: "scgi_temp"
    
    
    Sat Mar 25 08:54:25 UTC 2017
    Success: Nginx configure ok
    
     
Thread Status:
Not open for further replies.