说明

njs模块官网:
http://nginx.org/en/docs/njs/index.html
njs API:
http://nginx.org/en/docs/njs/reference.html

njs常用API

  • r.uri
  • r.method
  • r.requestBody

njs模块指令

js_content

然后在具体的某个 location 里,可以使用 js_content ,来指定一个 JS 函数来处理请求。不得不说,目前的 API 设计,和 Node.JS 的 http 模块很像。

js_set

除了这种方法之外,还可以通过 js_set 这个指令,将一个 JS 函数指定给某个 Nginx 变量。这样可以在 location 中像正常的 Nginx 变量一样使用这个变量,实际处理强求的时候,会执行这个 JS 函数,并将函数返回值绑定到变量上。

安装

http://nginx.org/en/linux_packages.html

添加yum仓库

vim /etc/yum.repos.d/nginx.repo

  1. [nginx-stable]
  2. name=nginx stable repo
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  4. gpgcheck=1
  5. enabled=1
  6. gpgkey=https://nginx.org/keys/nginx_signing.key
  7. [nginx-mainline]
  8. name=nginx mainline repo
  9. baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
  10. gpgcheck=1
  11. enabled=0
  12. gpgkey=https://nginx.org/keys/nginx_signing.key

安装nginx和njs

  1. sudo yum install yum-utils
  2. sudo yum install nginx nginx-module-njs nginx-module-njs-debuginfo

配置

配置Nginx拦截请求

  1. load_module modules/ngx_http_js_module.so;
  2. user nginx;
  3. worker_processes 1;
  4. error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include /etc/nginx/mime.types;
  11. default_type application/octet-stream;
  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';
  15. access_log /var/log/nginx/access.log main;
  16. sendfile on;
  17. #tcp_nopush on;
  18. keepalive_timeout 65;
  19. #gzip on;
  20. js_include /etc/nginx/my_proxy.js;
  21. upstream es {
  22. server bigdata-dev07: 9200 weight=1 fail_timeout=2s max_fails=2;
  23. server bigdata-dev08: 9200 weight=1 fail_timeout=2s max_fails=2;
  24. server bigdata-dev06: 9200 weight=1 fail_timeout=2s max_fails=2;
  25. }
  26. server {
  27. listen 8000;
  28. location / {
  29. try_files /_not_exists_ @es;
  30. }
  31. location @es {
  32. proxy_pass http://es;
  33. }
  34. location /_msearch {
  35. js_content power;
  36. }
  37. }
  38. #include /etc/nginx/conf.d/*.conf;
  39. }

重点:

  • load_module modules/ngx_http_js_module.so;
  • js_include /etc/nginx/my_proxy.js;
  • js_content power;

njs脚本限制

my_proxy.js

  1. function power(r) {
  2. if (r.uri != "/_msearch") {
  3. r.internalRedirect("@es")
  4. return;
  5. }
  6. if (!r.requestBody) {
  7. r.internalRedirect("@es");
  8. return;
  9. }
  10. try {
  11. var arr = r.requestBody.split("\n");
  12. for (var i = 0; i < arr.length; i++) {
  13. var body = arr[i];
  14. if (body) {
  15. if (dealJson(body, r)) {
  16. r.error("over range");
  17. r.return(403);
  18. return;
  19. }
  20. }
  21. }
  22. } catch (e) {
  23. r.error(r.uri + " " + e);
  24. }
  25. r.internalRedirect("@es")
  26. }
  27. function dealJson(body, r) {
  28. r.error(body);
  29. var data = JSON.parse(body);
  30. if (data) {
  31. var query = data["query"];
  32. if (query) {
  33. var bool = query["bool"];
  34. if (bool) {
  35. var res = overRange(bool["must"], r) || overRange(bool["must_not"], r)
  36. if (res) {
  37. return true;
  38. }
  39. }
  40. }
  41. }
  42. return false;
  43. }
  44. function overRange(must, r) {
  45. if (!must) {
  46. return false;
  47. }
  48. for (var j = 0, len = must.length; j < len; j++) {
  49. var obj = must[j];
  50. if (obj) {
  51. var range = obj["range"];
  52. if (range) {
  53. var values = Object.values(range);
  54. if (values && values.length > 0) {
  55. var leafObj = values[0];
  56. var minTime = leafObj["gte"];
  57. if (typeof(minTime) == 'undefined') {
  58. minTime = leafObj["gt"];
  59. }
  60. var maxTime = leafObj["lte"];
  61. if (typeof(maxTime) == 'undefined') {
  62. maxTime = leafObj["lt"];
  63. }
  64. if (typeof(maxTime) == 'number' && typeof(minTime) == 'number') {
  65. var limit = maxTime - minTime;
  66. if (limit > 86400000) { // 24hour
  67. return true;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return false;
  75. }

注:njs脚本仅是能跑通,优化后继再做!
86400000是毫秒数,也就是24小时。

文档更新时间: 2019-06-04 13:06   作者:admin