Configuring Nginx with JavaScript

Source: Internet
Author: User

Configuring Nginx with JavaScript

At the nginx.conf 2015 conference last month, officials announced that they had supported the use of JavaScript code to configure Nginx and named the implementation--nginscript. With Nginscript, you can easily implement a custom server configuration in the Niginx configuration file with the JS syntax.

Installation
# 下载最新版本的 nginx 并解压curl -O http://nginx.org/download/nginx-1.9.5.tar.gztar -xzvf nginx-1.9.5.tar.gz# 下载 nginscript 模块并解压curl -O http://hg.nginx.org/njs/archive/tip.tar.gztar -xzvf tip.tar.gz# 编译并安装 nginx$ cd nginx-1.9.5$ ./configure --add-module=刚才解压的nginscript目录$ make$ make install
Defining variables using Nginscript in nignx.conf

Using the Js_set directive, you can define a variable with JavaScript code:

$msg "   var m = ‘Hello ‘;   m += ‘world!‘;   m;";

These variables can be used by other Nginx directives:

location /hello {    add_header Content-Type text/plain;    200 $msg;}
Executing JavaScript code Snippets

The specified javacript code can be executed using the Js_run directive:

location /hello {    js_run "        var res;        res = $r.response;        res.contentType = ‘text/plain‘;        res.status = 200;        res.sendHeader(); res.send( ‘Hello, world!‘ ); res.finish(); ";}
Request Object

In JavaScript code, you can get the request object by $r variable, and the requester-related information is stored on this variable:

Js_set$summary "var a, S,H s = ' Request summary\N\n '; s + = ' Method: ' +$r. Method + ' \n '; s + = ' HTTPVersion: ' +$r. httpversion + ' \n '; s + = ' Host: ' +$r. Headers.host + 'n '; s + = ' Remote Address: ' + $r. remoteaddress + ' \n '; s + = ' uri: ' + $r. Uri + ' \< C5>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;";  
Response Response Object

In JavaScript code, you can get to the response object by $r. Response to set the response content:

"    var res = $r.response;    res.contentType = ‘text/plain‘; res.status = 200; res.sendHeader(); res.send( ‘Hello, world!‘ ); res.finish();";
The difference between Nginscript and JavaScript

Nginscript is not a complete JavaScript, it just implements a subset of the ECMAScript, and for the sake of efficiency, many JavaScript built-in objects are not implemented in Nginscript. I tried to use a date, JSON, and other objects that haven't been implemented yet.

This is almost all the nginscript knowledge, although the complete Emcascript specification is not implemented, but for the configuration nginx, personal feeling is enough.

Configuring Nginx with JavaScript

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.