diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..1377554ebea6f98a2c748183bc5a96852af12ac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git "a/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/README.md" "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/README.md" new file mode 100644 index 0000000000000000000000000000000000000000..80e8f65c4274a2a06171e19cbe3a0ff75e0ea700 --- /dev/null +++ "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/README.md" @@ -0,0 +1,67 @@ +# 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; +~~~ + +拷贝`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 +~~~ + +运行`curl`命令,测试JavaScript函数的执行效果: + +~~~shell +# 头字段test: xxx,响应字符串是各种变量拼成的 +curl 127.1 -v -H 'test: 1' + +# 多了一个头njs,body全大写,又添加了尾部字符串 +curl 127.1/filter -v +~~~ + +接下来可修改`content.js`里的JavaScript代码,重启NGINX,再观察curl的输出结果。 diff --git "a/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/content.js" "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/content.js" new file mode 100644 index 0000000000000000000000000000000000000000..5ebe8ebc5d384084a2101b3468301abe0a220405 --- /dev/null +++ "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/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\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/default.conf" "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/default.conf" new file mode 100644 index 0000000000000000000000000000000000000000..fd43ea73f0f12b569d12466fe899c69150ec377e --- /dev/null +++ "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/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\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/nginx.conf" "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/nginx.conf" new file mode 100644 index 0000000000000000000000000000000000000000..e574ccf9c93172e2a468d21724d08b374ffe63e7 --- /dev/null +++ "b/6 NJS\346\225\260\346\215\256\351\235\242\347\274\226\347\250\213/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; +}