background

In September 2015, Nginx announced support for JavaScript like languages. This means developers can control the world’s best HTTP and reverse proxy servers more easily and freely, and generate more useful and fun ideas on top of it. Nginx is also moving towards the next phase of dynamic configuration. You can check out the official introduction link.

A quick word about Nginx

Nginx [Engine X] is the most popular web server and reverse proxy server in the world. According to third-party companies, nginx is currently used in at least 23 percent of the world’s servers, and that number is growing. At present, it is also the first choice of BAT in China, so that is why we pay attention to it at the first time. Nginx does the following:

  • 1, work in TCP layer 7, can analyze and process all content of HTTP protocol.
  • 2, support lua, Perl, JavaScript dynamic language
  • 3. Support third-party plug-ins

Say again nginScript

NginScript is a subset of JavaScript/ECMAscript. It implements most of the capabilities of the JavaScript language, does not fully comply with the ECMAScript standard, and does away with some of the more difficult aspects of JavaScript.

NginScript is not implemented with the V8 engine. Instead, a smaller, less power-intensive virtual machine (VM) is implemented that is more suitable for nGINx application scenarios. Nginx implements its own set of lexical parsing for it.

3. NginScript runs in nginx configuration files. For example, in nginx.conf. So nginScript can do everything a traditional configuration file can do, while making configuration management dynamic. This is the most important reason nginScript came into being.

4. NginScript is an Nginx plug-in. The name of the plug-in is NJS. As with other Nginx plug-ins, we need to recompile Nginx to complete the installation.

5. NginScript is currently in its early stages of development. You can communicate with the Nginx team and make your appeal via email [email protected].

How to install nginScript

Just follow the official steps here:

// 1, download the latest nginx package: nginx.org/en/download… Wget nginx.org/download/ng… // The nginScript module is installed in the mercurial directory. The nginScript module is installed in the mercurial directory. You need to run yum install Mercurial hg clone hg.nginx.org/njs

//4, compile nginx, here only specific NJS module, other need to remember to install together oh. If you have not compiled nginx, some dependencies require yum to install, please do your own search. CD nginx – 1.9.4. / configure — add – the module =.. / NJS /nginx –prefix=/usr/local make make install ok

How to use nginScript

The use of nginScript mainly adds two instructions to the nginx configuration system. Specific instructions are as follows:

  • js_set, set the variable values in the configuration
  • js_runTo directly execute the configuration rule

1, let’s see how js_set works in nginx.conf.

http { js_set $msg" var str = 'hello,imweb'; // JavaScript str; "; server { ... location /{ return 200 $msg; }}}Copy the code

Results:

As you can see from the above example, we can set variable values to nginx arbitrarily by using JS. These variables can be used anywhere in the nginx configuration. For example, proxy_pass,limit_req_zone, and sub_filter. This is much more flexible than the previous configuration.

2. Js_run running rules and scenarios

  • js_runIs run in the location directive, matching the specified location path will execute the corresponding JavaScript
  • js_runThe HTTP content is generated directly through JavaScript

Here’s a concrete example:

location /imwebteam { js_run " var res; res = $r.response; res.status = 200; res.send('hello,imweb! '); res.finish(); "; }Copy the code

This is the same result as the first result. I won’t repeat it here.

In addition to processing the two instructions, there is an important variable $r

Js_set and js_run give you complete control over HTTP request requests through the use of the $r variable. You can see what’s in $r in the following simple example.

http { js_set $summary " var a, s, h; s = 'JS summary\n\n'; s += 'Method: ' + $r.method + '\n'; s += 'HTTP version: ' + $r.httpVersion + '\n'; s += 'Host: ' + $r.headers.host + '\n'; s += 'Remote Address: ' + $r.remoteAddress + '\n'; s += 'URI: ' + $r.uri + '\n'; s += 'Headers:\n'; for (h in $r.headers) { s += ' header \"' + h + '\" is \"' + $r.headers[h] + '\"\n'; } s += 'Args:\n'; for (a in $r.args) { s += ' arg \"' + a + '\" is \"' + $r.args[a] + '\"\n'; } s; "; server { listen 8000; location /imwebteam{ return 200 $summary; }}Copy the code

The result is shown below:

There are still problems with nginScript

After the above introduction, I believe you have a basic understanding of nginScript. So let’s see what’s wrong with this newborn.

  • First, the debugging method is weak. At present, it is still relatively primitive and displayed in the way of log, and the level of detail of error log is not satisfactory.
  • Second, the control is weak. At present, nginScript’s processing power is limited to HTTP request processing and response return level, and it is not able to dynamically process some content other than Nginx request, such as dynamic user data or dynamic update of forwarding configuration table.
  • Finally, the overall implementation is weak. The overall structure is still relatively simple, js_run and js_set run environment is not quite consistent, js_set ok code section in js_run will appear some exceptions.

All in all, nginScript is a new baby with great hopes and prospects. It takes time to polish and optimize. We also encourage you to provide feedback and even submit your own plugins. So that it has a better growth.

For our practice scenario

The two main scenarios discussed with Li Xiaoteng jun and Donald are realLog system and Host 2.0 system. NginScript is great news for both of these scenarios, where rule responses can be handled flexibly under the existing architecture. However, in terms of user configuration dynamic loading, we still need to implement it in other ways. We will first issue this part to the Nginx development team, and then discuss and synchronize with you further after looking at the specific situation.