我正在使用Nginx(版本1.9.9)作为我的后端服务器的反向代理.它需要根据POST请求的内容执行身份验证/授权.我在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";

}

}

在我的auth-server代码(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()将挂起.最终它会超时并返回“[Errno 32] Broken pipe”.

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

$curl --data '12345678' localhost:1234

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

我正在做什么明显的错误?

非常感谢!

最佳答案:

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

在其中一个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没有身体,身体被丢弃.现有模块的唯一解决方法是从请求正文中提取所需信息,并将其放入传递给auth服务的HTTP标头中.

标签:nginx,auth-request

来源: https://codeday.me/bug/20190516/1114665.html

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐