如何解决nginx匹配请求主体,不使用lua模块
nginx中是否可以根据请求正文是否包含字符串来执行某些操作? 我确定我可以使用Lua模块来做到这一点。 我试图找出是否有一种方法可以单独使用nginx。
我希望下面的内容可以工作。
location /students-api {
if ($request_body ~* "(.*)specialstudent(.*)" ) {
set $student_status 'special';
}
// and use student_status for some logic
}
解决方法
我认为它应该有效,但是需要对其进行测试。在实践中,我仅将function base64ToArrayBuffer(data) {
const bString = window.atob(data);
const bLength = bString.length;
const bytes = new Uint8Array(bLength);
for (let i = 0; i < bLength; i++) {
bytes[i] = bString.charCodeAt(i);
}
return bytes;
}
function base64toPDF(base64EncodedData,fileName = 'file') {
const bufferArray = base64ToArrayBuffer(base64EncodedData);
const blobStore = new Blob([bufferArray],{ type: 'application/pdf' });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blobStore);
return;
}
const data = window.URL.createObjectURL(blobStore);
const link = document.createElement('a');
document.body.appendChild(link);
link.href = data;
link.download = `${fileName}.pdf`;
link.click();
window.URL.revokeObjectURL(data);
link.remove();
}
用于记录日志,不确定在请求处理的重写阶段是否可用。这是一个official description,其中说:
当请求正文被读取到{{3}时,变量的值在
proxy_pass
,fastcgi_pass
,uwsgi_pass
和scgi_pass
指令处理的位置可用}}。
此外,如果以后不使用它们,则不需要那些捕获组来检查变量是否存在子字符串(实际上,您只是在浪费资源将其保留在内存中),只需$request_body
足够。
更新
这是另一种更有可能工作的方法,因为if ($request_body ~* "specialstudent") { ... }
指令肯定比请求处理的重写阶段晚执行:
proxy_add_header
更新2
在测试完所有这些之后,我可以确认map $request_body $special {
~*"specialstudent" "special";
# otherwise '$special' variable value will be empty
}
server {
...
location /students-api {
...
proxy_add_header X-Student-Status $special;
...
}
}
方法不起作用:
if
不出所料,server {
...
location /students-api {
if ($request_body ~* "specialstudent") {
set $student_status "special";
}
proxy_add_header X-Student-Status $student_status;
...
}
}
变量不会在请求处理的重写阶段初始化。但是,$request_body
方法可以正常工作:
map
真正令我惊讶的是,以下示例未设置两个标头中的任何一个:
map $request_body $student_status {
~*"specialstudent" "special";
# otherwise '$special' variable value will be empty
}
server {
...
location /students-api {
proxy_add_header X-Student-Status $student_status;
...
}
}
在请求处理的早期重写阶段以某种方式访问map $request_body $student_status {
~*"specialstudent" "special";
# otherwise '$special' variable value will be empty
}
server {
...
location /students-api {
if ($request_body ~* "specialstudent") {
set $student_special "special";
}
proxy_add_header X-Student-Status $student_status;
proxy_add_header X-Student-Special $student_special;
...
}
}
变量会使$request_body
转换也停止工作。我暂时没有这种行为的解释,如果有人可以解释这里发生的事情,我将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。