为 Nginx/Openresty 添加 nginx Njs
njs 是可以 javascrip 编写 nginx 处理逻辑。
njs 可以通过编译成 dynamic module 添加到 nginx/openresty ;
编译的过程:
- 到 http://hg.nginx.org/njs 下载 njs 代码;
- 进入 nginx/openrety 的源码目录
./configurate --add-dynamical-module=/xxxx/njs_root_path/nginx,参数为 njs 源码目录下的 nginx 目录的绝对路径- 在 nginx/openresty 源码根路径下执行
make - 第4步执行完后, 执行
make install - 在
nginx.conf下配置 njs 动态模块; nginx.conf
worker_processes 1;
load_module /usr/local/openresty/nginx/modules/ngx_http_js_module.so; # openrety 默认路径
events {
worker_connections 1024;
}
http {
js_include http.js;
server {
listen 80;
error_log /usr/local/openresty/nginx/logs/e.log;
location / {
js_content foo;
}
}
}
/usr/local/openresty/nginx/conf/http.js
function foo(r) {
r.return(200, "Hello world!"); // 这里直接返回会直接500;必须用 r.return
}
不过感觉 njs 目前还比较鸡肋;比 openresty 提供的 lua 嵌入相比,还差的很多。
