我一直在使用openresty进行简单的请求转发,它正在按预期工作,我使用以下代码将每个传入请求转发到另一个URL:
location /app/ {
proxy_pass https://example.com/abc/;
proxy_read_timeout 60s;
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Frame-Options "SAMEORIGIN" always;我用下面的代码记录每个帖子请求:
server {
log_format post_logs '[$time_local] "$request" $status '
'$body_bytes_sent "$http_referer" '
'"$http_user_agent" [$request_body]';
}
location /app/ {
access_log logs/post.log post_logs;
}现在,我的要求是,在转发每个请求之前,我希望筛选针对特定字符串/关键字的post请求正文数据,如果在post数据中找到特定的字符串/关键字,则只应该将其转发到代理URL https://example.com/abc/。
我做了一些研究,但没有找到任何帮助我做到这一点,谁能帮忙吗?
发布于 2022-11-25 13:18:53
最后我在lua的帮助下完成了这件事:
location /app/ {
.
.
.
access_by_lua '
ngx.req.read_body()
local data = ngx.req.get_body_data()
local match = ngx.re.match(ngx.var.request_body, "<reqid>search</reqid>")
#local match = ngx.re.match(ngx.var.request_body, "<reqid>search</reqid>","i") for case insensitive match
if match then
#nothing to do
else
return ngx.exit(ngx.HTTP_FORBIDDEN)
end';
proxy_pass https://example.com/abc/;
} 在这里,请求体数据将被赋值给变量' data‘,使用ngx.re.match,我们可以将字符串/关键字与请求体数据匹配,在上面的示例中,如果在请求正文中找不到<reqid>search</reqid>,那么它将返回403禁止的,如果找到,它将被传递给proxy_pass。
在处理之前,它对于过滤传入的请求非常有用。
发布于 2022-11-08 07:27:40
对Lua不太了解,但希望这能帮上忙:
location /app {
set $flag_true 1;
set_by_lua $flag '
local token = ngx.var.arg_token
local request_body = ngx.req.get_body_data()
ngx.req.read_body()
# do your filtering here
if string.find(request_body, "tiger") then
return ngx.var.flag_true
end
return "false"
';
if ($flag = 1) {
proxy_pass http://x.x.x.x:8092;
break;
}
echo "You do not have permission: $flag";
}灵感来源于:
https://stackoverflow.com/questions/74356646
复制相似问题