Search code examples
nginxluaopenresty

NGINX: How to process one location using different directives and rules depending on content-type?


Is there a way, not workaround, to process one location ~* <ADDRESS> {} but with different value of proxy_request_buffering (on/off) depending on content-type? For example, if multipart/form-data than proxy_request_buffering has to be off, and for other requests on. These kind of directives cannot be set dynamicly by variable or within IF condition. Hence, it should be something like one location as entrypoint that forwards requests to other sub-locations i suppose. But how can it be done? Please help.

It is application-specific thing, and one request type can be used for many purposes. That's why i cannot split them. Information about what type is stored within POST body. OR content-type is also good sign to decouple them.

The code example:

    location ~* /application/service$ {
        client_max_body_size '5000m';
        client_body_buffer_size '1m';

        proxy_request_buffering on;
        proxy_buffering on;

        rewrite_by_lua_file /etc/nginx/lua/service.lua;
        
        include /etc/nginx/conf.d/common/reverse.conf;
        proxy_pass $proxy_address;
    }

The goal is to be able to set the derectives client_max_body_size and proxy_request_buffering based on content-type.

Client -bigfile----*       *--> sub-location (buffering is off)
                    \     /
                   location
                    /     \
Client -regular----*       *--> sub-location (buffering is on) 

Solution

  • to process one location ~* {} but with different value of proxy_request_buffering (on/off) depending on content-type

    i met the same question. i think the easiest way to solve the problem is using nginx internal redirect mechanism. the code exmaple:

    location @multi_part {
        internal;
        proxy_buffering off;
        proxy_pass http://localserve;
    }
    
    location /kodo_test {
        rewrite_by_lua_block{
            if ngx.var.http_content_type == "multipart/form-data" then 
                return ngx.exec("@multi_part", nil)
            end 
        }
        proxy_buffering on;
        proxy_pass http://localserve;
    }