From aefb249d2cfc0b1ad02a30dbb4176cbeca604c18 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Sun, 26 Sep 2021 09:37:08 +0800 Subject: [PATCH 1/7] njs training --- 6 NJS/content.js | 40 ++++++++++++++++++++++++++++++++++++++++ 6 NJS/default.conf | 27 +++++++++++++++++++++++++++ 6 NJS/nginx.conf | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 6 NJS/content.js create mode 100644 6 NJS/default.conf create mode 100644 6 NJS/nginx.conf diff --git a/6 NJS/content.js b/6 NJS/content.js new file mode 100644 index 0000000..5ebe8eb --- /dev/null +++ b/6 NJS/content.js @@ -0,0 +1,40 @@ +// chrono @ 2021-09 + +export default { say_hello, header_filter, body_filter } + +function say_hello(r) +{ + let str = 'hello ' + + r.variables['hostname'] + ' ' + + r.variables['scheme'] + '://' + + r.variables['host'] + ' ' + + r.variables['request_uri'] + '\n' + + if (r.headersIn['test'] == '1') { + r.headersOut['test'] = 'xxx' + } + + r.return(200, str) +} + +let trailer = '\n' + +function header_filter(r) +{ + r.headersOut['njs'] = njs.version + + r.headersOut['Content-Length'] = + parseInt(r.headersOut['Content-Length']) + trailer.length +} + +function body_filter(r, data, flags) +{ + data = data.toUpperCase() + + if (flags.last) { + data = data + trailer + } + + r.sendBuffer(data, flags); +} + diff --git a/6 NJS/default.conf b/6 NJS/default.conf new file mode 100644 index 0000000..fd43ea7 --- /dev/null +++ b/6 NJS/default.conf @@ -0,0 +1,27 @@ +# chrono 2021-09 +# + +#load_module modules/ngx_http_js_module.so; +js_import /app/content.js; + +server { + + default_type text/html; + + #listen 80 ; + #server_name localhost; + + # curl 127.1 -v -H 'test: 1' + location / { + js_content content.say_hello; + } + + # curl 127.1/filter -v + location /filter { + js_header_filter content.header_filter; + js_body_filter content.body_filter; + + return 200 'test njs filter\n'; + } + +} diff --git a/6 NJS/nginx.conf b/6 NJS/nginx.conf new file mode 100644 index 0000000..e574ccf --- /dev/null +++ b/6 NJS/nginx.conf @@ -0,0 +1,34 @@ + +user nginx; +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +pid /var/run/nginx.pid; + +# njs dynamic module +load_module modules/ngx_http_js_module.so; + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/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"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + #tcp_nopush on; + + keepalive_timeout 65; + + #gzip on; + + include /etc/nginx/conf.d/*.conf; +} -- Gitee From b59e0a12f1b2c9e53186ec762a0c3de7f90e90b7 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Sun, 26 Sep 2021 09:43:19 +0800 Subject: [PATCH 2/7] README.md --- 6 NJS/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 6 NJS/README.md diff --git a/6 NJS/README.md b/6 NJS/README.md new file mode 100644 index 0000000..1f87c2d --- /dev/null +++ b/6 NJS/README.md @@ -0,0 +1,34 @@ +# Lab环境介绍 + +本次实验需要安装NGINX JavaScript模块,建议使用官方预构建包: + +~~~shell +cat << EOF > /etc/yum.repos.d/nginx.repo +[nginx] +name=nginx repo +baseurl=https://nginx.org/packages/mainline/centos/7/\$basearch/ +gpgcheck=0 +enabled=1 +EOF + +yum update -y + +yum install nginx -y +~~~ + +或者官方镜像: + +~~~docker +docker pull nginx:alpine +~~~ + +## 配置文件 + +参考`nginx.conf`,编辑NGINX的主配置文件,加载njs模块: + +~~~conf +# njs dynamic module +load_module modules/ngx_http_js_module.so; +~~~ + +## NJS脚本 -- Gitee From aba94f7095f387de3316e47806b330587c7f7f57 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Sun, 26 Sep 2021 09:44:49 +0800 Subject: [PATCH 3/7] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp -- Gitee From 8b488e64e8c8883a93c3c66551853be9bd26d8a5 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Sun, 26 Sep 2021 09:50:39 +0800 Subject: [PATCH 4/7] README.md --- 6 NJS/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/6 NJS/README.md b/6 NJS/README.md index 1f87c2d..c994eea 100644 --- a/6 NJS/README.md +++ b/6 NJS/README.md @@ -31,4 +31,25 @@ docker pull nginx:alpine load_module modules/ngx_http_js_module.so; ~~~ +拷贝default.conf到NGINX的安装目录`/etc/nginx/conf.d/`下,注意其中的`js_import`指令,指定读取的是`/app/`里的js脚本: + +~~~conf +js_import /app/content.js; +~~~ + + ## NJS脚本 + +拷贝`content.js`脚本文件到`/app/`目录下。 + +其中有3个JavaScript函数:`say_hello, header_filter, body_filter`。 + + +## 测试验证 + +测试NGINX配置是否正确,再启动NGINX: + +~~~shell +nginx -t +nginx +~~~shell -- Gitee From fa5c8b1ebd439566def20977cdadaf4d8e69ae2c Mon Sep 17 00:00:00 2001 From: chronolaw Date: Sun, 26 Sep 2021 09:54:06 +0800 Subject: [PATCH 5/7] README.md --- 6 NJS/README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/6 NJS/README.md b/6 NJS/README.md index c994eea..1ee32c0 100644 --- a/6 NJS/README.md +++ b/6 NJS/README.md @@ -31,7 +31,7 @@ docker pull nginx:alpine load_module modules/ngx_http_js_module.so; ~~~ -拷贝default.conf到NGINX的安装目录`/etc/nginx/conf.d/`下,注意其中的`js_import`指令,指定读取的是`/app/`里的js脚本: +拷贝`default.conf`到NGINX的安装目录`/etc/nginx/conf.d/`下,注意其中的`js_import`指令,指定读取的是`/app/`里的js脚本: ~~~conf js_import /app/content.js; @@ -52,4 +52,14 @@ js_import /app/content.js; ~~~shell nginx -t nginx +~~~ + +运行`curl`命令,测试JavaScript函数的执行效果: + ~~~shell +# 头字段test: xxx,字符串是各种变量拼成的 +curl 127.1 -v -H 'test: 1' + +# 多了一个头njs,body全大写,又添加了尾部字符串 +curl 127.1/filter -v +~~~ -- Gitee From 1d694af5b729226d8f923f3bc33f6af61bc53874 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Sun, 26 Sep 2021 09:58:24 +0800 Subject: [PATCH 6/7] README.md --- 6 NJS/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/6 NJS/README.md b/6 NJS/README.md index 1ee32c0..80e8f65 100644 --- a/6 NJS/README.md +++ b/6 NJS/README.md @@ -57,9 +57,11 @@ nginx 运行`curl`命令,测试JavaScript函数的执行效果: ~~~shell -# 头字段test: xxx,字符串是各种变量拼成的 +# 头字段test: xxx,响应字符串是各种变量拼成的 curl 127.1 -v -H 'test: 1' # 多了一个头njs,body全大写,又添加了尾部字符串 curl 127.1/filter -v ~~~ + +接下来可修改`content.js`里的JavaScript代码,重启NGINX,再观察curl的输出结果。 -- Gitee From cec288c5b7987a1f7b42894825922c6ec46418a9 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Sun, 26 Sep 2021 10:00:05 +0800 Subject: [PATCH 7/7] njs --- .../README.md" | 0 .../content.js" | 0 .../default.conf" | 0 .../nginx.conf" | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename 6 NJS/README.md => "6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/README.md" (100%) rename 6 NJS/content.js => "6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/content.js" (100%) rename 6 NJS/default.conf => "6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/default.conf" (100%) rename 6 NJS/nginx.conf => "6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/nginx.conf" (100%) diff --git a/6 NJS/README.md "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/README.md" similarity index 100% rename from 6 NJS/README.md rename to "6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/README.md" diff --git a/6 NJS/content.js "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/content.js" similarity index 100% rename from 6 NJS/content.js rename to "6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/content.js" diff --git a/6 NJS/default.conf "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/default.conf" similarity index 100% rename from 6 NJS/default.conf rename to "6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/default.conf" diff --git a/6 NJS/nginx.conf "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/nginx.conf" similarity index 100% rename from 6 NJS/nginx.conf rename to "6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/nginx.conf" -- Gitee