Nginx's nginScript Example Analysis

12-19-2023

Let's talk briefly about nginx first

nginx

In the above example, we can see that we can set variable values for nginx at will through js. These variables can be used everywhere in nginx configuration. For example: proxy _ pass, limit _ req _ zone and sub _ filter. The flexibility here has been greatly improved compared with the previous configuration.

2. Operating rules and scenarios of js_run

Js_run runs in the location instruction, and the corresponding javascriptjs_run will be executed if the path of the specified location is matched. The content returned by http is generated directly through javascript. Here is a concrete 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 one. I won't go into details here.

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

Through js_set and js_run, you can have complete control over http request, and the control mode is the use of variable $ r. What is in $ TERM can be seen through the following simple examples.

http { js_set $summary " var a, s, h; s = 'js summary '; s += 'method: ' + $r.method + ' '; s += 'http version: ' + $r.httpversion + ' '; s += 'host: ' + $r.headers.host + ' '; s += 'remote address: ' + $r.remoteaddress + ' '; s += 'uri: ' + $r.uri + ' '; s += 'headers: '; for (h in $r.headers) { s += ' header "' + h + '" is "' + $r.headers

There are still some problems in nginscript.

After the above introduction, I believe everyone has 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 primitive, and it is displayed by log, and the detail of error log is not satisfactory. Secondly, the control is weak. At present, the processing of nginscript is limited to the processing of http request and the return of response, and it is not possible to dynamically process some contents other than nginx request, 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 environments of js_run and js_set are not consistent, and some exceptions will appear in the code segment where js_set executes ok. On the whole, nginscript is still a newborn with good wishes and prospects. It takes some time to polish and optimize. I also hope that you can provide more comments and feedback, and even submit your own plug-ins. So that it can grow better.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us