About nginx's newly released JavaScript capability nginScript

Source: Internet
Author: User
Tags mercurial

About nginx's newly released JavaScript capability nginScript

Background

On July 6, September 2015, nginx announced support for JavaScript-like languages. This means that developers can more easily and freely control the world's best HTTP and reverse proxy servers, and more useful and interesting ideas can be derived from this. Nginx is also evolving towards the next phase of dynamic configuration. You can click to view the official introduction link.

Let's talk about nginx.

Nginx [engine x] is the most popular and best web server and reverse proxy server in the world. According to statistics from third-party companies, at least 23% of the servers around the world are using nginx. Of course, this number is still expanding. At present, it is also the first choice for domestic BAT, so this is why we are immediately concerned about it. Nginx can mainly do the following:

  • 1. Working on the Seventh Layer of TCP, all HTTP content can be analyzed and processed.

  • 2. Supports lua, perl, and JavaScript dynamic languages.

  • 3. Support for third-party plug-ins

Let's talk about nginScript.

1,NginScript is a subset of JavaScript/ECMAscript.. It implements the capabilities of most JavaScript languages, does not fully comply with the ECMAScript standard, and does not discard the more difficult part of JavaScript.

2,NginScript is not implemented through the V8 engine. It is implemented through a smaller Virtual Machine VM with lower energy consumption and more suitable for nginx application scenarios. It can be understood that nginx implements its own lexical parsing.

3,NginScript runs in the nginx configuration file.. For example, in the nginx. conf file. Therefore, nginScript can complete all the things that traditional configuration files can handle and make configuration management dynamic. This is also the most important reason for the emergence of nginScript.

4,nginScriptIt exists as an nginx plug-in. Plug-in name: njs. Like other nginx plug-ins, We need to recompile nginx for installation.

5,NginScript is currently in the early development status. You can communicate with the nginx team through email nginx-devel@nginx.org and other methods and put forward your demands.

How to install nginScript

Follow the official steps here:

// 1, download the latest nginx package, the address is visible: http://nginx.org/en/download.html wget http://nginx.org/download/nginx-1.9.4.tar.gz // 2, extract tar-xzvf nginx-1.9.4.tar.gz // 3, get nginScript module through mercurial, if mercurial is not installed here, run the yum install mercurial hg clone http://hg.nginx.org/njs first

// 4. Compile nginx. Here, only the njs module is specific. Remember to install other required modules together. If you have not compiled nginx, and some dependent modules require yum installation, search for them by yourself. Cd nginx-1.9.4./configure -- add-module = ../njs/nginx -- prefix =/usr/local make install OK, this installation is complete, we can start to play.

How to Use nginScript

NginScript is mainly used to add two commands in the nginx Configuration System. The specific commands are as follows:

  • js_set, Set the variable value in the configuration

  • js_runDirectly execute the configuration rules

1. check it out first.js_setHow to run it in nginx. conf.

http {
    js_set $msg"
            var str = 'hello,imweb';
            // JavaScript str;
    ";

    server {
        ...
        location /{
            return 200 $msg;
        }
    }
}

Result:

In the above example, we can see that we can set variable values for nginx at will through JS. These variables can be used in various areas of nginx configuration. For example, proxy_pass, limit_req_zone, and sub_filter. The configuration has greatly improved the flexibility.

2,js_runRunning rules and scenarios

  • js_runIs running in the location command, matching the specified location path will execute the corresponding JavaScript

  • js_runIs directly produced through JavaScript HTTP returned content

The following is a specific example:

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

The result is the same as the first result. I will not go into details here.

3. An important variable besides processing two Commands$r

Passjs_setAndjs_runYou can have full control over HTTP request requests. The control mode is variable.$r.$rThe following simple example shows what is in.

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;     } } 

Result

NginScript

After the above introduction, I believe you have a basic understanding of nginScript. So let's see what problems this newborn has.

  • First, the debugging method is weak. At present, the error logs are relatively primitive and displayed in log mode, and the details of error logs are not satisfactory.
  • Secondly, the control is weak. Currently, nginScript is only used to process http requests and return response. It cannot dynamically process any content other than nginx requests, such as dynamic user data or dynamic update of forwarding configuration tables.
  • Finally, the overall implementation is weak. The overall structure is relatively simple. The runtime environment of js_run and js_set is inconsistent. Some exceptions may occur in the code segment where js_set executes OK on js_run.

In general, nginScript is still a newborn with a great desire and bright prospects. It takes some time for polishing and optimization. I also hope that you will provide more comments and feedback, or even submit your own plug-ins. So that it can grow better.

For our practical scenarios

The two main scenarios discussed previously with Mr. Li and Mr. donald are the realLog and nohost2.0 systems. NginScript is undoubtedly a good news for both scenarios. In terms of Rule response, there are flexible solutions under the existing system. However, in terms of user configuration dynamic loading, we still need to implement it in other ways. In this part, we will first mention issue to the nginx development team to see the specific situation and further discuss and synchronize it with you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.