r.requestBody/r.requestText/r.requestBuffer 未定义(NJS - NGINX 上的 JavaScript 模块)

问题描述 投票:0回答:1

我正在使用 JavaScript 构建 NGINX 模块,该模块读取传入请求并设计是否将它们传递到代理服务器,或者忽略它们并返回“block.html”页面作为响应 [有点 WAF]。

问题是当我尝试访问

r.requestBody/r.requestText/r.requestBuffer
(所有三种方式都应该给我整个请求)时,变量是
undefined

这里是 JavaScript 文件(目前只有“读取传入消息”功能)和 NGINX 的配置文件:

waf.js

function main(r)
{
    return "Request: " + r.requestText;
}

export default { main };

/etc/nginx/conf.d/default.conf

js_path "/etc/nginx/njs";
js_import waf.js;

js_set $return_value_of_main_func_in_waf waf.main;
log_format ret_val $return_value_of_main_func_in_waf;

server {
    listen       80;
    server_name  localhost;

    access_log  /var/log/nginx/njs_output.log ret_val;

    location / {
        js_content waf.main;
    }

    location /block.html {
        root   /usr/share/nginx/html;
    }

    location @app-backend {
        proxy_pass http://127.0.0.1:7777;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

njs_output.log

Request: undefined
Request: undefined
Request: undefined
Request: undefined

我尝试访问

r
变量中的其他内容,例如
r.rawHeadersIn
,输出很好:

Host,localhost,Connection,keep-alive,Cache-Control,max-age=0,sec-ch-ua,\x22Not?A_Brand\x22;v=\x228\x22, \x22Chromium\x22;v=\x22108\x22, \x22Google Chrome\x22;v=\x22108\x22,sec-ch-ua-mobile,?0,sec-ch-ua-platform,\x22Windows\x22,Upgrade-Insecure-Requests,1,User-Agent,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36,Accept,text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9,Sec-Fetch-Site,none,Sec-Fetch-Mode,navigate,Sec-Fetch-User,?1,Sec-Fetch-Dest,document,Accept-Encoding,gzip, deflate, br,Accept-Language,he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7,Cookie,security_level=0; security=low; PHPSESSID=ruioabebdprspfihjst4vu66he; session=9603a1c6-1fe4-4f93-be1a-68ad6a5738ed.vsLXilN0X5P1XWLCWBveUL1PkW8
javascript nginx nginx-config nginx-module njs
1个回答
0
投票

我在 njs 存储库上发现了 an issues 来解决这个问题。它有一个指向博客文章的链接,其中解决方案是使用镜像指令

例如

location / {
  # non-existent url but forces body to be read
  mirror /sbVE8Tvdrpjqf0D47mObQ97kzj7JPf
  js_content waf.main;
}
© www.soinside.com 2019 - 2024. All rights reserved.