Nginx auth_request handler accessing the body of a POST request?

I am using Nginx (version 1.9.9) as a reverse proxy for my internal server. It should perform authentication / authorization based on the contents of the POST requests. And I was having trouble reading the body of the POST request in my auth_request handler. Here is what I got.

Nginx configuration (corresponding part):

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";
    }
}

And in my auth-server server code (Python 2.7), I am trying to read the request body as follows:

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

I printed content_len and had the correct value. However, self.rfile.read () will just hang. And in the end, it will time out and return "[Errno 32] Broken pipe".

This is how I posted the test data on the server:

$ curl --data '12345678' localhost:1234

" 0".

- , ?

!

+4
1

nginx-auth-request nginx.com. POST .

, :

, proxy_pass_request_body, Content-Length

, auth HTTP GET, POST. GET , . HTTP-, auth.

+5

Source: https://habr.com/ru/post/1661008/


All Articles