Set Misc#

The set-misc module adds set_* directives that compute variable values: they escape and unescape URIs, quote strings for SQL or JSON, encode and decode HEX, Base32, and Base64, compute MD5, SHA-1, and HMAC digests, generate random values, and more. These directives can be mixed freely with the directives of the Rewrite module, such as set and if.

Installation#

To install the module, use one of the following packages:

  • Angie: angie-module-set-misc

  • Angie PRO: angie-pro-module-set-misc

Loading the Module#

To work with the module, it must be loaded in the context of main{}. The example below also uses directives from the Echo module:

load_module modules/ndk_http_module.so;
load_module modules/ngx_http_set_misc_module.so;
load_module modules/ngx_http_echo_module.so;

Configuration Example#

server {
    listen 80;
    server_name localhost;

    location /ifempty {
        set $a $arg_a;
        set_if_empty $a 56;

        echo "arg_a = '$arg_a'";
        echo "a = '$a'";
    }

    location /unescape {
        set_unescape_uri $a $arg_a;
        set_escape_uri $b $a;

        echo "arg_a = '$arg_a'";
        echo "a = '$a'";
        echo "b = '$b'";
    }

    location /base32 {
        set_encode_base32 $a $arg_a;
        set_decode_base32 $b $a;

        echo "arg_a = '$arg_a'";
        echo "a = '$a'";
        echo "b = '$b'";
    }

    location /hex {
        set_encode_hex $a $arg_a;
        set_decode_hex $b $a;

        echo "arg_a = '$arg_a'";
        echo "a = '$a'";
        echo "b = '$b'";
    }
}

Demonstration#

$ curl localhost/ifempty/?a=100

  arg_a = '100'
  a = '100'

$ curl localhost/ifempty

  arg_a = ''
  a = '56'

$ curl localhost/unescape/?a=Hello%20world!

  arg_a = 'Hello%20world!'
  a = 'Hello world!'
  b = 'Hello%20world!'

$ curl localhost/base32/?a=abcde

  arg_a = 'abcde'
  a = 'c5h66p35'
  b = 'abcde'

$ curl localhost/hex/?a=abcde

  arg_a = 'abcde'
  a = '6162636465'
  b = 'abcde'

Additional Information#

Version: v0.34

The full description of directives and source code is available at: openresty/set-misc-nginx-module.