Nginx auth_request 处理程序正在访问 POST 请求主体?

Nginx auth_request handler accessing POST request body?

我正在使用 Nginx(版本 1.9.9)作为我的后端服务器的反向代理。它需要根据POST请求的内容执行authentication/authorization。我在阅读 auth_request 处理程序中的 POST 请求正文时遇到问题。这是我得到的。

Nginx 配置(相关部分):

server {
    location / {
        auth_request /auth-proxy;
        proxy_pass http://backend/;
    }

    location = /auth-proxy {
        internal;
        proxy_pass http://auth-server/;
        proxy_pass_request_body on;
        proxy_no_cache "1";
    }
}

在我的授权服务器代码 (Python 2.7) 中,我尝试这样读取请求正文:

class AuthHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def get_request_body(self):
        content_len = int(self.headers.getheader('content-length', 0))
        content = self.rfile.read(content_len)
        return content

我打印了 content_len,它的值是正确的。但是,self.rfile.read() 只会挂起。最终它会超时并且 returns “[Errno 32] Broken pipe”.

这是我将测试数据发布到服务器的方式:

$ curl --data '12345678' localhost:1234

上面的命令也挂起并最终超时并打印 "Closing connection 0".

我在做什么有明显的错误吗?

非常感谢!

nginx-auth-request-module 的代码是 annotated at nginx.com。该模块总是用空缓冲区替换 POST body。

在其中一个 tutorials 中,他们解释了原因,指出:

As the request body is discarded for authentication subrequests, you will need to set the proxy_pass_request_body directive to off and also set the Content-Length header to a null string

原因是 auth 子请求是通过 HTTP GET 方法发送的,而不是 POST。由于 GET 没有 body,body 被丢弃。现有模块的唯一解决方法是从请求 body 中提取所需的信息并将其放入传递给身份验证服务的 HTTP header。