angie njs 模块试用
angie njs 是一个类似lua 的模块,提供了基于js 进行nginx 功能开发的能力(但是目前没有nginx lua 模块强大)
以下是一个简单试用
环境准备
基于了以前测试nginx lua 模块的基础镜像
- http.js
function foo(r) {
r.log("hello from foo() handler");
return "foo";
}
function summary(r) {
var a, s, h;
s = "JS summary\n\n";
s += "Method: " + r.method + "\n";
s += "HTTP version: " + r.httpVersion + "\n";
s += "Host: " + r.headersIn.host + "\n";
s += "Remote Address: " + r.remoteAddress + "\n";
s += "URI: " + r.uri + "\n";
s += "Headers:\n";
for (h in r.headersIn) {
s += " header '" + h + "' is '" + r.headersIn[h] + "'\n";
}
s += "Args:\n";
for (a in r.args) {
s += " arg '" + a + "' is '" + r.args[a] + "'\n";
}
return s;
}
function baz(r) {
r.status = 200;
r.headersOut.foo = 1234;
r.headersOut['Content-Type'] = "text/plain; charset=utf-8";
r.headersOut['Content-Length'] = 15;
r.sendHeader();
r.send("nginx");
r.send("java");
r.send("script");
r.finish();
}
function hello(r) {
r.return(200, "Hello world!");
}
// since 0.7.0
async function hash(r) {
let hash = await crypto.subtle.digest('SHA-512', r.headersIn.host);
r.setReturnValue(Buffer.from(hash).toString('hex'));
}
export default {foo, summary, baz, hello, hash};
- angie.conf
# package: angie-module-auth-jwt
#load_module modules/ngx_http_auth_jwt_module.so;
# package: angie-module-auth-spnego
#load_module modules/ngx_http_auth_spnego_module.so;
# package: angie-module-brotli
#load_module modules/ngx_http_brotli_filter_module.so;
#load_module modules/ngx_http_brotli_static_module.so;
# package: angie-module-cache-purge
#load_module modules/ngx_http_cache_purge_module.so;
# package: angie-module-dav-ext
#load_module modules/ngx_http_dav_ext_module.so;
# package: angie-module-echo
#load_module modules/ngx_http_echo_module.so;
# package: angie-module-enhanced-memcached
#load_module modules/ngx_http_enhanced_memcached_module.so;
# package: angie-module-eval
#load_module modules/ngx_http_eval_module.so;
# package: angie-module-geoip2
#load_module modules/ngx_http_geoip2_module.so;
#load_module modules/ngx_stream_geoip2_module.so;
# package: angie-module-headers-more
#load_module modules/ngx_http_headers_more_filter_module.so;
# package: angie-module-image-filter
#load_module modules/ngx_http_image_filter_module.so;
# package: angie-module-keyval
#load_module modules/ngx_http_keyval_module.so;
# package: angie-module-lua
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
#load_module modules/ngx_stream_lua_module.so;
# package: angie-module-ndk
# package: angie-module-njs
load_module modules/ngx_http_js_module.so;
load_module modules/ngx_stream_js_module.so;
# package: angie-module-opentracing
#load_module modules/ngx_http_opentracing_module.so;
# package: angie-module-perl
#load_module modules/ngx_http_perl_module.so;
# package: angie-module-redis2
#load_module modules/ngx_http_redis2_module.so;
# package: angie-module-rtmp
#load_module modules/ngx_rtmp_module.so;
# package: angie-module-set-misc
#load_module modules/ngx_http_set_misc_module.so;
# package: angie-module-subs
#load_module modules/ngx_http_subs_filter_module.so;
# package: angie-module-upload
#load_module modules/ngx_http_upload_module.so;
# package: angie-module-vod
#load_module modules/ngx_http_vod_module.so;
# package: angie-module-xslt
#load_module modules/ngx_http_xslt_filter_module.so;
user angie;
worker_processes auto;
worker_rlimit_nofile 65536;
error_log /var/log/angie/error.log notice;
events {
worker_connections 65536;
}
http {
include /etc/angie/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format extended '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" rt="$request_time" '
'"$http_user_agent" "$http_x_forwarded_for" '
'h="$host" sn="$server_name" ru="$request_uri" u="$uri" '
'ucs="$upstream_cache_status" ua="$upstream_addr" us="$upstream_status" '
'uct="$upstream_connect_time" urt="$upstream_response_time"';
access_log /var/log/angie/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
resolver 114.114.114.114;
#gzip on;
# 此处配置js 模块
js_import /opt/js/http.js;
js_set $foo http.foo;
js_set $summary http.summary;
js_set $hash http.hash;
include /etc/angie/http.d/*.conf;
}
#stream {
# include /etc/angie/stream.d/*.conf;
#}
- 站点集成
default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/angie/html;
index index.html index.htm;
}
location /status/ {
api /status/;
allow 127.0.0.1;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/angie/html;
}
location = /baz {
default_type text/html;
add_header X-Foo $foo;
js_content http.baz;
}
location = /summary {
default_type text/html;
return 200 $summary;
}
location = /hello {
default_type text/html;
js_content http.hello;
}
}
- 效果
说明
njs 模块目前提供的能力还是比较方便的,同时我们也可以试用typescript 进行模块的开发,这样也就具有了强类型的能力,还是很不错的
参考资料
https://angie.software/en/install/#install-docker
https://nginx.org/en/docs/njs/index.html
https://github.com/rongfengliang/angie_njs_lua_leaning