search
HomeOperation and MaintenanceNginxnginx JavaScript capabilities nginScript example analysis

nginx can mainly do the following:

1. Working on the seventh layer of tcp, it can analyze and process all contents of the http protocol.
2. Support lua, perl, javascript dynamic language
3. Support third-party plug-ins

Let’s talk about nginscript

1. nginscript is javascript/ecmascript subset. 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 implemented through a small virtual machine (vm) that is smaller, has lower energy consumption, and is more suitable for nginx application scenarios. It can be understood that nginx implements its own set of lexical analysis.

3. nginscript runs in the nginx configuration file. For example: in the nginx.conf file. Therefore, nginscript can complete everything that traditional configuration files can handle, and at the same time, it can make configuration management dynamic. This is also the most important reason for the emergence of nginscript.

4. nginscript exists as an nginx plug-in. The plug-in name is: njs. Like other nginx plug-ins, we need to recompile nginx to complete the installation.

5. nginscript is currently in early development status. You can communicate with the nginx team and put forward your demands through email.

How to install nginscript

Just follow the official steps here:

// 1. Download The latest nginx package, the address can be seen: wget //2. Unzip tar -xzvf nginx-1.9.4.tar.gz //3. Get the nginscript module through mercurial. If mercurial is not installed here, you need to run yum install mercurial hg clone

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

Specific how to use nginscript

The use of nginscript mainly adds 2 instructions to the nginx configuration system. The specific instructions are:

js_set, set the variable value in the configuration

js_run, directly execute the configuration rules

1. First look at js_set in nginx.conf How does it work?

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

Result:

nginx JavaScript capabilities nginScript example analysis

In the above example, it can be seen that we can set variable values ​​​​to nginx through js at will. These variables can be used in various places in nginx configuration. For example: proxy_pass, limit_req_zone, and sub_filter. Compared with the previous configuration, the flexibility here has been greatly improved.

2. The running rules and scenarios of js_run

js_run is run in the location instruction, and the corresponding javascript will be executed if it matches the path of the specified location
js_run Yes Directly generate the content returned by http through javascript
Here is a specific example:

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

This result is the same as the first result. I won’t go into details here.

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

Through js_set and js_run, you can have complete control over the http request request. Control The way is to use the variable $r. What's in $r can be seen with 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;
 }
}

The result is as shown in the figure:

nginx JavaScript capabilities nginScript example analysis

nginscript’s current problems

After the above introduction, I believe Everyone already has a basic understanding of nginscript. So let's take a look at what problems this newborn has.

First of all, the debugging method is weak. At present, it is still relatively primitive and is displayed through logs, and the detail of the error log is very unsatisfactory.
Secondly, the control is weak. At present, nginscript's processing power is limited to the processing of http requests and returning responses. It is not yet able to dynamically process some content other than nginx requests, such as dynamic user data or dynamic updates of forwarding configuration tables.
Finally, the overall implementation is weak. The overall structure is relatively simple. The running environments of js_run and js_set are inconsistent. The code segment where js_set executes ok will cause some exceptions on js_run.
In summary, nginscript is still a newborn with great wishes and prospects. It takes some time to polish and optimize. I also hope that everyone will provide more opinions and feedback, and even submit your own plug-ins. This allows it to grow better.

For our practical scenario

These are two main scenarios that I discussed with Li Xiaotengjun and Donald before, the reallog system and the nohost2.0 system. nginscript is undoubtedly great news for both scenarios here. In terms of rule response, it can have a very flexible processing method under the existing system. However, in terms of dynamic loading of user configuration, we still need to implement it in other ways. For this part, we will first raise an issue to the nginx development team and take a look at the specific situation before further discussing and synchronizing it with everyone.

The above is the detailed content of nginx JavaScript capabilities nginScript example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

NGINXUnit supports multiple programming languages ​​and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure nginx in WindowsHow to configure nginx in WindowsApr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve nginx403 errorHow to solve nginx403 errorApr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools