hi, 大佬们好, 我在做一个前端反向代理工具, 遇到一个不理解的报错

        location / {
            lua_need_request_body on;
            set $upp '';
            set $cookie_domain $host;
            content_by_lua_file conf/lua/proxy.lua;
            log_by_lua_block {
                local log = require "log";
                log[ngx.ctx.uid].sent = true;
            }
        }

        location /proxy {
            internal;
            proxy_cookie_domain ~.*$ $cookie_domain;
            proxy_cookie_path ~.*$ /;
            proxy_set_header 'Access-Control-Allow-Origin' '*';
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_pass $upp;
        }

这是我的 lua 代码


local res = ngx.location.capture("/proxy", {
    share_all_vars = true
});


for k, v in pairs(res.header) do
    ngx.header[k] = v;
end
ngx.status = res.status;
ngx.print(res.body)

原因应该是 ngx.location.capture 的请求升级为 websocket 了, 有什么办法能避免这个报错吗

    找到原因了

    ngx.location.capture

    这里有个提示

    See HTTP method constants methods other than POST. The method option is ngx.HTTP_GET by default.

    默认是 GET 方法, 但代理的请求是其他方法, 修正如下

    
    local method_map = {
        GET = ngx.HTTP_GET,
        HEAD = ngx.HTTP_HEAD,
        PUT = ngx.HTTP_PUT,
        POST = ngx.HTTP_POST,
        DELETE = ngx.HTTP_DELETE,
        OPTIONS = ngx.HTTP_OPTIONS,
        MKCOL = ngx.HTTP_MKCOL,
        COPY = ngx.HTTP_COPY,
        MOVE = ngx.HTTP_MOVE,
        PROPFIND = ngx.HTTP_PROPFIND,
        PROPPATCH = ngx.HTTP_PROPPATCH,
        LOCK = ngx.HTTP_LOCK,
        UNLOCK = ngx.HTTP_UNLOCK,
        PATCH = ngx.HTTP_PATCH,
        TRACE = ngx.HTTP_TRACE
    }
    
    local method = ngx.req.get_method()
    
    local res =
        ngx.location.capture(
        "/proxy",
        {
            method = method_map[method],
            share_all_vars = true
        }
    )
    Write a Reply...