Small blog about system administration.

Parse POST body request into nginx variable

There is a form input module you can use to parse POST body into a variable:

https://github.com/calio/form-input-nginx-module

Installation
        Get the nginx source code from nginx.net ( http://nginx.net/ ).
        Get the ngx_devel_kit source code from (http://github.com/simpl/ngx_devel_kit)
        Unpack the source code and build nginx with this module.

        $ wget 'http://nginx.org/download/nginx-1.7.4.tar.gz'
        $ tar -xzvf nginx-1.7.4.tar.gz
        $ cd nginx-1.7.4/

        $ git-clone http://github.com/simpl/ngx_devel_kit.git
        $ git-clone http://github.com/calio/form-input-nginx-module.git

        $ ./configure --add-module=/somepath/form-input-nginx-module --add-module=/somepath/ngx_devel_kit
        $ make -j2
        $ make install

Usage
        set_form_input $variable;
        set_form_input $variable argument;

        set_form_input_multi $variable;
        set_form_input_multi $variable argument;

    example:

        #nginx.conf

        location /foo {
            # ensure client_max_body_size == client_body_buffer_size
            client_max_body_size 100k;
            client_body_buffer_size 100k;

            set_form_input $data;    # read "data" field into $data
            set_form_input $foo foo; # read "foo" field into $foo
        }

        location /bar {
            # ensure client_max_body_size == client_body_buffer_size
            client_max_body_size 1m;
            client_body_buffer_size 1m;

            set_form_input_multi $data; # read all "data" field into $data
            set_form_input_multi $foo data; # read all "data" field into $foo

            array_join ' ' $data; # now $data is an string
            array_join ' ' $foo;  # now $foo is an string
        }

No comments :

Post a Comment