Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study of JavaScript capability of nginx nginScript

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "nginx JavaScript capability nginScript case analysis". In daily operation, I believe many people have doubts about nginx JavaScript capability nginScript case analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "nginx JavaScript capability nginScript case analysis". Next, please follow the editor to study!

Nginx can mainly do the following:

1. Working in the seventh layer of tcp, all the contents of http protocol can be analyzed and processed.

2. Support lua,perl,javascript dynamic language

3. Support third-party plug-ins

Let's talk about nginscript.

1. 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 abandons the more difficult parts of javascript.

2. Nginscript is not implemented through the V8 engine. Instead, it is achieved through a small virtual machine (vm) that is smaller, consumes less energy, and is more in line with the nginx application scenario. It can be understood that nginx has implemented a set of lexical parsing for it.

3. Nginscript runs in the configuration file of nginx. For example: in the nginx.conf file. So nginscript can do everything that traditional configuration files can handle, while making configuration management dynamic. This is the most important reason for the emergence of nginscript.

4. Nginscript exists as a nginx plug-in. The plug-in is called njs. Like other nginx plug-ins, we need to recompile nginx to complete the installation.

5. Nginscript is currently in the early state of research and development. You can communicate with nginx team and put forward your request by email and other means.

How to install nginscript

Just follow the steps given by the authorities:

/ / 1. Download the latest nginx package, which can be found in wget / / 2, decompress tar-xzvf nginx-1.9.4.tar.gz / / 3, and obtain the nginscript module through mercurial. If mercurial is not installed here, you need to run yum install mercurial hg clone first.

/ / 4. Compile nginx. Only the njs module is specified here. Remember to install other required modules together. If you have not compiled nginx and some dependent modules need to be installed by yum, please search for them yourself. Cd nginx-1.9.4. / configure-- add-module=../njs/nginx-- prefix=/usr/local make make install ok, this is all installed, and we can start playing.

How to use nginscript

The use of nginscript is mainly due to the addition of two instructions to the configuration system of nginx. The specific instructions are as follows:

Js_set, set the value of the variable in the configuration

Js_run, direct execution of configuration rules

1. Take a look at how js_set works in nginx.conf.

Http {js_set $msg "var str = 'hello,imweb'; / / javascript str;"; server {... Location / {return 200$ msg;}

Results:

In the above example, you can see that we can arbitrarily set the value of a variable to nginx through js. These variables can be used everywhere in the nginx configuration. For example: proxy_pass,limit_req_zone, and sub_filter. This has greatly improved the flexibility compared to the previous configuration.

2. Running rules and scenarios of js_run

Js_run is run in the location instruction. Matching the path of the specified location will execute the corresponding javascript.

Js_run generates the content returned by http directly through javascript.

Here is a specific example:

Location / imwebteam {js_run "var res; res = $r.response; res.status = 200; res.send ('HelloJournal imwebmasters'); res.finish ();";}

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

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

You can have complete control over http request requests through js_set and js_run, which is controlled by the use of the variable $r. What in $r can be seen through the following simple example.

Http {js_set $summary "var a, s, h; s ='js summary\ n\ nregions; s + = 'method:' + $r.method +'\ nregions; s + = 'httpversion:' + $r.httpversion +'\ nregions; s + = 'host:' + $r.headers.host +'\ nregions; s + = 'remoteaddress:' + $r.remoteaddress +'\ nregions; s + = 'uri:' + $r.uri +'\ nregions; s + = 'headers:\ n' For (h in $r.headers) {s + = 'header\ "+ h +'\" is\ "'+ $r.headers [h] +'\"\ nregions;} s + = 'args:\ ncustomers; for (an in $r.args) {s + =' arg\ "+ a +'\" is\ "+ $r.args [a] +'\"\ ncustomers;} s; "; server {listen 8000; location / imwebteam {return 200$ summary;}

The result is shown in the figure:

The problems existing in nginscript at present

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

First of all, the debugging method is weak. At present, it is still relatively primitive, displayed in the way of log, and the level of detail of the error log is not satisfactory.

Secondly, the control is weak. At present, the processing strength of nginscript is limited to the processing of http request and the return of response, and it is still unable to dynamically deal with some contents other than nginx requests, such as dynamic user data or dynamic update of forwarding configuration table.

Finally, the overall realization is weak. The overall structure is still relatively simple, the running environment of js_run and js_set is not quite the same, and the code snippet in which js_set executes ok will have some exceptions on js_run.

Generally speaking, nginscript is still a newborn with bright wishes and prospects. It takes some time to polish and optimize. I also hope that you can provide a lot of comments and feedback, or even submit your own plug-ins. So that it can grow better.

For our practice scenario

This is the two main scenarios discussed with Li Xiaotengjun and donald, the reallog system and the nohost2.0 system. Nginscript is undoubtedly good news for both 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. In this part, we first mention issue to the nginx development team, look at the specific situation and then further discuss and synchronize with you.

At this point, the study on "nginScript instance Analysis of nginx's JavaScript capabilities" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report