ScaleScaleScaleScale

Tips / Nginx


nginScript: Nginx + JavaScript

According to the latest Githut reports, JavaScript is the most widely used programming language. In fact, a substantial portion of the World Wide Web is written in this language. JavaScript is an agile, simple, powerful and flexible programming platform.

Nginx, on the other hand, is the most popular and best performing HTTP server. It is generating a web revolution when developers and sysadmins look for better performance on their websites.

Nginx + JavaScript = nginScript

nginScript is a JavaScript implementation used to enable dynamic configuration from within the same web server. Just a few days ago, at this year’s nginx.conf, Nginx original author and CTO of Nginx.com, Igor Sysoev, explained everything about this cool new feature.

This custom JavaScript VM was built to be very efficient in terms time. It has a great low startup and tear-down time, which allows JS snippets to run more efficiently than ever before. It also includes a technology to individually block and manage different Nginx operations (like suspend or resume) in a better way.

Then, there is the configuration syntax that allows you to introduce JavaScript snippets into your own Nginx configuration. The snippets are evaluated at run time when someone makes the requests and establishes communication with your web server. This makes it possible for developers to create better apps, while manipulating requests and controlling responses directly from inside Nginx.

This sounds great, and many of people are relating this with Nginx running as an app server like NodeJS, for example. However, it is important to clarify that nginScript doesn’t pretend to be and wasn’t built to be an app server. The inclusion of JavaScript inside of the core of Nginx is not there to serve apps, but to extend the control you have over your web apps by manipulating requests, responses and add controls.

How Can You Install the nginScript Module?

Get the latest source for Nginx at http://nginx.org/en/download.html:

wget http://nginx.org/download/nginx-1.9.4.tar.gz
tar -xvpzf nginx-1.9.4.tar.gz

Get the development sources for nginScript:

hg clone http://hg.nginx.org/njs

Build and install Nginx:

cd nginx-1.9.4
./configure --add-module=../njs/nginx --prefix=/your/custom/installation/directory
make
make install

Examples of nginScript working with declaration of variables, using js_set:

http {
    js_set $hello_world "
            var str = 'Hello World!';
            // JavaScript
            str;
    ";
    server {
        ...
        location /{
            return 200 $hello_world;
        }
    }
}

The result of the JS snippet is stored in a variable called “$hello_world.” Later, inside the server {} block, this is used as a response to a web request at a pre-defined location.

Conclusion

If you’re good at writing JavaScript code, you are going to love this cool new integration feature to extend control of your web apps. Learn more from Igor Sysoev’s original announcement: Launching nginScript and Looking Ahead

Popular search terms:

  • nginx enable javascript
  • nginScript
  • nginx block js
  • nginx not serving javascript
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking