OpenResty: Ответ - PullRequest
       10

OpenResty: Ответ

0 голосов
/ 02 сентября 2018

Я хочу перехватить тело ответа http и отправить его в другую конечную точку, используя resty.http. Вот мой конфиг, я не могу получить тело внутри content_by_lua_block (это ноль). Тело ответа печатается в access.log, но отсутствует внутри content_by_lua_block. Любые указатели приветствуются. Я новичок в openresty и ссылки приветствуются.

--- nginx.conf --- start 

    worker_processes  1;
    error_log /tmp/error.log debug;
    events {
        worker_connections  128;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        lua_package_path "/usr/local/Cellar/openresty/1.13.6.2/lualib/resty/?.lua;;";
        lua_need_request_body on;
        log_format bodylog '$remote_addr - $remote_user [$time_local] '
          '"$request" $status $body_bytes_sent '
          '"$http_referer" "$http_user_agent" $request_time '
          '<"$request_body" >"$response_body"';

        server {
            listen       8081 ;
            server_name 127.0.0.1;
            set $response_body "";
            location /posts/1 {
                            mirror /mirror;
                            mirror_request_body on;
                            proxy_pass  http://localhost:3000/posts/1;
                            access_log /tmp/access.log bodylog;
                         }
            location = /mirror {   // mirror traffic for requests
                    internal;
                    proxy_pass http://127.0.0.1:8080/analyzeservlet-1.0.0/analyzeservlet;
                    content_by_lua_block {
                            if ngx.var.resp_body ~= "" then
                                    local http = require "resty.http"
                                    local httpc = http.new()
                                    local res, err = httpc:request_uri("<uri", {
                                    method = "POST",
                                    body = ngx.var.resp_body,
                                    headers = {
                                                    ["Content-Type"] = "application/x-www-form-urlencoded",
                                              } })
                                    end
                            }
                    }
            }
            client_body_buffer_size 16k;
            client_max_body_size 16k;
            body_filter_by_lua_block {
                            local response_body = string.sub(ngx.arg[1], 1, 1000)
                            ngx.ctx.buffered = (ngx.ctx.buffered or "") .. response_body
                        if ngx.arg[2] then
                                ngx.var.response_body = ngx.ctx.buffered
                        end
        }
include servers/*;
}
...